-
Notifications
You must be signed in to change notification settings - Fork 0
/
exploit.py
70 lines (57 loc) · 1.87 KB
/
exploit.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
from pwn import *
import argparse
import tempfile
import requests
def get_url():
parser = argparse.ArgumentParser()
parser.add_argument("url", help="The URL of the ELF file")
args = parser.parse_args()
return args.url
def fetch_file(url):
# fetch the file
response = requests.get(url)
if response.status_code == 200:
return response.content
else:
print("Failed to fetch the file!!!")
print(f"Status code: {response.status_code}")
exit(1)
def get_flag(url):
# create a temporary file-like object from the ELF data
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
elf_data = fetch_file(url)
tmp_file.write(elf_data)
tmp_file.flush()
tmp_file_path = tmp_file.name
# load the ELF file
elf = ELF(tmp_file_path)
# disassemble the main function
function_name = 'main'
if function_name in elf.symbols:
func_addr = elf.symbols[function_name]
print("-"*50)
print(f"Dissambly of the function '{function_name}':")
disassembly = elf.disasm(func_addr, 64)
print(disassembly)
print("-"*50)
for line in disassembly.split("\n"):
if 'mov' in line and 'eax' in line:
eax_value = [
val for val in line.split() if val.startswith('0x')]
if eax_value:
hex2dec = int(eax_value[0], 16)
flag = f"picoCTF{{{hex2dec}}}"
break
else:
print(f"Function '{function_name}' not found in symbols.")
# clean up the temporary file
if os.path.exists(tmp_file_path):
os.remove(tmp_file_path)
return flag
if __name__ == "__main__":
url = get_url()
flag = get_flag(url)
if flag:
print(f"Flag: {flag}")
else:
print("Flag not found!!!\nMake sure the URL is correct!!!")