This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
config.go
266 lines (238 loc) · 7.13 KB
/
config.go
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"bytes"
"crypto/tls"
"encoding/base64"
"flag"
"fmt"
"hash"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
"github.com/twmb/murmur3"
)
//nolint
var (
h bool
v bool
version string
commit string
date string
Branch string
GitDirty string
HashURL string
Hashfile string
ImageBase64 string
UserAgent string
IsUint32 bool
FofaFormat bool
ShodanFormat bool
InsecureSkipVerify bool
ReqTimeOut int = 10
Debug bool
DefaultUA string = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
)
// PrintVersion 打印版本信息
func PrintVersion() {
fmt.Printf("Version: %s\n", version)
fmt.Printf("Compile: %s\n", date)
fmt.Printf("Commit: %s\n", commit)
fmt.Printf("Branch: %s\n", Branch)
fmt.Printf("GitDirty: %s\n", GitDirty)
}
//nolint:gochecknoinits // this is init
func init() {
flag.BoolVar(&h, "h", false, "look help \n iconhash favicon.ico \n iconhash https://www.baidu.com/favicon.ico")
flag.BoolVar(&v, "v", false, "version")
flag.BoolVar(&Debug, "debug", false, "debug mode")
flag.BoolVar(&FofaFormat, "fofa", true, "fofa search format")
flag.BoolVar(&ShodanFormat, "shodan", false, "shodan search format \n iconhash -file test/favicon.ico -shodan -fofa=false")
flag.BoolVar(&IsUint32, "uint32", false, "uint32")
flag.BoolVar(&InsecureSkipVerify, "skip-verify", true, "https InsecureSkipVerify")
flag.StringVar(&Hashfile, "file", "", "mmh3 hash from file \n iconhash -file favicon.ico")
flag.StringVar(&HashURL, "url", "", "mmh3 hash from url \n iconhash -url https://www.baidu.com/favicon.ico")
flag.StringVar(&UserAgent, "user-agent", DefaultUA, "mmh3 hash from url")
flag.StringVar(&ImageBase64, "b64", "", "mmh3 hash image base64 from file \n iconhash -file test/favicon.ico ")
IconHashArgs := map[string]int{
"-h": 1,
"-v": 1,
"-fofa": 1,
"-shodan": 1,
"-uint32": 1,
"-file": 1,
"-url": 1,
"-user-agent": 1,
"-b64": 1,
"-debug": 1,
"-skip-verify": 1,
}
flag.Parse()
if v {
PrintVersion()
return
}
if h || len(os.Args) == 1 {
flag.Usage()
return
}
// nolint
if len(os.Args) == 2 {
arg := os.Args[1]
if _, ok := IconHashArgs[arg]; !ok {
FofaFormat = false
if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
HashURL = arg
} else if _, err := os.Stat(arg); err == nil {
Hashfile = arg
} else {
fmt.Print("not file or url please check\n")
os.Exit(1)
}
} else {
flag.Usage()
os.Exit(0)
}
}
if len(os.Args) > 2 && !strings.HasPrefix(os.Args[1], "-") {
flag.Usage()
os.Exit(1)
}
if Debug {
fmt.Print("--------------------------- var value --------------------------------\n")
fmt.Printf("h :%t\n", h)
fmt.Printf("v :%t\n", v)
fmt.Printf("Version :%s\n", version)
fmt.Printf("Commit :%s\n", commit)
fmt.Printf("Compile :%s\n", date)
fmt.Printf("HashURL :%s\n", HashURL)
fmt.Printf("Hashfile :%s\n", Hashfile)
fmt.Printf("ImageBase64 :%s\n", ImageBase64)
fmt.Printf("UserAgent :%s\n", UserAgent)
fmt.Printf("IsUint32 :%t\n", IsUint32)
fmt.Printf("FofaFormat :%t\n", FofaFormat)
fmt.Printf("ShodanFormat :%t\n", ShodanFormat)
fmt.Printf("InsecureSkipVerify :%t\n", InsecureSkipVerify)
fmt.Printf("Debug :%t\n", Debug)
fmt.Printf("DefaultUA :%s\n", DefaultUA)
defer fmt.Print("--------------------------- var value --------------------------------\n")
}
}
// PrintResult 打印结果
func PrintResult(result string) {
if !ShodanFormat && !FofaFormat {
fmt.Printf("%s\n", result)
}
if FofaFormat {
fmt.Printf("icon_hash=\"%s\"\n", result)
}
if ShodanFormat {
fmt.Printf("http.favicon.hash:%s\n", result)
}
}
// FromURLGetContent 从 URL 中获取图片内容
func FromURLGetContent(requrl string) (content []byte, err error) {
if Debug {
fmt.Print("--------------------------- start url content --------------------------------\n")
fmt.Printf("====> url: %s\n", HashURL)
defer fmt.Print("--------------------------- end url content --------------------------------\n")
}
client := &http.Client{
Timeout: time.Second * time.Duration(ReqTimeOut),
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: InsecureSkipVerify}, // param
},
}
req, err := http.NewRequest("GET", requrl, nil) //nolint
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", UserAgent)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close() //nolint
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if Debug {
fmt.Printf("===> status code: %d\n", resp.StatusCode)
fmt.Printf("====> content: \n%s\n", body)
}
return body, nil
}
// FromfileGetContent 从文件中获取图片内容
func FromfileGetContent(path string) (content []byte, err error) {
if Debug {
fmt.Print("---------------------------start From file get content--------------------------------\n")
defer fmt.Print("---------------------------end From file get content--------------------------------\n")
}
fi, err := os.Open(path)
if err != nil {
return nil, err
}
defer fi.Close() // nolint
content, err = ioutil.ReadAll(fi)
if Debug {
fmt.Printf("====> fileContent:\n %s\n", content)
}
if err != nil {
return nil, err
}
return content, nil
}
/*
Mmh3Hash32 计算 mmh3 hash
*/
func Mmh3Hash32(raw []byte) string {
var h32 hash.Hash32 = murmur3.New32()
h32.Write(raw)
if IsUint32 {
return fmt.Sprintf("%d", h32.Sum32())
}
return fmt.Sprintf("%d", int32(h32.Sum32()))
}
// StandBase64 计算 base64 的值
func StandBase64(braw []byte) []byte {
bckd := base64.StdEncoding.EncodeToString(braw)
var buffer bytes.Buffer
for i := 0; i < len(bckd); i++ {
ch := bckd[i]
buffer.WriteByte(ch)
if (i+1)%76 == 0 {
buffer.WriteByte('\n')
}
}
buffer.WriteByte('\n')
if Debug {
fmt.Print("---------------------------start base64 content--------------------------------\n")
fmt.Printf("====> base64:\n%s\n", buffer.String())
defer fmt.Print("---------------------------end base64 content--------------------------------\n")
}
return buffer.Bytes()
}
// SplitChar76 按照 76 字符切分
func SplitChar76(braw []byte) []byte {
// 去掉 data:image/vnd.microsoft.icon;base64
if strings.HasPrefix(string(braw), "data:image/vnd.microsoft.icon;base64,") {
braw = braw[37:]
}
var buffer bytes.Buffer
for i := 0; i < len(braw); i++ {
ch := braw[i]
buffer.WriteByte(ch)
if (i+1)%76 == 0 {
buffer.WriteByte('\n')
}
}
buffer.WriteByte('\n')
if Debug {
fmt.Print("---------------------------start base64 content--------------------------------\n")
fmt.Printf("====> base64 split 76:\n %s\n", buffer.String())
defer fmt.Print("---------------------------end base64 content--------------------------------\n")
}
return buffer.Bytes()
}