Skip to content

Instantly share code, notes, and snippets.

@phracker
Created August 13, 2014 08:14
Show Gist options
  • Save phracker/fd56aa7ccd01460d68ea to your computer and use it in GitHub Desktop.
Save phracker/fd56aa7ccd01460d68ea to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Usage: encodejs.py <fileToEncode>
#
# Encodes a js file using percent encoding.
# Resulting file calls unescape() and eval() to decode and execute the encoded string.
#
# Discovered while trying to get anonym.to's anonymize.js (http://js.anonym.to/anonym/anonymize.js)
# to anonymize https links as well as http. Thought it was a goofy and unnecessary
# obfuscation strategy. So I wrote up an encoder.
# Modified anonymize.js script at https://gist.github.com/phracker/b952b618e19a01a623de
import sys,os
es = "eval(unescape('"
def usage():
print "Usage: encodejs.py <fileToEncode>"
args = sys.argv
del args[0]
if(len(args) != 1):
usage()
sys.exit()
fPath = os.path.expanduser(args[0])
fPathName, fPathExt = os.path.splitext(fPath)
if(fPathExt != ".js"):
print "Not a .js file. Aborting."
usage()
sys.exit()
o = open(fPathName + ".enc" + fPathExt,"w")
with open(fPath) as f:
while True:
c = f.read(1)
if not c:
es = es + "'));"
o.write(es)
o.close()
f.close()
print "Success! File written to: " + fPathName + ".enc" + fPathExt
break
ec = "%" + hex(ord(c)).split('x')[1]
es = es + ec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment