How to Protect My Python Scripts on Raspberry Pi

Python script to execute on Raspbian (Code Protection)

The theoretical aspects if you can obfuscate python sourcecorde are already widely discussed for example in this stackoverflow question.

If you just want to make it a little bit harder for someone to read your code compiling it as .pyc could be a solution or maybe your .exe export even runs with WINE on raspbian.

Raspberry pi protection against reverse engineering the codes

Just to be clear, "cant read a file" means "can't run a program", which means "can't see what process are running, see files in directories".

From your question, I don't understand why you'd even leave the pi user in place...


... runs backend programs with root privilege

Never a good idea - use service accounts instead.

But I dont want curious customer that plugs in HDMI and see my code.

Then don't enable the HDMI output, don't have a graphical desktop installed and disable the login prompt. You might want to look at a "minimal" / "lite" image.

Remember that the UART can present a login prompt, so make sure that's disabled too.

And as the config.txt and kernel need to be in cleartext in the boot partition they can be easily swapped... thus these steps are not going to be terribly effective.

I also dont want him to take the SD card and extract the code.

You could look at encrypting the filesystems (e.g: LUKS), but the Raspberry Pi has no native ability to store data and identify itself... so your encryption key can only be something like the MAC address, or stored in cleartext on the SD card...

Fundamentally this is just going to be a deterrent from casual "oh what's this" investigations.

"Physical access is total access"... once you put it in the customer's hands, you're looking at deterrents more than absolutes.

I heard its possible to reverse engineer the code even if compiled. So I simply want the programs (python script) to be there but cannot be accessed in any way.

Python doesn't get compiled until runtime, so you'll need to ship the device with your source code on it...


If you really want to secure your Intellectual Property, then perhaps the Raspberry Pi isn't the best option? It's up to you to balance cost vs security.

Hide/protect Python code

The compiled code (.pyc files) can be used if you wish for others to be able to execute but not to read or modify the source code (.py, .pyw).

Simply:

  1. run your application
  2. then copy all the relevant .pyc files into another folder and you should be able to
  3. run it all from the new location

So long as all the appropriate modules are still able to be loaded, everything will work. This will require the version of python to be the same (can't run .pyc files from python 2.4 with python 2.7 and vice-versa)

The other thing to know is that strings will be preserved. You should open them up in a good text editor (I use vim) and inspect the content if you are worried about what others can see.

py2exe is of course another example, but you lose the ability to have cross-platform code at that point -- and if your application is for the Raspberry Pi -- that won't work.

Since you provided no other information about how you intend to run the code, it's not clear if the source will be a module or intended to be run directly. You should read this post to learn more.

setting rpi-gpio pin on high after running a python script

To make my comment an answer:

Gpiozero only resets the pins it touches, so if you don't initialize or touch pin 26 with gpiozero (i.e. replace step_en.off() with pigs w 26 0 and don't even initialize step_en), it shouldn't also reset the pin:

import os
import time

import gpiozero

step_pul = gpiozero.DigitalOutputDevice(21)

def motor_step():
SPR = 40000 # steps per rotation
step_count = SPR
delay = .000025
for x in range(step_count):
step_pul.on()
time.sleep(delay)
step_pul.off()
time.sleep(delay)

# Enable motor
os.system("pigs w 26 0")

for i in range(1):
# camTrigger(1)
motor_step()

# Disable motor
os.system("pigs w 26 1")


Related Topics



Leave a reply



Submit