forked from GhostTroops/scan4all
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
276 lines (260 loc) · 7.07 KB
/
util.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
267
268
269
270
271
272
273
274
275
276
package pkg
import (
"crypto/tls"
"fmt"
"github.com/corpix/uarand"
"github.com/hbakhtiyor/strsim"
"io/ioutil"
"math/rand"
"net/http"
"net/http/cookiejar"
"net/url"
"reflect"
"sort"
"strings"
"time"
)
// fuzz 响应对象封装
type Response struct {
Status string
StatusCode int
Body string
Header *http.Header // 不用负责对象,引用,节约内存开销
ContentLength int
RequestUrl string
Location string
}
var (
HttpProxy string // 代理
CeyeApi string // Ceye api
CeyeDomain string // Ceye domain
Fuzzthreads = 32 // 2,4,8,16,32,采用2的N次方的数字
)
// http密码爆破
func HttpRequsetBasic(username string, password string, urlstring string, method string, postdata string, isredirect bool, headers map[string]string) (*Response, error) {
var tr *http.Transport
var err error
if HttpProxy != "" {
uri, _ := url.Parse(HttpProxy)
tr = &http.Transport{
MaxIdleConnsPerHost: -1,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
Proxy: http.ProxyURL(uri),
IdleConnTimeout: 15 * time.Second,
}
} else {
tr = &http.Transport{
MaxIdleConnsPerHost: -1,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
IdleConnTimeout: 15 * time.Second,
}
}
client := &http.Client{
Timeout: time.Duration(10) * time.Second,
Transport: tr,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}}
if isredirect {
jar, _ := cookiejar.New(nil)
client = &http.Client{
Timeout: time.Duration(10) * time.Second,
Transport: tr,
Jar: jar,
}
}
req, err := http.NewRequest(strings.ToUpper(method), urlstring, strings.NewReader(postdata))
if err != nil {
return nil, err
}
req.SetBasicAuth(username, password)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("User-Agent", uarand.GetRandom())
for v, k := range headers {
req.Header[v] = []string{k}
}
var resp *http.Response
// resp, err = tr.RoundTrip(req)
resp, err = client.Do(req)
if err != nil {
//防止空指针
return &Response{"999", 999, "", nil, 0, "", ""}, err
}
var location string
var reqbody string
defer resp.Body.Close()
if body, err := ioutil.ReadAll(resp.Body); err == nil {
reqbody = string(body)
}
if resplocation, err := resp.Location(); err == nil {
location = resplocation.String()
}
return &Response{resp.Status, resp.StatusCode, reqbody, &resp.Header, len(reqbody), resp.Request.URL.String(), location}, nil
}
// 需要考虑缓存
// 1、缓解网络不好的情况
// 2、缓存有效期为当天
// 3、缓存命中需和请求的数据完全匹配
func HttpRequset(urlstring string, method string, postdata string, isredirect bool, headers map[string]string) (*Response, error) {
var tr *http.Transport
if HttpProxy != "" {
uri, _ := url.Parse(HttpProxy)
tr = &http.Transport{
MaxIdleConnsPerHost: -1,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
Proxy: http.ProxyURL(uri),
}
} else {
tr = &http.Transport{
MaxIdleConnsPerHost: -1,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
}
}
client := &http.Client{
Timeout: time.Duration(10) * time.Second,
Transport: tr,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}}
if isredirect {
jar, _ := cookiejar.New(nil)
client = &http.Client{
Timeout: time.Duration(10) * time.Second,
Transport: tr,
Jar: jar,
}
}
req, err := http.NewRequest(strings.ToUpper(method), urlstring, strings.NewReader(postdata))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("User-Agent", uarand.GetRandom())
for v, k := range headers {
req.Header[v] = []string{k}
}
resp, err := client.Do(req)
if err != nil {
//防止空指针
return &Response{"999", 999, "", nil, 0, "", ""}, err
}
var location string
var reqbody string
defer resp.Body.Close()
if body, err := ioutil.ReadAll(resp.Body); err == nil {
reqbody = string(body)
}
if resplocation, err := resp.Location(); err == nil {
location = resplocation.String()
}
return &Response{resp.Status, resp.StatusCode, reqbody, &resp.Header, len(reqbody), resp.Request.URL.String(), location}, nil
}
func Dnslogchek(randomstr string) bool {
urlStr := fmt.Sprintf("http://api.ceye.io/v1/records?token=%s&type=dns&filter=%s", CeyeApi, randomstr)
if resp, err := HttpRequset(urlStr, "GET", "", false, nil); err == nil {
if !StrContains(resp.Body, `"data": []`) && strings.Contains(resp.Body, `{"code": 200, "message": "OK"}`) { // api返回结果不为空
return true
}
}
return false
}
func RandomStr() string {
lowercase := "1234567890abcdefghijklmnopqrstuvwxyz"
randSource := rand.New(rand.NewSource(time.Now().Unix()))
var (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
randBytes := make([]byte, 8)
for i, cache, remain := 8-1, randSource.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = randSource.Int63(), letterIdxMax
}
if idx := int(cache) & int(letterIdxMask); idx < len(lowercase) {
randBytes[i] = lowercase[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(randBytes)
}
// 判断 i 是否存在slice中
func SliceInAny[T any](i T, slice []T) bool {
for _, j := range slice {
if reflect.DeepEqual(i, j) {
return true
}
}
return false
}
// 判断 i 是否存在slice中
func IntInSlice(i int, slice []int) bool {
if slice == nil {
return false
}
sort.Ints(slice)
index := sort.SearchInts(slice, i)
if index < len(slice) && slice[index] == i {
return true
}
return false
}
// 判断 str 是否存在slice中
func StringInSlice(str string, slice []string) bool {
if slice == nil {
return false
}
sort.Strings(slice)
index := sort.SearchStrings(slice, str)
if index < len(slice) && slice[index] == str {
return true
}
return false
}
func SliceInString(str string, slice []string) bool {
if slice == nil {
return false
}
for _, v := range slice {
// 基于相似度计算
if 0.9 < strsim.Compare(str, v) {
return true
}
}
return false
}
var a1 = strings.Split("app,net,org,vip,cc,cn,co,io,com,gov.edu", ",")
// 兼容hacker one 域名表示方式,以下格式支持
// *.xxx.com
// *.xxx.xx1.*
func Convert2Domains(x string) []string {
aRst := []string{}
x = strings.TrimSpace(x)
if "*.*" == x || -1 < strings.Index(x, ".*.") {
return aRst
}
if -1 < strings.Index(x, "(*).") {
x = x[4:]
}
if -1 < strings.Index(x, "*.") {
x = x[2:]
}
if 2 > strings.Index(x, "*") {
x = x[1:]
}
if -1 < strings.Index(x, ".*") {
x = x[0 : len(x)-2]
for _, j := range a1 {
aRst = append(aRst, x+"."+j)
}
} else {
aRst = append(aRst, x)
}
return aRst
}