-
Notifications
You must be signed in to change notification settings - Fork 0
/
exploit_gadget.py
149 lines (116 loc) · 4.46 KB
/
exploit_gadget.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
141
142
143
144
145
146
147
148
from typing import List
from abc import ABCMeta, abstractmethod
from ctypes import *
from z3 import *
from random import choice
from gadget_template import *
ALLOWED_BIT = [8, 16, 32]
class exploit_gadget:
__metaclass__ = ABCMeta
def __init__(self):
pass
def generate(self, constraint):
pass
'''
Greater than
'''
class Constraint_GT(exploit_gadget):
def overflow(self, constraint):
constraint = simplify(constraint)
var = constraint.arg(1)
cons_value = constraint.arg(0)
nbit = var.size()
if nbit not in ALLOWED_BIT:
return
template = gadget_template["flag_start"] + \
gadget_template["overflow_"+str(nbit)] + \
gadget_template["flag_end"]
print(constraint, cons_value)
tmp = cons_value.as_long() + 1
if tmp > (1<<(nbit-1)):
tmp = tmp - (1<<(nbit))
template += gadget_template["pos_var_filter"]
else:
template += gadget_template["neg_var_filter"]
cons_value = (1<<(nbit-1)) - tmp
codes = template.format(tmp1_name = self.tmp_prefix+"_"+str(self.cnt),
var_name = str(var),
cons_value = str(cons_value))
self.cnt += 1
return codes
def __init__(self):
self.gadgets = [self.overflow]
self.tmp_prefix="GT"
self.cnt = 0
def generate(self, constraint):
constraint = simplify(constraint)
if not isinstance(constraint, BoolRef):
return
if str(constraint.decl()) == "Not":
return
#constraint = simplify(constraint.arg(0).arg(0) <= constraint.arg(0).arg(1) +1)
elif str(constraint.decl()) == "<=":
constraint = simplify(constraint.arg(0)-1 <= constraint.arg(1))
gadget = choice(self.gadgets)
codes = gadget(constraint)
return codes
'''
Less than
'''
class Constraint_LT(exploit_gadget):
def underflow(self, constraint):
constraint = simplify(constraint)
var = constraint.arg(0)
cons_value = constraint.arg(1)
nbit = var.size()
if nbit not in ALLOWED_BIT:
return
template = gadget_template["flag_start"] + \
gadget_template["underflow_"+str(nbit)] + \
gadget_template["flag_end"]
tmp = cons_value.as_long()
if tmp > (1<<(nbit-1)):
tmp = tmp - (1<<(nbit))
template += gadget_template["neg_var_filter"]
else:
template += gadget_template["pos_var_filter"]
cons_value = (1<<(nbit-1)) + tmp
codes = template.format(tmp1_name = self.tmp_prefix+"_"+str(self.cnt),
var_name = str(var),
cons_value = str(cons_value))
self.cnt += 1
return codes
def __init__(self):
self.gadgets = [self.underflow]
self.tmp_prefix="LT"
self.cnt = 0
def generate(self, constraint):
constraint = simplify(constraint)
if not isinstance(constraint, BoolRef):
return
if str(constraint.decl()) == "Not":
return
#constraint = simplify(constraint.arg(0).arg(0) >= (constraint.arg(0).arg(1)))
elif str(constraint.decl()) == "<=":
constraint = simplify(constraint.arg(0) <= constraint.arg(1) + 1)
gadget = choice(self.gadgets)
codes = gadget(constraint)
return codes
'''
['__bool__', '__class__', '__copy__', '__deepcopy__', '__del__', '__delattr__', '__dict__',
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
'__init_subclass__', '__le__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__nonzero__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__',
'_repr_html_', 'arg', 'as_ast', 'children', 'ctx_ref', 'decl', 'eq', 'from_string', 'get_id', 'hash', 'num_args',
'params', 'serialize', 'sexpr', 'sort', 'sort_kind', 'translate', 'use_pp']
'''
if __name__ == "__main__":
gt = Constraint_GT()
lt = Constraint_LT()
x = BitVec("x", 8)
#y = BitVec("y", 16)
cons1 = 56<=x
#cons2 = 1000>=y
#k = x<=-98
print(gt.generate(cons1))
#print(lt.generate(cons2))