This script can be useful if you want to obfuscate your django project sources. I use the pyobfuscate tool, a little bit patched, the patch you can get here. The patch switchs off the classes and methods obfuscation as they could be imported from other files. The pyobfuscate util can obfuscate only one file at once. This script can obfuscate all .py files from the specified path (src_dir), at the end of the obfuscation it compiles them using compileall module. Set the src_dir and the dst_dir variables. Note: remove original settings.py and urls.py from the src_dir cause pyobfuscate util raises an error trying to encode them. The decorators are not handled correctly by pyobfuscate tool. I'm using django for about year and i didn't use the decorators except the '@transaction' decorators. I replace them from the sources at the beginning of the obfuscation and put them at their places in encoded file. In case of using other decorators you should make some changes in code ( line # 64 ).
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | #!/usr/bin/python
#
# This tool is used for obfuscate the django project sources
#
import sys
import os
import os.path
import tempfile
import re
import compileall
# Source files path
src_dir = '/path/to/sources'
# Path to save obfuscated sources
dst_dir = '/path/to/results'
###################################
def process_dir(dir):
# recursive function
# processing the files, subdirs, create the directory in dst_dir path
print 'Processing directory %s' % dir
d_dir = dst_dir + dir[len(src_dir):]
if 1 != os.path.isdir(d_dir):
try:
os.mkdir(d_dir)
except:
print "\nCould not create the dir: " + d_dir
sys.exit(1)
for f in get_py_files(dir):
process_file(dir + '/' + f)
for d in get_sub_dirs(dir):
process_dir(dir + '/' + d)
def process_file(f):
# 1. parsing the python source file specified(f), getting the '@transaction' decorators and save them in dictionary (mapped by def names)
# 2. obfuscate the code
# 3. set the '@transaction' decorators at their places
# 4. save the results in dst_dir path
print 'Processing file %s' % f
fd = open(f,'r')
ln = fd.readlines()
fd.close()
ln_tmp = []
trans_defs = {}
for i in range(0,len(ln)):
m = re.search('^\s*#', ln[i])
if m != None:
continue
ln[i] = re.sub("\r?", '', ln[i])
if ln[i][0:12] == '@transaction':
if ln[i+1][0:3] != 'def':
print '\nError at line %i: transaction directive without DEF' % i
sys.exit(1)
else:
m = re.search('def ([\w\d\_]+)', ln[i+1])
if m == None or m.group(1) == None:
print '\nError at line %i: transaction directive without DEF' % i
sys.exit(1)
else:
trans_defs[m.group(1)] = re.sub("\n$", '', ln[i])
else:
ln_tmp.append(ln[i])
tmp_fd, tmp_name = tempfile.mkstemp()
os.write(tmp_fd, ''.join(ln_tmp))
obfuscated_code = set_trans_defs(get_obfuscated_data(tmp_name), trans_defs)
d_file = dst_dir + f[len(src_dir):]
fd = open(dst_dir + f[len(src_dir):], 'w')
fd.write(obfuscated_code)
fd.close()
os.close(tmp_fd)
def get_py_files(dir):
# get all python source files from the path specified(dir)
return filter( lambda x: (1 == os.path.isfile(dir + '/' +x)) and (x[len(x)-3:] == '.py'), os.listdir(dir))
def get_sub_dirs(dir):
# get sub dirs from the path specified(dir)
# dir - path
return filter( lambda x: 1 == os.path.isdir(src_dir + '/' +x), os.listdir(dir))
def get_obfuscated_data(f):
# create the pipe with PYOBFUSCATE util and read its output
# f - path to python source file
return os.popen('pyobfuscate ' + f).read()
def set_trans_defs(data,trans_defs):
# put the '@transaction..' decorators at their places (according to methods names)
ln_new = []
for ln in data.split("\n"):
if ln[0:3] == 'def':
m = re.search('def ([\w\d\_]+)', ln)
if m.group(1) in trans_defs:
ln_new.append(trans_defs[m.group(1)])
ln_new.append(ln)
return "\n".join(ln_new)
if __name__ == '__main__':
# starting process from the source root dir
process_dir(src_dir)
# compile all sources
compileall.compile_dir(dst_dir)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
the scripts can obfuscate django project? I do it , but the django project can't run. did you do it ? and how to download the patch? the url you offer don't work
#
Please login first before commenting.