Re:Show HN: Pytroj - Infect .pyc files
by jgeralnik on Monday September 26, 2011 @01:53PM
(#3039981)
Exactly. When you install my package, I can infect other packages and have them execute arbitrary code without you realizing it.
This is by no means unique to python, and in fact can happen whenever you run any program. It just shows the dangers of running things you don't really trust.
Doing this in python is, however, slightly harder to detect, as you are changing the pyc file and not the py file itself. Someone who looks at the source will notice nothing amiss, and in fact if the update the source the pyc will be rebuilt cleanly (until it gets reinfected).
Javascript and ruby are not generally byte-compiled and so you would have to change the source to infect them.
Other languages that compile either to bytecode or executables can be just as easily be infected.
[ Parent
| on HN ]
For ruby you could likely do something similar using rubinius which can precompile .rb files to .rbc files prior to execution.
Though they discourage the use of the .rbc files directly so not sure this is much to worry about as they change the bytecode format often enough.
The real way to make it undetectable would be to somehow distribute your package so that its own .pyc file has the code to infect others, but the source file doesn't. Otherwise, it just takes a glance at the code to see what's going on.