/
numberbreak.py
140 lines (101 loc) · 3.65 KB
/
numberbreak.py
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
#!/usr/bin/python
# __author__ = 'joshg'
import urllib, urllib2, cookielib, os.path, sys, getopt
from bs4 import BeautifulSoup
def getcorrectnumbervalue(htmltext):
"Parses the HTML text and gets out the required number"
parsed_html = BeautifulSoup(htmltext,"html.parser")
print parsed_html.body.form.p
print 'Score: ' + parsed_html.body.div.p.text.split("Score:")[1]
numberstringminmax = parsed_html.body.form.p.text
numberstring = numberstringminmax.decode("utf-8").replace(u'The minimum number?',u'')
maxnum = True
if len(numberstring) != len(numberstringminmax.decode("utf-8")):
maxnum = False
else:
maxnum = True
numberstring = numberstringminmax.decode("utf-8").replace(u'The maximum number?',u'')
minval=1000000
maxval=-1000000
for currNum in numberstring.split(", "):
if int(currNum) > maxval: maxval = int(currNum)
if int(currNum) < minval: minval = int(currNum)
finalval = 0
if maxnum:
finalval=maxval
else:
finalval=minval
return finalval;
def getrequest( url ):
req = urllib2.Request(url)
rsp = urllib2.urlopen(req)
return rsp.read();
def postrequest( url, values ):
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
rsp = urllib2.urlopen(req)
return rsp.read();
def writetofile(filename, text):
f = open(filename, 'a')
f.write(text+'\n')
f.close()
def usage():
print 'WRONG FLAGS!!!!!!'
def processargs(argv):
try:
opts, args = getopt.getopt(argv, "vu:of:", ["verbose", "url=", "output", "file="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-u", "--url"):
print "urlString = "+ arg
global urlString
urlString = arg
elif opt in ("-o","--output"):
print "DoOutput = True"
global DoOutput
DoOutput = True
elif opt in ("-f","--file"):
global OutputFile
print "OutputFile = "+ arg
OutputFile = arg
elif opt in ("-v","--verbose"):
global VerboseMode
print "VerboseMode = True"
VerboseMode = True
return;
global VerboseMode
VerboseMode = False
global urlString
urlString = ""
global OutputFile
OutputFile= ""
global DoOutput
DoOutput = False
processargs(sys.argv[1:])
if urlString == "": urlString = 'http://s37109-102007-bxw.tarentum.hack.me/'
if (OutputFile == "") and (DoOutput): OutputFile = os.path.dirname(os.path.abspath(__file__)) + '\output.txt'
if VerboseMode: print "[+] Parameters assigned"
if os.path.isfile(OutputFile):
print 'FILE: "' + OutputFile + '" already exists!'
sys.exit(2)
if VerboseMode: print "[+] Output file checked"
cookie_jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
urllib2.install_opener(opener)
if VerboseMode: print "[+] Cookies configured"
# acquire cookie
rsp_html = getrequest(urlString+'number.php')
# print rsp_html
if VerboseMode: print "[+] Got first request"
while True:
if DoOutput: writetofile(OutputFile, rsp_html+"\n\n\n")
currNum = getcorrectnumbervalue(rsp_html)
if VerboseMode: print "[+] Got correct number to send ("+ str(currNum) + ")"
if DoOutput: writetofile(OutputFile, "Number sent: " + str(currNum) + "\n\n\n")
content = postrequest(urlString+'proc.php ', dict(number=currNum, submit='submit'))
if VerboseMode: print "[+] Sent number as POST request"
if DoOutput: writetofile(OutputFile, content+"\n\n\n")
rsp_html = getrequest(urlString+'number.php')
if VerboseMode: print "[+] Getting next GET request"