1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| package main
import ( "context" "fmt" "io" "net" "net/http" "net/url" "strings" "time" )
type SSRFProtector struct { AllowedSchemes []string AllowedPorts []int Timeout time.Duration MaxBodySize int64 }
func NewSSRFProtector() *SSRFProtector { return &SSRFProtector{ AllowedSchemes: []string{"http", "https"}, AllowedPorts: []int{80, 443, 8080}, Timeout: 10 * time.Second, MaxBodySize: 10 * 1024 * 1024, } }
func (p *SSRFProtector) ValidateURL(urlStr string) error { parsedURL, err := url.Parse(urlStr) if err != nil { return fmt.Errorf("无效的URL: %v", err) } schemeAllowed := false for _, scheme := range p.AllowedSchemes { if strings.ToLower(parsedURL.Scheme) == scheme { schemeAllowed = true break } } if !schemeAllowed { return fmt.Errorf("不允许的协议: %s", parsedURL.Scheme) } hostname := parsedURL.Hostname() if hostname == "" { return fmt.Errorf("无效的主机名") } ips, err := net.LookupIP(hostname) if err != nil { return fmt.Errorf("无法解析域名: %v", err) } for _, ip := range ips { if isPrivateIP(ip) { return fmt.Errorf("禁止访问内网地址: %s", ip.String()) } } port := parsedURL.Port() if port == "" { if parsedURL.Scheme == "https" { port = "443" } else { port = "80" } } portAllowed := false for _, allowedPort := range p.AllowedPorts { if port == fmt.Sprintf("%d", allowedPort) { portAllowed = true break } } if !portAllowed { return fmt.Errorf("不允许的端口: %s", port) } return nil }
func isPrivateIP(ip net.IP) bool { if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() { return true } privateIPBlocks := []*net.IPNet{ {IP: net.IPv4(10, 0, 0, 0), Mask: net.CIDRMask(8, 32)}, {IP: net.IPv4(172, 16, 0, 0), Mask: net.CIDRMask(12, 32)}, {IP: net.IPv4(192, 168, 0, 0), Mask: net.CIDRMask(16, 32)}, {IP: net.IPv4(169, 254, 0, 0), Mask: net.CIDRMask(16, 32)}, } for _, block := range privateIPBlocks { if block.Contains(ip) { return true } } if ip.String() == "169.254.169.254" { return true } return false }
func (p *SSRFProtector) SafeRequest(urlStr string) ([]byte, error) { if err := p.ValidateURL(urlStr); err != nil { return nil, err } client := &http.Client{ Timeout: p.Timeout, CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, } ctx, cancel := context.WithTimeout(context.Background(), p.Timeout) defer cancel() req, err := http.NewRequestWithContext(ctx, "GET", urlStr, nil) if err != nil { return nil, err } resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() limitedReader := io.LimitReader(resp.Body, p.MaxBodySize) body, err := io.ReadAll(limitedReader) if err != nil { return nil, err } return body, nil }
func fetchHandler(w http.ResponseWriter, r *http.Request) { urlStr := r.URL.Query().Get("url") if urlStr == "" { http.Error(w, "缺少URL参数", http.StatusBadRequest) return } protector := NewSSRFProtector() body, err := protector.SafeRequest(urlStr) if err != nil { http.Error(w, fmt.Sprintf("请求失败: %v", err), http.StatusForbidden) return } w.Write(body) }
func main() { http.HandleFunc("/fetch", fetchHandler) fmt.Println("Server starting on :8080") http.ListenAndServe(":8080", nil) }
|