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
| package main
import ( "bytes" "crypto" "crypto/rand" "crypto/rsa" "crypto/sha256" "crypto/x509" "encoding/base64" "encoding/json" "encoding/pem" "fmt" "io/ioutil" "net/http" "os" "sort" "time" )
func genSign(s []byte) string { hashed := sha256.Sum256(s) privateKeyPEM := []byte("-----BEGIN RSA PRIVATE KEY-----\r\nMIIEogIBAAKCAQEAv0xjefuBTF6Ox940ZqLLUFFBDtTcB9dAfDjWgyZ2A55K+VdG\r\nc1L5LqJWuyRkhYGFTlI4K5hRiExvjXuwIEed1norp5cKdeTLJwmvPyFgaEh7Ow19\r\nTu9sTR5hHxThjT8ieArB2kNAdp8Xoo7O8KihmBmtbJ1umRv2XxG+mm2ByPZFlTdW\r\nRFU38oCPkGKlrl/RzOJKRYMv10s1MWBPY6oYkRiOX/EsAUVae6zKRqNR2Q4HzJV8\r\ngOYMPvqkau8hwN8i6r0z0jkDGCRJSW9djWk3Byi3R2oSdZ0IoS+91MFtKvWYdnNH\r\n2Ubhlnu1P+wbeuIFdp2u7ZQOtgPX0mtQ263e5QIDAQABAoIBAD67GwfeTMkxXNr3\r\n5/EcQ1XEP3RQoxLDKHdT4CxDyYFoQCfB0e1xcRs0ywI1be1FyuQjHB5Xpazve8lG\r\nnTwIoB68E2KyqhB9BY14pIosNMQduKNlygi/hKFJbAnYPBqocHIy/NzJHvOHOiXp\r\ndL0AX3VUPkWW3rTAsar9U6aqcFvorMJQ2NPjijcXA0p1MlZAZKODO2wqidfQ487h\r\nxy0ZkriYVi419j83a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----") block, _ := pem.Decode(privateKeyPEM) privateKey, _ := x509.ParsePKCS1PrivateKey(block.Bytes)
signature, _ := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hashed[:]) return base64.StdEncoding.EncodeToString(signature) }
func genTimestampStr() string { now := time.Now() unixMilli := now.UnixNano() / int64(time.Millisecond) return fmt.Sprintf("%d", unixMilli) }
func generateData(params map[string]string) string { keys := make([]string, 0, len(params)) for k := range params { keys = append(keys, k) } sort.Strings(keys)
var buf bytes.Buffer for _, k := range keys { v := fmt.Sprintf("%v", params[k]) if v != "" { buf.WriteString(k) buf.WriteString(v) } } return buf.String() }
type Response struct { Code int `json:"code"` Message string `json:"message"` Data struct { Categories []struct { ID int `json:"id"` Name string `json:"name"` Total int `json:"total"` Rules []struct { Name string `json:"name"` Code string `json:"code"` } `json:"rules"` } `json:"categories"` Total int `json:"total"` Page struct { Num int `json:"num"` Size int `json:"size"` Total int `json:"total"` } `json:"page"` } `json:"data"` }
func SaveCategoriesData(response Response) { for _, category := range response.Data.Categories { for _, rule := range category.Rules { f, _ := os.OpenFile("rules.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) defer f.Close() f.WriteString(rule.Name + ":::" + rule.Code + "\n") } } }
func main() { auth := "<auth>"
arguments := map[string]string{"cid": "10579", "keyword": "", "page": "1", "size": "10", "ts": genTimestampStr()} firstReq, _ := http.NewRequest("GET", "https://api.fofa.info/v1/rules/categories?cid="+arguments["cid"]+"&keyword=&page=1&size=10&ts="+arguments["ts"]+"&sign="+genSign([]byte(generateData(arguments)))+"&app_id=9e9fb94330d97833acfbc041ee1a76793f1bc691", nil) firstReq.Header.Add("Authorization", auth) httpResp, err := http.DefaultClient.Do(firstReq) if err != nil { panic(err) }
var resp Response err = json.NewDecoder(httpResp.Body).Decode(&resp) if err != nil { panic(err) }
totalPage := resp.Data.Page.Total/resp.Data.Page.Size + 1 for i := 101; i <= totalPage; i++ { fmt.Println("正在爬取第" + fmt.Sprintf("%d", i) + "页数据,总页数:" + fmt.Sprintf("%d", totalPage)) arguments["page"] = fmt.Sprintf("%d", i) arguments["ts"] = genTimestampStr() req, _ := http.NewRequest("GET", "https://api.fofa.info/v1/rules/categories?cid="+arguments["cid"]+"&keyword=&page="+arguments["page"]+"&size=10&ts="+arguments["ts"]+"&sign="+genSign([]byte(generateData(arguments)))+"&app_id=9e9fb94330d97833acfbc041ee1a76793f1bc691", nil) req.Header.Add("Authorization", auth) httpResp, err := http.DefaultClient.Do(req) if err != nil { panic(err) } content, _ := ioutil.ReadAll(httpResp.Body) fmt.Println(string(content))
err = json.NewDecoder(httpResp.Body).Decode(&resp) if err != nil { panic(err) } SaveCategoriesData(resp) } }
|