编写工具Fofa命令行版
fofa.go
package fofa
const BaseURL = "https://fofa.info" //URL为常量
type Client struct { //创建结构体来维护apikey
apiKey string
}
func New(apiKey string) *Client{ //返回实例apiKey
return &Client{apiKey: apiKey}
}
api.go
package fofa
import (
"encoding/json"
"fmt"
"net/http"
)
type APIInfo struct { //结构体处理对应json数据
Username string `json:"username"`
Category string `json:"category"`
Fcoin int `json:"fcoin"`
Isvip bool `json:"isvip"`
VipLevel int `json:"vip_level"`
FofacliVer string `json:"fofacli_ver"`
FofaServer bool `json:"fofa_server"`
}
func (s *Client) APIInfo() (*APIInfo, error) {
res, err := http.Get(fmt.Sprintf("%s/api/v1/info/my?key=%s", BaseURL, s.apiKey)) //对api接口发起请求
if err != nil{
return nil, err
}
defer res.Body.Close()
var ret APIInfo
if err := json.NewDecoder(res.Body).Decode(&ret); err != nil { //接收返回的数据
return nil, err
}
return &ret, nil
}
host.go
package fofa
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
)
type FofaSearch struct { //创建结构体对应接收的数据
Error bool `json:"error"`
ConsumedFpoint int `json:"consumed_fpoint"`
RequiredFpoints int `json:"required_fpoints"`
Size int `json:"size"`
Page int `json:"page"`
Mode string `json:"mode"`
Query string `json:"query"`
Results [][]string `json:"results"`
}
func (s *Client) HostSearch(q string) (*FofaSearch, error) {
res, err := http.Get( //Get请求查询数据
fmt.Sprintf("%s/api/v1/search/all?key=%s&qbase64=%s", BaseURL, s.apiKey, base64.StdEncoding.EncodeToString([]byte(q))),
)
if err != nil {
return nil, err
}
defer res.Body.Close()
var ret FofaSearch
if err := json.NewDecoder(res.Body).Decode(&ret); err != nil { //接收数据
return nil, err
}
return &ret, nil
}
main.go
package main
import (
"Fofa/fofa"
"fmt"
"log"
"os"
)
func main() {
if len(os.Args) != 2 {
log.Fatalln("Usage: Fofa Searchterm")
}
apiKey := "xxxxx"
s := fofa.New(apiKey)
info ,err := s.APIInfo()
if err != nil {
log.Panicln(err)
}
fmt.Printf("Username: %sn", info.Username)
hostSearch, err := s.HostSearch(os.Args[1])
if err != nil {
log.Panicln(err)
}
fmt.Printf("Query:%s | Count: %dn" ,hostSearch.Query, hostSearch.Size)
for _, host := range hostSearch.Results {
fmt.Printf("Url: %s | Ip: %s | Port: %sn", host[0], host[1], host[2])
}
}
go.mod
module Fofa
运行结果
知识星球
原文始发于微信公众号(CatalyzeSec):Golang学习-fofa命令行工具编写
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论