Skip to content

Instantly share code, notes, and snippets.

@offlinemark
Last active January 25, 2023 17:23
Show Gist options
  • Save offlinemark/5be8375ba28d5b7dd2d0 to your computer and use it in GitHub Desktop.
Save offlinemark/5be8375ba28d5b7dd2d0 to your computer and use it in GitHub Desktop.
Shellcode helper. Given a string, generate push instructions to push string onto stack.
#!/usr/bin/env python
'''
Tool for writing shellcode. Give it a string to push onto the stack and it
generates the corresponding push instructions.
'''
import sys
def print_word(word):
print 'push', '0x' + word[::-1].encode('hex')
if len(sys.argv) != 2:
print 'usage: <program> text'
sys.exit()
if len(sys.argv[1]) % 4 != 0:
print 'text must be multiple of 4. pad your slashes or something.'
sys.exit()
chunks = map(lambda x: sys.argv[1][x:x+4], range(0, len(sys.argv[1]), 4))[::-1]
map(print_word, chunks)
@cyberflashguru
Copy link

Updated the code to run on python 3.x
https://www.online-python.com/5DldoxN9hn

#!/usr/bin/env python
import sys
import binascii

'''
Tool for writing shellcode. Give it a string to push onto the stack and it
generates the corresponding push instructions.
'''

def print_word(word):
    print('push', '0x' + binascii.hexlify(word[::-1].encode()).decode())

if len(sys.argv) != 2:
    print('usage: <program> text')
    sys.exit()

if len(sys.argv[1]) % 4 != 0:
    print('text must be multiple of 4. pad your slashes or something.')
    sys.exit()

chunks = list(map(lambda x: sys.argv[1][x:x+4], range(0, len(sys.argv[1]), 4)))[::-1]
list(map(print_word, chunks))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment