19

I'm building a fairly complex interpreted program in Python. I've been working on most of this code for other purposes for a few months, and therefore don't want my client to be able to simply copy and try to sell it, as I think it's worth a fair amount.

The problem is that I need the script to run on a server that my client is paying for, so is there any way I can secure a particular folder on the machine from root access, or make it so only one particular use can access the directory? The OS is Ubuntu.

Mike Partridge
  • 6,587
  • 1
  • 25
  • 39
  • 13
    What kind of contract/agreement exists between you and your client? Do they own the code, or have you merely licensed usage of it to them? Or have you not worked any of this out? – matt b Apr 06 '11 at 15:46
  • 2
    I thought it was possible to compile python source into binary. Is possible for this project? If so you could deploy the binary instead. – FrustratedWithFormsDesigner Apr 06 '11 at 15:51
  • nightcracker is right in my opinion. Proper licensing and contracts should prevent all this, not technical measures. Especially because you can always decompile/de-obfuscate something and `root` is allowed to do *everything*. – Bobby Apr 06 '11 at 16:02
  • Think of it the other way, if they break a contract or license you can sue them till they weep – Jakob Bowyer Apr 06 '11 at 16:09
  • 9
    Everybody always says their code is worth a fair amount. – Kaleb Brasee Apr 09 '11 at 01:39
  • There's a good discussion on this topic here: https://groups.google.com/forum/#!topic/nodejs/mPIcq5mHihM – jpillora Aug 07 '13 at 01:36

6 Answers6

41

License it.

Really, that's all!

orlp
  • 2,476
  • 1
  • 19
  • 20
  • 6
    My first thought was to downvote this as not an answer, but heck, you're right. Licensing and signing contracts is the way to go here. – Bobby Apr 06 '11 at 15:52
  • @Bobby: Almost the only way to go. The alternative is to keep adding delightful, useful new features so quickly that stealing the old version would be a waste of time. – S.Lott Apr 06 '11 at 17:11
  • would creative commons suffice as a license? –  Apr 06 '11 at 21:27
  • Creative commons have a lot of licenses. If you would edit your question with your demands from the license (free software, redistributable or not, etc) then I could suggest one. – orlp Apr 06 '11 at 21:32
  • @James Eggers: The question about what license to use belongs on programmers. – Bobby Apr 07 '11 at 14:37
  • 13
    -1 The question asks for ways to stop a client seeing the code, this does not do that. People do not always obey licenses, and it may not be possible to find out for sure legally in order to sue. – Orbling Apr 09 '11 at 02:05
  • 8
    You can't stop the client seeing Python code, it's too easy to disassemble. DRM doesn't work (if they can run it, they can disassemble it). So the only answer is a legal one. And once you're relying on the law for enforcement, there's no point making life harder for yourself or your customers by complicating the technical base. – ncoghlan Apr 09 '11 at 10:52
  • @Orbling: That's not what the question stated: "therefore don't want my client to be able to simple copy and paste it and then try and sell it". No one said anything about seeing code. – orlp Apr 13 '11 at 20:01
  • @nightcracker: In order to copy and paste it, you have to see it - once you can, you can. It is the issue... – Orbling Apr 13 '11 at 21:27
  • @Orbling: It's all about how you define `being able`. If it means physically possible to copy/paste, yes, then you have to obfuscate the code somehow. But I interpret it as "I don't want my client to make money off my code". – orlp Apr 13 '11 at 22:00
  • @nightcracker: In many cases, how would you know? ie. It's not always possible to sue someone to prevent that, the only safe measure is not to allow tangible access to the code, that means compiling, or for interpreted, encryption/obfuscation to whatever level is possible. – Orbling Apr 13 '11 at 23:09
  • @ncoghlan: Disassembly is not always overly useful to anyone, unless they get back some structure of the original, the identifiers, etc. If you run code through an obfuscator and make it as hard to disassemble as possible, that helps. Law is a very unreliable preventative - see any pirated software. – Orbling Apr 13 '11 at 23:11
  • @Orbling: Ah, you just disproved your own point. Compiled, DRM-riddled software gets pirated *anyway* (and often provides a better end-user experience than the legitimate copies, since the "I'm terrified you might be a thief!" crap has been removed). Legal agreements and good customer service are your only effective options. Focus on providing a good experience for the people that *are* giving you money, and forget about the freeloaders. The second you make the experience of a paying customer *worse* due to fear of freeloaders, you're doing it wrong. – ncoghlan Apr 14 '11 at 04:49
  • @ncoghlan: I think there is a very different situation from people using your software, to people reusing your code - for alternative means. – Orbling Apr 14 '11 at 11:45
  • 1
    @ncoghlan: Putting signs and laws (i.e. the license) in place is generally not enough to stop people with bad intentions. So you need fences and controls to achieve at least part of the goal. To prevent the offence (seeing/reusing/stealing code in this case) you need to take *all* reasonable measures. This includes licensing the code, of course, but also making a potential felon's life harder by using simple yet effective solutions. Indeed, finding the balance is hard (e.g. invasive DRMs suck) but that is the price of efficiency. What if the police gave up saying "they won't obey the law" ? –  May 10 '16 at 10:26
