Skip to content

Instantly share code, notes, and snippets.

@kurtbrose
Created June 18, 2012 05:53
Show Gist options
  • Save kurtbrose/2947061 to your computer and use it in GitHub Desktop.
Save kurtbrose/2947061 to your computer and use it in GitHub Desktop.
exec_secure
import struct
import hashlib
import zlib
LONG = struct.Struct('>L')
def exec_secure(secure_file):
with open(secure_file, "rb") as f:
sec_data = f.read()
sec_data = zlib.decompress(sec_data)
header = LONG.unpack_from(sec_data)
sec_data = sec_data[LONG.size:]
signature = sec_data[:header[0]]
sec_data = sec_data[header[0]:]
sec_data = "".join([chr(ord(a)^0x5A) for a in sec_data])
if hashlib.sha1(sec_data + "0xDEADBEEF").digest() != signature:
raise Exception("invalid signature for data in file "+secure_file)
else:
exec(sec_data)
def lock_data(plain_file, encrypted_file):
with open(plain_file) as f:
plain_data = f.read()
signature = hashlib.sha1(plain_data + "0xDEADBEEF").digest()
data = "".join([chr(ord(a)^0x5A) for a in plain_data])
sec_data = LONG.pack(len(signature)) + signature + data
with open(encrypted_file, "wb") as ef:
ef.write(zlib.compress(sec_data))
print "HELLLOOO"
'''
example usage session:
>>> import exec_secure
HELLLOOO
>>> exec_secure.lock_data('exec_secure.py', 'exec_secure.epy')
>>> exec_secure.exec_secure('exec_secure.epy')
HELLLOOO
>>> open('exec_secure.epy').read()
"x\x9c\x8dT\xcd\x8e\xd30\x10n%\xaah\xafh\x1b!\x156\xab\xec\xc5\x8e\xa8\x0bq\x92n
\xe44s\x80+\x92O\\\xb8\xe4P\xb1R9@\xedh\x15\x19\xe5%x\x05^\x80\x07\xe01x\x06\xce
{_\ta\xa7\xf9q\xbbY\x84/\xfe\x99o\xc6\x9fg\xbe\xf1d29\x7f\xf3\xe5\xd7\xfb?g\x8bo
\xef~\xde\x7f\xfc\xf1\xfd\xee7{\xab>\xd0u\x10#\xa20A\xab\x94\xf0v\x172\x1c&\xf4\
xba\xdb^\x9a5w\xe7\xe7/\xd4M\x8b,\xcf\x9aI\xd4[\xb7\x96\x9c\xe7\x90)\xf0!\x9daHW
\x08\xc4a\x9ae4\x01Yp\xa5\xc7\x92\x92P\xc5\x01D\xb6\xedVU\xe8\xba\x92\x8aa\x95\x
1d`fh\xc0,g\x84\xe9\xcb\xb2\x12\x01\xcb\x85\x1c\xb3\x19Ve\x0ei\xbc\x0e\x10`,:\xd
3\x00\x0e\xb5/ \r5\xd4\xcbU\x14\xb0\xf4\xf5,C\xf1z\x04k\x05\xee\x96\xd3\xc6\r\xd
3K(\x9c\x01G7\x11#\xfa\x016\xb08\xdc4\xdd9\xce?\x03\xf6\xb0b\x14WU\xe5\xab\x98Fb
\x9a\x86H\xc4(\x17L>\xd9\xf9\x9f\x17Re1RL\xd1\xa8G;\x03s\x9au\xf5*q\xc8>\xf5oS{U
\xed\xfc\x0boq\xf1\xcc\xf3\x9eW\xb2\xcc\xe9\x060\x11R}\xbd\x19\x9e1\xa4\xdd\x0c\
xc4(\x06\xe5\xf9)\x04\x84\xc6\x91\xa8h\xf4\x92%4\xb7\x9em\xa84\xe15\x1bSCU\xed\x
edj\xf7\xe1 \xc1'\xc1\x8d@\xac\xcc7\xa2Ib]\x12\xb3\x17A\xc2h\xd4\xaa\x02\xa2\x14
]\x05\x04\xf2q\x05\r\xd0S\xe9\x1c,\x8f\x8a\xc7\xaa\xddQ\xca,\xb7\xc7\x92\xd6\x07
\xf9\xffZ\rQ\x9dQ\x9d5\xf22\x9a\x14\x89i\x8b\x8e\x9c\x94\x9a\xc4@u\xdf\xdc\xd8\x
fb\x0fY8N\x92n\xa5e\xdbJ\x90\x9d\xe4=+\x97\x88\x12\x10M\xc7<\xec\x17]\n\x1e "
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment