-
Notifications
You must be signed in to change notification settings - Fork 72
/
class_WebRequest.ahk
277 lines (241 loc) · 5.96 KB
/
class_WebRequest.ahk
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
; Link:
; Author:
; Date:
; for: AHK_L
/*
*/
requests := object()
loop, 643
{
tooltip, % "sending request " A_Index - 3
request := new WebRequest("http://natas19.natas.labs.overthewire.org/index.php")
;~ request.addGet("debug", "true")
request.setCredentials("natas19", "4IwIrekcuZlA9OsjOkoUtwU6lhokCPYs")
;~ request.addPost("username", "admin")
;~ request.addPost("password", "password")
request.setTimeout(20000)
request.addCookie("PHPSESSID", AscToHex(A_Index - 3 "-admin"))
request.goGet()
.addMisc(A_Index - 3)
requests[A_Index] := request
}
waitForAllResponses(requests)
strings := object()
tooltip, Sorting responses
for index, r in requests
{
;go through every response
if(strings.hasKey(r.getResponse())) {
strings[r.getResponse()] .= ", " r.getMisc()
} else {
strings[r.getResponse()] := r.getMisc()
}
}
for resp, values in strings
{
FileAppend, % values "`n" resp, % A_index ".txt"
}
ExitApp
AscToHex(str) {
if(str)
{
str := Chr((Asc(str)>>4)+48) Chr((x:=Asc(str)&15)+(x>9 ? 55:48)) AscToHex(SubStr(str,2))
}
StringLower, str, str
return str
}
waitForAllResponses(respArr) {
for index, r in respArr
{
while true
{
tooltip, % "waiting for " index " to respond"
if(r.isReady())
{
break
}
sleep 50
}
}
return
}
#c::
ExitApp
class WebRequest {
HTTPREQUEST_SETCREDENTIALS_FOR_SERVER := 0
HTTPREQUEST_SETCREDENTIALS_FOR_PROXY := 1
url := ""
getVars := Object()
postVars := Object()
cookies := Object()
header := Object()
requestObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
response := ""
username := ""
password := ""
throttle := true
serverOrProxy := ""
/* Assumes a valid url
*/
__New(url) {
this.url := url
return this
}
addMisc(item) {
this.item := item
}
getMisc() {
return this.item
}
setThrottle(bool) {
this.throttle := bool
return this
}
removeHeader(key) {
this.headers.Remove(key)
return this
}
removePost(key) {
this.postVars.Remove(key)
return this
}
isReady() {
return this.requestObj.waitForResponse(1)
}
/*
* Ensure isReady before calling this
*/
getResponse() {
return this.requestObj.responseText
}
makeUri() {
safeUri := this.url
for key, val in % this.getVars {
safeUri .= "?" this.UriEncode(key) "=" this.UriEncode(val)
}
;~ MsgBox, % "Uri is " safeUri
return safeUri
}
makeData() {
data := ""
for key, val in this.postVars {
data .= this.UriEncode(key) "=" this.UriEncode(val) "&"
}
StringTrimRight, data, data, 1
;~ msgbox, % "data is " data
return data
}
goGet() {
this.requestObj.Open("GET", this.makeUri(), this.throttle)
this.__setHeaders()
this.requestObj.Send()
return this
}
__makeCookies() {
str := ""
for key, val in this.cookies {
str .= this.UriEncode(key) "=" this.UriEncode(val) "&"
}
StringTrimRight, str, str, 1
return str
}
/*
* called after open and before send
*/
__setHeaders(type := "") {
if(this.timeout) {
this.requestObj.setTimeouts(0, 30000, 30000, this.timeout)
}
if(this.username) {
this.Base64encUTF8(encoded, this.username ":" this.password)
this.requestObj.setRequestHeader("Authorization", "Basic " encoded)
}
if(this.__MakeCookies()) {
this.requestObj.setRequestHeader("Cookie", this.__makeCookies())
}
if(type == "POST") {
this.requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
}
for key, val in this.headers
{
this.requestObj.setRequestHeader(key, value)
}
this.requestObj.option(4) := 13056 ; WinHttpRequestOption_SslErrorIgnoreFlags 13056: ignore all err, 0: accept no err
;~ this.requestObj.setRequestHeader("User-Agent", "Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.5.0")
;~ this.requestObj.setRequestHeader("Accept"
;~ , "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
}
goPost() {
this.requestObj.Open("POST", this.makeUri(), this.throttle)
this.__setHeaders("POST")
;~ MsgBox % this.makeUri()
this.requestObj.Send(this.makeData())
return this
}
addCookie(key, value) {
this.cookies[key] := value
return
}
setTimeout(miliseconds) {
this.timeout := miliseconds
return this
}
addGet(key, value) {
this.getVars[key] := value
return this
}
addPost(key, value) {
this.postVars[key] := value
return this
}
addHeader(key, value) {
this.header[key] := value
return this
}
/* serverOrProxy is one of the HTTPREQUEST_CREDENTIALS at the top of the class
*/
setCredentials(username, pass, serverOrProxy := 0) {
this.userName := username
this.password := pass
this.ServerOrProxy := serverOrProxy
return this
}
UriEncode(Uri, Enc := "UTF-8") {
StringReplace, Uri, Uri, %A_Space%, +, ALL
;~ MsgBox % Uri
; modified from jackieku's code (http://www.autohotkey.com/forum/post-310959.html#310959)
this.StrPutVar(Uri, Var, Enc)
f := A_FormatInteger
SetFormat, IntegerFast, H
Loop, % StrLen(Uri)
{
Code := NumGet(Var, A_Index - 1, "UChar")
If (chr(Code) == "+")
{
;~ this is the space character
Res .= "+"
}
Else If (Code >= 0x30 && Code <= 0x39 ; 0-9
|| Code >= 0x41 && Code <= 0x5A ; A-Z
|| Code >= 0x61 && Code <= 0x7A) ; a-z
Res .= Chr(Code)
Else
Res .= "%" . SubStr(Code + 0x100, -1)
}
SetFormat, IntegerFast, %f%
Return, Res
}
Base64encUTF8( ByRef OutData, ByRef InData )
{ ; by SKAN + my modifications to encode to UTF-8
InDataLen := this.StrPutVar(InData, InData, "UTF-8") - 1
DllCall( "Crypt32.dll\CryptBinaryToStringW", UInt,&InData, UInt,InDataLen, UInt,1, UInt,0, UIntP,TChars, "CDECL Int" )
VarSetCapacity( OutData, Req := TChars * ( A_IsUnicode ? 2 : 1 ), 0 )
DllCall( "Crypt32.dll\CryptBinaryToStringW", UInt,&InData, UInt,InDataLen, UInt,1, Str,OutData, UIntP,Req, "CDECL Int" )
Return TChars
}
StrPutVar(Str, ByRef Var, Enc = "") {
Len := StrPut(Str, Enc) * (Enc = "UTF-16" || Enc = "CP1200" ? 2 : 1)
VarSetCapacity(Var, Len, 0)
Return, StrPut(Str, &Var, Enc)
}
}