10

You can always compile all you files to byte code pyc. There are decompilers out there that can generate source code out of it but nothing serious.

However that will just solve the ability to read the code of your program. To protect the only way is to license it as nightcracker said, because even if you compiled your code, to lets say machine code, if your work is not protected by a license, it can still be commercialized against your will.

Bottom line, compile to byte code and more importantly License it

  • I have a question. Does the byte compiler use arbitrary variable names, or does it use the old variable names? (I would imagine it would have to use the old ones, as external modules rely on that namespace). –  Apr 06 '11 at 16:19
  • @Garrett: The bytecode contains the variable names. It's still a relatively unreadable mess when disassembled (try [digesting the disassembly](http://www.ideone.com/0N3Us) of [this recipe](http://code.activestate.com/recipes/577612-seven-bit-colored-analogue-bar-graph-generator-dem/?in=lang-python)), and there aren't any (recent) decompilers. –  Apr 06 '11 at 16:35
7

Use Cython. This will allow you to compile your program as a native executable. Then it should be much harder to steal.

As for the directory, the only advice I can give you is make sure you've got your permissions set up correctly. ACLs may be your friend, although I'm not 100% sure that they can restrict root from accessing a file. Even if they could, root could still just change the permission. He's root, he's god -- that's just how these things work.

http://www.korokithakis.net/node/109

Chris Eberle
  • 919
  • 8
  • 12
1

I'd suggest licensing, too. On top of licensing, let's encrypt the source code of main routines using asymmetric key algorithm so that only your client's machine can run it. One of the key in the pair be something obtained from the hardware (example: network card's serial number) of your client's machine. Use the other key in the pair to decrypt the source code when running the program. Note that the only deliverable in plaintext would be the decryption routine and the rest would be in ciphertext.

This way your client can copy-and-paste your seemingly gibberish code but can't run it elsewhere. My suggestion is not completely bullet-proof however: the interpreter may store the decrypted program somewhere in memory. Then it is possible that some hacker retrieve your program in plaintext during execution I guess.

As for preventing folders from root access, I agree that root can't be stopped from accessing any files/folders.

  • This is just obfuscation, with the right tools, there will *always* be a point in time where the code is in plain text. A simple way to defeat your solution would be to simply swap out `ruby`/`python`/`node` with a custom program mirrors the execution API and outputs the code. – jpillora Aug 07 '13 at 01:40
  • Additionally, the private key (for decryption) will have to be available to the environment, therefore available to an attacker. – Fixee Oct 10 '17 at 23:47
1

As the user above showed, disassemblers can get the code back, but as yet it is not very readable (at least not for the open source disassemblers).

I was thinking about this, and one way that I think you could solve this problem (if you call forced open code a problem) is to write an automatic re factoring script. This would be fairly simple actually. You would just feed the script your module, and it would rename all the module-specific variables. This, along with only releasing the compiled file, would do a lot to obfuscate your code.

Doing a search on the PyPI, I found this: http://pypi.python.org/pypi/pyfuscate/0.1 . You should check it and other's like it out and report back :D

Also: You should also License it, of course.

0

Licensing is the best answer here. That said, why does it have to run on their gear? If it is so critically important you might want to spring for a service and build some sort of service API around things so folks can't even see your intellectual property to steal it.

Wyatt Barnett
  • 20,685
  • 50
  • 69