/
setregs.asm
72 lines (63 loc) · 1.65 KB
/
setregs.asm
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
<%
from pwnlib.regsort import regsort
from pwnlib.shellcraft import registers, eval
from pwnlib.shellcraft.amd64 import mov
%>
<%page args="reg_context, stack_allowed = True"/>
<%docstring>
Sets multiple registers, taking any register dependencies into account
(i.e., given eax=1,ebx=eax, set ebx first).
Args:
reg_context (dict): Desired register context
stack_allowed (bool): Can the stack be used?
Example:
>>> print(shellcraft.setregs({'rax':1, 'rbx':'rax'}).rstrip())
mov rbx, rax
push 1
pop rax
>>> print(shellcraft.setregs({'rax': 'SYS_write', 'rbx':'rax'}).rstrip())
mov rbx, rax
push SYS_write /* 1 */
pop rax
>>> print(shellcraft.setregs({'rax':'rbx', 'rbx':'rax', 'rcx':'rbx'}).rstrip())
mov rcx, rbx
xchg rax, rbx
>>> print(shellcraft.setregs({'rax':1, 'rdx':0}).rstrip())
push 1
pop rax
cdq /* rdx=0 */
</%docstring>
<%
reg_context = {k:v for k,v in reg_context.items() if v is not None}
eax = reg_context.get('rax', None)
edx = reg_context.get('rdx', None)
cdq = False
if isinstance(eax, str):
try:
eax = eval(eax)
except NameError:
pass
if isinstance(edx, str):
try:
edx = eval(edx)
except NameError:
pass
if isinstance(eax, int) and isinstance(edx, int) and eax >> 63 == edx:
cdq = True
reg_context.pop('rdx')
sorted_regs = regsort(reg_context, registers.amd64)
%>
% if not sorted_regs:
/* setregs noop */
% else:
% for how, src, dst in sorted_regs:
% if how == 'xchg':
xchg ${src}, ${dst}
% else:
${mov(src, dst)}
% endif
% endfor
% if cdq:
cdq /* rdx=0 */
% endif
% endif