-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_to_asm_push.py
More file actions
executable file
·77 lines (65 loc) · 2.65 KB
/
Copy pathstring_to_asm_push.py
File metadata and controls
executable file
·77 lines (65 loc) · 2.65 KB
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
#!/usr/bin/python
"""
Convert an arbitrary string into asm push instructions
Copyright (C) 2010 st0w <st0w@st0w.com>
This is released under the MIT License... Do with this whatever the
hell you want, I don't care.
I started this in perl, but really, python is much better. I know what
my variable types are, why do you keep assuming you know better than
me, perl?
"""
# ---*< Standard imports >*---------------------------------------------------
import sys
# ---*< Initialization >*-----------------------------------------------------
def scrusage():
sys.stderr.write("Usage %s 'string to convert to ASM push instructions.' "
"string2\n (Separate strings with spaces, contain single "
"strings with whitespace in quotes)\n" % sys.argv[0])
sys.exit(1)
if len(sys.argv) < 2:
scrusage()
# ---*< Code >*---------------------------------------------------------------
print "BITS 32"
print "; Push string \"" + sys.argv[1] + "\" on stack\n"
print "xor eax,eax\t; Clear EAX"
print "push eax\t; Place NULL terminator on stack\n"
strh = ''
strc = ''
strco = ''
print "; Little-Endian notation"
for i, v in enumerate(reversed(sys.argv[1])):
strh += "%02x" % ord(v)
if v == '\n':
strc += "\\n"
strco = "\\n" + strco
else:
strc += v
strco = v + strco
if (i == (len(sys.argv[1]) - 1)) and ((i % 4) != 3) :
"""If we're at the last byte, which is NOT a four byte boundary"""
while len(strc) < 4:
strc += '*' # Pad with '*' chars to reach word boundary
strco = '*' + strco
strh += "%02x" % ord('*')
if (i % 4) == 3 or (i == (len(sys.argv[1]) - 1)) :
"""If we're at the fourth byte, or the last byte which is also a four byte
boundary
"""
print "push 0x" + strh + "\t; " + strc + ' == ' + strco
strh = ''
strc = ''
strco = ''
print
"""
Now also take into account that if the last word pushed was not four
even bytes that ESP does NOT refer to the start of the string.
Instead, it refers to null bytes. Which would somewhat defeat the
purpose of pushing a string onto the stack. So if that's the case,
adjust ESP accordingly before copying the value into ECX.
"""
sys.stdout.write("mov ecx,esp\t; ECX now contains the address of the "
"string. Have a nice day. :)\n")
if len(sys.argv[1]) % 4:
sys.stdout.write("add cl,%s\t; Adjust ECX to point to the start of the "
"string, past leading '*' pads @ ESP\n" %
str(4 - len(sys.argv[1]) % 4))