Reputation: 159
I am beginning to solve programming CTFs in Python when I came accross a challenge for automating math calculations to get the flag.
The source code of the welcoming page looks like this:
<html>
<head>
<meta charset="utf-8">
<title>FastMath</title>
</head>
<body>
<div id="content" style="text-align: center;">
Can you solve the level 1?<br/><h3><div id='calc'>1 + 1</div></h3><br/>
<form action="play.php" method="post">
<input type="text" name="res" />
<input type="submit" value="OK">
</form>
</div>
</body>
I wrote my Python code to automate it in order to get the flag as follows
#!/usr/bin/env python
import requests
import re
encoding = 'utf-8'
url = '0.0.0.0:8091'
session = requests.Session()
response = session.get(url)
content = response.text
while True:
try:
#solves = re.findall('Can you solve the level (.*) ?', content)[0]
number = re.findall('(?:[0-9 ()]+[*+/-])+[0-9 ()]+', content)[0]
post_url = url + '/play.php'
solution = eval(number)
post_data = { "res" : solution }
response = session.post(post_url, data = post_data)
content = response.content
#debug
print(content)
#print('number of solves: ' + solves)
except:
flag = re.findall('FLAG{(.*)}', content)[0]
print(flag)
break
It manages to make the first calculation as I get Can you solve the level 2? and submits as I can see from the output but then it gives out errors:
b'<html>\n\n<head>\n\n\t<meta charset="utf-8">\n\t<title>FastMath</title>\n\n</head>\n<body>\n\n\t<div id="content" style="text-align: center;">\n\t\tCan you solve the level 2?<br/><h3><div id=\'calc\'>1 - 5</div></h3><br/>\t\t<form action="#" method="post">\n\t\t\t<input type="text" name="res" /></p>\n\t\t\t<p><input type="submit" value="OK"></p>\n\t\t</form>\n\t</div>\n</body>\n\n'
Traceback (most recent call last):
File "mmm.py", line 14, in <module>
number = re.findall('(?:[0-9 ()]+[*+/-])+[0-9 ()]+', content)[0]
File "/usr/lib/python3.6/re.py", line 222, in findall
return _compile(pattern, flags).findall(string)
TypeError: cannot use a string pattern on a bytes-like object
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "mmm.py", line 27, in <module>
flag = re.findall('LMFRCTF{(.*)}', content)[0]
File "/usr/lib/python3.6/re.py", line 222, in findall
return _compile(pattern, flags).findall(string)
TypeError: cannot use a string pattern on a bytes-like object
I tried making in the converting it all to string but it didn't work, even using r"something"
or b"something"
didn't help much!
Upvotes: 0
Views: 632
Reputation: 2776
response.text
will give you a str
, not byte
s but response.content
will give you byte
s.
Choose the type you want to use and use it consistently.
re
will handle bytes if the regular expression is byte
s as well.
Upvotes: 2