From 6814897ce2de08180b6537fcccfb104827e2a131 Mon Sep 17 00:00:00 2001 From: n0p <0x90@n0p.cc> Date: Thu, 25 Oct 2018 23:14:29 +0200 Subject: Init. --- packer.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 packer.py (limited to 'packer.py') diff --git a/packer.py b/packer.py new file mode 100644 index 0000000..b74a5ac --- /dev/null +++ b/packer.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python2 + +import lief +import os +import subprocess + +subprocess.call(["clang", "-s", "-m32", "-nostdlib", "-nodefaultlibs", "-fPIC", "-Wl,-shared", "entry.c", "-oentry_tmp"]) +subprocess.call(["clang", "-s", "-m32", "-pie", "forgetful_commander.c", "-oforgetful_commander_tmp"]) + +entry = lief.parse("entry_tmp") +forgetful_commander = lief.parse("forgetful_commander_tmp") + +segment_added = forgetful_commander.add(entry.segments[1]) + +forgetful_commander.header.entrypoint = segment_added.physical_address + +# Encrypt exec segment +content = forgetful_commander.segments[3].content + +clr = [0, 0, 0, 0] +flux = [0x78, 0x75, 0x6c, 0x46] +for i in xrange(0, len(content), 4): + tmp = content[i:i+4] + content[i] ^= flux[0] ^ clr[0] + content[i+1] ^= flux[1] ^ clr[1] + content[i+2] ^= flux[2] ^ clr[2] + content[i+3] ^= flux[3] ^ clr[3] + flux.append(flux.pop(0)) + clr = tmp + +forgetful_commander.segments[3].content = content +forgetful_commander.segments[3].add(lief.ELF.SEGMENT_FLAGS.W) + +# Encrypt ro segment +content = forgetful_commander.segments[4].content + +clr = [0, 0, 0, 0] +flux = [0x78, 0x75, 0x6c, 0x46] +for i in xrange(0, len(content), 4): + tmp = content[i:i+4] + content[i] ^= flux[0] ^ clr[0] + content[i+1] ^= flux[1] ^ clr[1] + content[i+2] ^= flux[2] ^ clr[2] + content[i+3] ^= flux[3] ^ clr[3] + flux.append(flux.pop(0)) + clr = tmp + +forgetful_commander.segments[4].content = content +forgetful_commander.segments[4].add(lief.ELF.SEGMENT_FLAGS.W) + +forgetful_commander.write("public/forgetful_commander") + +os.remove("entry_tmp") +os.remove("forgetful_commander_tmp") -- cgit v1.2.3