Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!lightspeed.eweka.nl!lightspeed.eweka.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'resulting': 0.04; 'tree': 0.05; 'subject:Python': 0.06; '"__main__":': 0.09; '__name__': 0.09; 'formatting': 0.09; 'pep': 0.09; 'sys,': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; '"w")': 0.16; 'ast': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happy.': 0.16; 'interest,': 0.16; 'knock': 0.16; 'line).': 0.16; 'ought': 0.16; 'wrote:': 0.18; 'skip:p 40': 0.19; 'unlike': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'together.': 0.24; 'cc:2**0': 0.24; 'least': 0.26; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; 'skip:p 30': 0.29; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'that.': 0.31; 'usually': 0.31; 'comments,': 0.31; 'names.': 0.31; 'sep': 0.31; 'subject:end': 0.31; 'subject:users': 0.31; "they'll": 0.31; 'file': 0.32; 'this.': 0.32; 'probably': 0.32; 'figure': 0.32; 'run': 0.32; 'there,': 0.34; 'subject:from': 0.34; 'could': 0.34; "can't": 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'ones,': 0.36; "i'll": 0.36; 'possible': 0.36; 'should': 0.36; 'easily': 0.37; 'whatever': 0.38; 'files': 0.38; 'pm,': 0.38; '(from': 0.39; 'even': 0.60; 'skip:o 30': 0.61; 'information': 0.63; 'note:': 0.66; 'benefit': 0.68; 'fact,': 0.69; 'actually,': 0.84; 'local,': 0.84; 'subject:source': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=cGjjZlB1nRYQMRAcHHE3h5ZH43VIpzUDHmOf6qGgwoA=; b=CDBuV1fKXC/04/vXbAgbyhZ9waBai82/iqCyrvcc7/f8yUT6ZhDsOHBaoYqytv14aN /GLMrCU7pVaAILzsKrHyg0jLOKzVgRUIraPxQYMlLEoCXioSmUVsauTYwJpb3MHwygO3 5OIHRyMIvq2bevtM09/ndSFZ4u1oVl3JWzMlNOmcQO47nypIJPoqq/OJMVWTKn0kRkd4 Q/xvMT5Boco2h1p0qRrXo+Sq8MCMgF1VFo0N/4toL+jWRAFJU5GM9yh1h58lyFOBj8Fp w5Wor/i1slVeXA95k2+K0w3c7+7uLyTJB2Hm2yLuWSaUfjrHj3MWEUIsy7tSx16reTgY kh9w== MIME-Version: 1.0 X-Received: by 10.152.7.73 with SMTP id h9mr6545217laa.27.1411985648463; Mon, 29 Sep 2014 03:14:08 -0700 (PDT) In-Reply-To: References: <89381545-5edc-4f85-a4b8-86eebfdf34ee@googlegroups.com> <9ad69bd8-ca77-438f-adfe-87d727ca170c@googlegroups.com> Date: Mon, 29 Sep 2014 20:14:08 +1000 Subject: Re: Obscuring Python source from end users From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1411985650 news.xs4all.nl 2933 [2001:888:2000:d::a6]:45219 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5782 X-Received-Body-CRC: 2206807549 Xref: csiph.com comp.lang.python:78456 On Mon, Sep 29, 2014 at 6:55 PM, Chris Angelico wrote: > It ought to be possible to do an AST reconstitution for at least part > of this. I can hunt down some of my PEP 463 test code to help out with > that. It should be possible to figure out what names are local, and > then just use those. > > If this is of interest, I'll see what I can knock together. Actually, why reconstitute? The AST can be compiled. Here's a Python cruncher/obscurer: import ast, sys, os # Note: If the file can't be parsed, this will raise SyntaxError # (from the ast.parse() line). def crunch(fn, outfn): with open(fn, "rb") as f: data = f.read() tree = ast.parse(data) os.makedirs(os.path.split(outfn)[0], exist_ok=True) with open(outfn, "w") as f: print("from ast import *", file=f) print("_code = "+ast.dump(tree), file=f) print("fix_missing_locations(_code)", file=f) print("[globals().__delitem__(_) for _ in dir() if _[0]!='_']", file=f) print("exec(compile(_code,__name__,'exec'))", file=f) if __name__ == "__main__": for fn in sys.argv[1:]: outfn = "crunched/" + fn crunch(fn, outfn) print("Crunched", fn, "to", outfn) The resulting files should work just the same as the original ones, but will be virtually unreadable. This is still only obscurity, though; the only information actually removed is formatting and comments, not even names. But you could easily add an ast.walk() in there, just after the ast.parse(), and do whatever transformations you want. Note that unlike with JavaScript crunching, this won't shrink the files - in fact, it's usually going to expand them. They'll probably also take longer to load. There is no benefit other than the obscurity. Run this over some of your files, then see if your auditors are happy. ChrisA