|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/chainreactors/fingers" |
| 14 | + "github.com/chainreactors/fingers/common" |
| 15 | + "github.com/chainreactors/fingers/resources" |
| 16 | + "github.com/chainreactors/utils/encode" |
| 17 | + "github.com/chainreactors/utils/httputils" |
| 18 | + "github.com/jessevdk/go-flags" |
| 19 | + "gopkg.in/yaml.v3" |
| 20 | +) |
| 21 | + |
| 22 | +var opts struct { |
| 23 | + // 指定要使用的引擎,多个引擎用逗号分隔 |
| 24 | + Engines string `short:"e" long:"engines" description:"Specify engines to use (comma separated)" default:"fingers,fingerprinthub,wappalyzer,ehole,goby"` |
| 25 | + |
| 26 | + // 是否忽略SSL证书验证 |
| 27 | + InsecureSSL bool `short:"k" long:"insecure" description:"Skip SSL certificate verification"` |
| 28 | + |
| 29 | + // 目标URL |
| 30 | + URL string `short:"u" long:"url" description:"Target URL to fingerprint" required:"true"` |
| 31 | + |
| 32 | + // 是否显示详细信息 |
| 33 | + Verbose bool `short:"v" long:"verbose" description:"Show verbose debug information"` |
| 34 | + |
| 35 | + // 是否只检测favicon |
| 36 | + FaviconOnly bool `short:"f" long:"favicon" description:"Only detect favicon"` |
| 37 | + |
| 38 | + // 资源文件覆盖 |
| 39 | + GobyFile string `long:"goby" description:"Override goby.json.gz with custom file"` |
| 40 | + FingerprintHubFile string `long:"fingerprinthub" description:"Override fingerprinthub_v3.json.gz with custom file"` |
| 41 | + EholeFile string `long:"ehole" description:"Override ehole.json.gz with custom file"` |
| 42 | + FingersFile string `long:"fingers" description:"Override fingers_http.json.gz with custom file"` |
| 43 | + WappalyzerFile string `long:"wappalyzer" description:"Override wappalyzer.json.gz with custom file"` |
| 44 | + AliasesFile string `long:"aliases" description:"Override aliases.yaml with custom file"` |
| 45 | +} |
| 46 | + |
| 47 | +// 处理资源文件覆盖 |
| 48 | +func processResourceFile(filePath string) ([]byte, error) { |
| 49 | + if filePath == "" { |
| 50 | + return nil, nil |
| 51 | + } |
| 52 | + |
| 53 | + data, err := ioutil.ReadFile(filePath) |
| 54 | + if err != nil { |
| 55 | + return nil, fmt.Errorf("failed to read file %s: %v", filePath, err) |
| 56 | + } |
| 57 | + |
| 58 | + ext := strings.ToLower(filepath.Ext(filePath)) |
| 59 | + |
| 60 | + // 如果是JSON文件,转换为YAML |
| 61 | + if ext == ".json" { |
| 62 | + var jsonData interface{} |
| 63 | + if err := json.Unmarshal(data, &jsonData); err != nil { |
| 64 | + return nil, fmt.Errorf("failed to parse JSON file %s: %v", filePath, err) |
| 65 | + } |
| 66 | + |
| 67 | + data, err = yaml.Marshal(jsonData) |
| 68 | + if err != nil { |
| 69 | + return nil, fmt.Errorf("failed to convert JSON to YAML for file %s: %v", filePath, err) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // 如果文件不是.gz结尾,进行gzip压缩 |
| 74 | + if !strings.HasSuffix(filePath, ".gz") { |
| 75 | + compressedData, err := encode.GzipCompress(data) |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("failed to compress file %s: %v", filePath, err) |
| 78 | + } |
| 79 | + data = compressedData |
| 80 | + } |
| 81 | + |
| 82 | + return data, nil |
| 83 | +} |
| 84 | + |
| 85 | +func main() { |
| 86 | + // 解析命令行参数 |
| 87 | + _, err := flags.Parse(&opts) |
| 88 | + if err != nil { |
| 89 | + os.Exit(1) |
| 90 | + } |
| 91 | + |
| 92 | + // 处理资源文件覆盖 |
| 93 | + if opts.GobyFile != "" { |
| 94 | + if data, err := processResourceFile(opts.GobyFile); err != nil { |
| 95 | + fmt.Println(err) |
| 96 | + os.Exit(1) |
| 97 | + } else { |
| 98 | + resources.GobyData = data |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if opts.FingerprintHubFile != "" { |
| 103 | + if data, err := processResourceFile(opts.FingerprintHubFile); err != nil { |
| 104 | + fmt.Println(err) |
| 105 | + os.Exit(1) |
| 106 | + } else { |
| 107 | + resources.Fingerprinthubdata = data |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if opts.EholeFile != "" { |
| 112 | + if data, err := processResourceFile(opts.EholeFile); err != nil { |
| 113 | + fmt.Println(err) |
| 114 | + os.Exit(1) |
| 115 | + } else { |
| 116 | + resources.EholeData = data |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if opts.FingersFile != "" { |
| 121 | + if data, err := processResourceFile(opts.FingersFile); err != nil { |
| 122 | + fmt.Println(err) |
| 123 | + os.Exit(1) |
| 124 | + } else { |
| 125 | + resources.FingersHTTPData = data |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if opts.WappalyzerFile != "" { |
| 130 | + if data, err := processResourceFile(opts.WappalyzerFile); err != nil { |
| 131 | + fmt.Println(err) |
| 132 | + os.Exit(1) |
| 133 | + } else { |
| 134 | + resources.WappalyzerData = data |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if opts.AliasesFile != "" { |
| 139 | + if data, err := processResourceFile(opts.AliasesFile); err != nil { |
| 140 | + fmt.Println(err) |
| 141 | + os.Exit(1) |
| 142 | + } else { |
| 143 | + resources.AliasesData = data |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // 创建HTTP客户端 |
| 148 | + client := &http.Client{ |
| 149 | + Transport: &http.Transport{ |
| 150 | + TLSClientConfig: &tls.Config{ |
| 151 | + InsecureSkipVerify: opts.InsecureSSL, |
| 152 | + }, |
| 153 | + }, |
| 154 | + } |
| 155 | + |
| 156 | + // 创建引擎实例 |
| 157 | + var engineNames []string |
| 158 | + if opts.Engines != "" { |
| 159 | + engineNames = strings.Split(opts.Engines, ",") |
| 160 | + } |
| 161 | + |
| 162 | + engine, err := fingers.NewEngine(engineNames...) |
| 163 | + if err != nil { |
| 164 | + fmt.Printf("Failed to create engine: %v\n", err) |
| 165 | + os.Exit(1) |
| 166 | + } |
| 167 | + |
| 168 | + if opts.Verbose { |
| 169 | + fmt.Printf("Loaded engines: %s\n", engine.String()) |
| 170 | + } |
| 171 | + |
| 172 | + // 发送HTTP请求 |
| 173 | + resp, err := client.Get(opts.URL) |
| 174 | + if err != nil { |
| 175 | + fmt.Printf("Failed to request URL %s: %v\n", opts.URL, err) |
| 176 | + os.Exit(1) |
| 177 | + } |
| 178 | + defer resp.Body.Close() |
| 179 | + |
| 180 | + // 检测指纹 |
| 181 | + var frames common.Frameworks |
| 182 | + if opts.FaviconOnly { |
| 183 | + content := httputils.ReadBody(resp) |
| 184 | + frame := engine.DetectFavicon(content) |
| 185 | + if frame != nil { |
| 186 | + frames.Add(frame) |
| 187 | + } |
| 188 | + } else { |
| 189 | + frames = engine.Match(resp) |
| 190 | + } |
| 191 | + |
| 192 | + // 输出结果 |
| 193 | + if opts.Verbose { |
| 194 | + fmt.Printf("\nDetected frameworks for %s:\n", opts.URL) |
| 195 | + for _, frame := range frames { |
| 196 | + fmt.Printf("Name: %s\n", frame.Name) |
| 197 | + fmt.Printf("Vendor: %s\n", frame.Vendor) |
| 198 | + fmt.Printf("Product: %s\n", frame.Product) |
| 199 | + fmt.Printf("Version: %s\n", frame.Version) |
| 200 | + fmt.Printf("CPE: %s\n", frame.CPE()) |
| 201 | + fmt.Printf("---\n") |
| 202 | + } |
| 203 | + } else { |
| 204 | + fmt.Println(frames.String()) |
| 205 | + } |
| 206 | +} |
0 commit comments