forked from Th4nat0s/Chall_Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathropval.py
executable file
·124 lines (102 loc) · 2.68 KB
/
ropval.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
#!/usr/bin/python
# v 0.1
# Copyleft Thanat0s
# http://Thanat0s.trollprod.org
#
# Licence GNU GPL
import sys
from subprocess import call
import os
import re
def find_first(number):
for items in result:
if items[0] <= number:
return (items)
if len(sys.argv) < 2:
print' Will find all static numeric values'
print 'very usefull for a rop add eax,[ebx-xxxxxx]'
print ' ex : ropval.py mybinary 0xb8a0008 | sort -n'
sys.exit()
file = open(sys.argv[1], 'rb')
filename = sys.argv[1]
byteArr = bytearray(file.read())
file.close()
filesize = len(byteArr)
print "- Loaded " + str(filesize) + " Bytes"
sys.stdout.flush()
print "- Elfread "
sys.stdout.flush()
if len(sys.argv) >= 3:
offset=int(sys.argv[2],16)
else:
offset=0
# Find all mapped section in elf (thank's to readelf)
elf=[]
import subprocess
cmd = subprocess.Popen('readelf -S '+sys.argv[1], shell=True, stdout=subprocess.PIPE)
for line in cmd.stdout:
match = re.match(r'.* (AX|A) ', line)
if match:
# print line.rstrip()
regex = re.search(r']\s\S+\s+\S+\s+([0-9a-f]{8})\s([0-9a-f]{6})\s([0-9a-f]{6})',line)
if regex:
# Stock OFFSET, LEN, MAPPING
elf.append([int(regex.group(2),16),int(regex.group(3),16),int(regex.group(1),16)])
print "- Finding values"
sys.stdout.flush()
result= []
# Fetch all possible values from elf sections
for section in elf:
j = 0
for i in range(section[0],section[0]+section[1]-4):
potential = byteArr[i+0] +( byteArr[i+1] << 8) + (byteArr[i+2] << 16 )+ (byteArr[i+3] << 24)
found = False
for items in result:
if int(potential) == items[0]:
found = True
break
if not found:
result.append ( [int(potential), section[2]+j ])
j = j+1
result.sort()
if len(sys.argv) == 5:
print "Find a way from "+ sys.argv[3] + " to " + sys.argv[4],
SRC = int(sys.argv[3],16)
DST = int(sys.argv[4],16)
if DST > SRC:
GAP = DST-SRC
else:
GAP = 0xFFFFFFFF - SRC + DST + 1
print "Gap is " + hex(GAP)
result.reverse()
SOLUTION= []
print "solution :"+ str(GAP)+" sub(",
while GAP <> 0:
# hopefully 1 is alway present
CANDIDATE = find_first(GAP)
SOLUTION.append (CANDIDATE)
GAP = GAP - CANDIDATE[0]
for items in SOLUTION:
print str(items[0]),
print ")"
print "memory location :",
for items in SOLUTION:
print '%08X' % int(items[1]) + ",",
print ""
print "memory offset (" + str(offset) + ") :",
for items in SOLUTION:
print '"%08X"' % int(int(items[1]+offset) % 0xffffffff) + "," ,
print ""
else:
print "Decval","Hexval","mOffset",
if offset<>0:
print "sOffest"
else:
print ""
for items in result:
print str(items[0]),
print '%08X' % items[0] ,
print '%08X' % int(items[1]),
if offset<>0:
print '%08X' % int(int(items[1]+offset) % 0xffffffff),
print ""