News for hackers. Stuff that matters.
Re:How to recover lost Python source code if it's still resi... by nisa on Sunday March 12, 2017 @06:38AM (#13850134)
If it's a single file can't you just copy the deleted filehandle from procfs?

https://www.linux.com/news/bring-back-deleted-files-lsof

Re:How to recover lost Python source code if it's still resi... by icebraining on Sunday March 12, 2017 @08:21AM (#13850365)
The code was deleted, not necessarily the file. But that's a nice tool to have as well.
Re:How to recover lost Python source code if it's still resi... by im3w1l on Saturday March 11, 2017 @08:39PM (#13848400)
This is worth a read just for the explanation of how to get an interactive python shell connected to a running script.
Re:How to recover lost Python source code if it's still resi... by colanderman on Saturday March 11, 2017 @09:13PM (#13848512)
To save everyone a click: https://pypi.python.org/pypi/pyrasite/

Wish I'd known about that years ago just for the ability to grab a call stack from a running program.

Re:How to recover lost Python source code if it's still resi... by cyphar on Monday March 13, 2017 @02:50AM (#13855749)
For those wondering how it works, it's basically just a wrapper around gdb which calls the internal C exec implementation in the Python interpreter (which then executes some other script file which you specify). Pretty cool.
Re:How to recover lost Python source code if it's still resi... by bdowling on Saturday March 11, 2017 @07:10PM (#13848061)
The method described is for recovering and decompiling bytecode into equivalent Python code, which is not exactly the same thing as source code, since it won't contain things like comments and unexported object names.

edit: made more accurate

Re:How to recover lost Python source code if it's still resi... by Latty on Sunday March 12, 2017 @06:48AM (#13850155)
Of course, Python is a little better than most languages for things like 'comments' - as a lot of them aren't really comments but docstrings, which are actually a part of the class, not a true comment, and so presumably can be restored.
Re:How to recover lost Python source code if it's still resi... by lewk on Sunday March 12, 2017 @01:25PM (#13851785)
Not if you use `python -OO`, which will optimize the docstrings out.
Re:How to recover lost Python source code if it's still resi... by dr_zoidberg on Saturday March 11, 2017 @08:44PM (#13848416)
Indeed, but it just gave me an idea for a weird use case we've been discussing at work :)
Re:How to recover lost Python source code if it's still resi... by yaur on Sunday March 12, 2017 @04:48AM (#13849870)
If it ever makes sense to do this it means you aren't committing frequently enough.
Re:How to recover lost Python source code if it's still resi... by sciurus on Sunday March 12, 2017 @09:37PM (#13854577)
Yep.

Amusingly, Simon and I were working on the same codebase during Eventbrite's quarterly hackathon when this happened. He was working solo on one feature while myself and three other developers were working on another. Because we were collaborating, we had to commit frequently simply to stay in sync. Because he was working alone, he was able to avoid committing and get into a situation where figuring out the process in this gist was necessary.

Despite this distraction and 1/4 the personpower, Simon arguably still wrote something cooler than we did.

Re:How to recover lost Python source code if it's still resi... by oogali on Sunday March 12, 2017 @11:38AM (#13851121)
Yes, in a perfect world...

However, if you're reverse engineering a system you've inherited, or one where source code isn't readily available, then this decompilation makes sense.

Re:How to recover lost Python source code if it's still resi... by remy_luisant on Sunday March 12, 2017 @04:54AM (#13849890)
Thanks for the reminder.
Re:How to recover lost Python source code if it's still resi... by edmorley on Saturday March 11, 2017 @07:58PM (#13848236)
This is certainly an interesting technique, however in this case surely git-reflog would have been easier/resulted in not losing code comments etc?
Re:How to recover lost Python source code if it's still resi... by revelation on Saturday March 11, 2017 @08:06PM (#13848270)
He presumably used checkout on a file with uncommitted changes.
Re:How to recover lost Python source code if it's still resi... by lewk on Sunday March 12, 2017 @01:32PM (#13851822)
Awesome use of pyrasite, great job! --luke (pyrasite author)
Re:How to recover lost Python source code if it's still resi... by mixmastamyk on Saturday March 11, 2017 @11:27PM (#13848959)
Interesting. One of the comments asks why he didn't copy the .py from the docker container, but it doesn't get answered. Too bad because I was curious also.
Re:How to recover lost Python source code if it's still resi... by chias on Saturday March 11, 2017 @11:45PM (#13849027)
Depending on the author's dev practices, the script might have been in a volume so that editing it on the host would allow the container to receive the changes as they get saved. While I wouldn't normally suggest this approach, it can be handy at times.
Re:How to recover lost Python source code if it's still resi... by yeukhon on Sunday March 12, 2017 @04:18AM (#13849770)
Mounting host directory in a container is quite the popular approach. n almost every development tutorials I have read using Docker mentions this approach.
Re:How to recover lost Python source code if it's still resi... by simonw on Sunday March 12, 2017 @01:15AM (#13849362)
Yup that was exactly the problem: I had the code in a folder that was mounted to the container as a volume.
Re:How to recover lost Python source code if it's still resi... by coldtea on Sunday March 12, 2017 @01:46PM (#13851904)
I had to do the exact thing for my .bash_profile, as I accidentally had deleted it (with tons of customizations) and I didn't have quick access to my backups, but I did had the deleted .bash_profile loaded into memory in some open terminal sessions.

Fortunately, you can just write:

  declare
to get all your aliases, functions, etc from the current shell session.
Re:How to recover lost Python source code if it's still resi... by scorpioxy on Saturday March 11, 2017 @07:07PM (#13848052)
Pardon my ignorance but why is this a big deal? Debuggers can attach to running processes and pyrasite can inject code into running processes so for a language like python, it makes sense that you would be able to fetch either the resident bytecode or the code itself. What am I missing?
Re:How to recover lost Python source code if it's still resi... by pepve on Saturday March 11, 2017 @07:19PM (#13848098)
You're not missing anything. But a bunch of us didn't figure it out before and we're just now learning about it. So we're excited, we're learning something new!

(Please, please, don't be condescending to people who are learning something new.)

Re:How to recover lost Python source code if it's still resi... by mannykannot on Saturday March 11, 2017 @10:33PM (#13848775)
There's a first time for everything you know...

https://news.ycombinator.com/item?id=13848028

Re:How to recover lost Python source code if it's still resi... by Twirrim on Saturday March 11, 2017 @09:07PM (#13848491)
It's some other people's day as part of the 10,000.

https://xkcd.com/1053/

Re:How to recover lost Python source code if it's still resi... by sillysaurus3 on Saturday March 11, 2017 @06:24PM (#13847861)
Would this work on Dropbox, which is mostly written in Python?
Re:How to recover lost Python source code if it's still resi... by tyingq on Saturday March 11, 2017 @07:04PM (#13848035)
They only ship encrypted pyc files, along with a one off Python interpeter that has the key inside it. Additional tricks as well, like scrambling the opcode table, disabling built in functionality you could use to dump in memory structures, etc.

Of course, since the key is in the binary, you can hack at it: https://www.usenix.org/conference/woot13/workshop-program/pr...

Re:How to recover lost Python source code if it's still resi... by santaragolabs on Sunday March 12, 2017 @07:04AM (#13850187)
Yep and they change things around every once in a while too. I RE'd dropbox several times using several different techniques. I just checked my old tarball containing a script which downloads dropbox binary, downloads the Python interpreter, builds the opcode table database and then decompiles everything.

  gvb@santarago:/tmp/lookinsidethebox$ ./run.sh
  fetched all dependencies..lets try decompiling
  no saved opcode mapping found; try to generate it
  pass one: automatically generate opcode mapping
  108 opcodes
  pass two: decrypt files, patch bytecode and decompile
  1928/1928
  successfully decrypted and decompiled: 1727 files
  error while decrypting: 0 files
  error while decompiling: 196 files
  opcode misses: 7 total 0x6c (108) [#9],  0x2c (44) [#14],  0x8d (141) [#15],  0x2e (46) [#1],  0x2d (45) [#14],  0x30 (48) [#5],  0x71 (113) [#11783],  
A starting point to do this yourself is: https://github.com/rumpeltux/dropboxdec. After unmarshalling the new pyc files the seed read in via the rng() function is in newer Dropbox installations passed through a Mersenne twister from which 4 DWORD values are being read which are then used to construct the key for the Tiny Encryption Algorithm cipher.

After that you get the binary blob back which you can unmarshall now. But you still need to figure out the opcode mapping. For that I used a trick publicly first done (to the best of my knowledge) by the author of PyREtic (Rich Smith) released at BH 2010. He just compares the stdlib pyc files with the stdlib included within dropbox (after decrypting those pyc files) byte by byte. That should yield a mapping of opcodes.

Then pass everything through uncompyle2 and you've got pretty readable source code back. Some files will refuse to decompile but that means hand-editing / fine-tuning the last bits of your opcode table a bit.

EDIT: follow-up on parent comment; the encryption keys are not in the interpreter. The interpreter is patched to not expose co_code and more (to make this memory dumping more difficult; injecting an shared object is a different technique that I used too). It's also patched to use the different opcode mapping and the unmarshalling of pyc files upon loading them. However the key for each pyc file is derived from data strictly in those files themselves. It's pretty clear when you load up the binary in IDA Pro and compare the unmarshalling code with a standard Python interpreter's code

Re:How to recover lost Python source code if it's still resi... by tyingq on Sunday March 12, 2017 @03:25PM (#13852524)
I thought that they would have added more obfuscation after that paper was published. Since the opcode generation and parsing is reset, per pyc file, I expected to see stuff like:

- A rotating opcode table that changes every X opcodes

- Multiple opcodes that referenced the same operation, selected randomly at generation time.

Re:How to recover lost Python source code if it's still resi... by neurostimulant on Sunday March 12, 2017 @04:36AM (#13849832)
Now I understand why they hired Guido back then.
Re:How to recover lost Python source code if it's still resi... by drdaeman on Saturday March 11, 2017 @06:40PM (#13847924)
IIRC, they used to randomize bytecode a bit, to avoid decompilation with off-the-shelf tools.
Re:How to recover lost Python source code if it's still resi... by kodfodrasz on Sunday March 12, 2017 @03:02AM (#13849550)
I'm pretty sure this would be a violation of the EULA.
Re:How to recover lost Python source code if it's still resi... by dorianm on Saturday March 11, 2017 @06:42PM (#13847931)
Anybody knows if it's possible in Ruby? Just being able to have a live irb from a process ID would be pretty awesome
Re:How to recover lost Python source code if it's still resi... by vidarh on Saturday March 11, 2017 @11:38PM (#13849000)
"sudo gdb -p [process id]" will attach to the running process. If you attach that to a Ruby process, you can do:

   call rb_eval_string("puts 'hi from the Ruby process'")
(The latter will output to the ruby scripts STDOUT so if you can't see the stdout from the Ruby interpreter, it won't produce anything - test it by just starting a ruby interpreter and find it's pid)

You can use that to (try to) load code. For example try loading pry and setting up a pry-remote connection. You can also use any other part of the MRI extension API - gdb can do quite complicated calls.

Note that any uncaught error may easily cause the Ruby script you're connected to to terminate, and there are plenty of opportunities to cause problems since when you're connecting gdp to the process, the Ruby interpreter will be paused at an arbitrary point, which may leave the interpreter in an inconsistent state.

To wrap the above up into anything reasonable, you'd want to first ensure the interpreter is in a decent state by setting a breakpoint somewhere "sane" in the interpreter, continue execution, and then execute some very carefully crafted code to let you inject the code you need without triggering any errors that mess up for the script, or changing variables etc. the script depends on.

It probably could be wrapped up into something quite nice combined with pry/pry-remote, though.

Re:How to recover lost Python source code if it's still resi... by dorianm on Monday March 13, 2017 @08:36PM (#13863935)
Thanks, I will probably make a wrapper gem around that (e.g. maybe just a `call rb_eval_string ("binding.pry")`).

In my case I get "No symbol "rb_backtrace" in current context.", I will look deeper into it.

Also, there is a great gist about Ruby GDB debugging: https://gist.github.com/mmullis/6211061.

Re:How to recover lost Python source code if it's still resi... by jkm3141 on Saturday March 11, 2017 @07:31PM (#13848144)
While not exactly the same it's still quite useful. https://github.com/Mon-Ouie/pry-remote
Re:How to recover lost Python source code if it's still resi... by dorianm on Saturday March 11, 2017 @08:16PM (#13848304)
Thanks, I guess I could just have some special key that triggers pry-remote (E.g trap ctrl-D or something)
Re:How to recover lost Python source code if it's still resi... by w4tson on Sunday March 12, 2017 @03:38AM (#13849675)
I did this yesterday. However IntelliJ had my back. It's History feature has saved my bacon on more than one occasion.

One time I was able to go back weeks to fetch some code I'd long since deleted and was nowhere to be found in git.

Re:How to recover lost Python source code if it's still resi... by mintplant on Sunday March 12, 2017 @04:11AM (#13849748)
I have this in my .vimrc which serves a similar purpose:

    " persist undo history to file / across sessions
    set undofile
    set undodir=$HOME/.vimundo
    " max out history length
    set history=9999
    set undoleves=9999999999
    set undoreload=10000

(Don't forget to mkdir "$HOME/.vimundo".)

Combine that with https://github.com/mbbill/undotree and you can easily walk the entire history of every file you edit.

Re:How to recover lost Python source code if it's still resi... by semi-extrinsic on Sunday March 12, 2017 @10:16AM (#13850736)
That undotree plugin looks sweet! Do I understand correctly that if I do N undos, then fat-finger an edit instead of a yank, I can still get back to where I was before the N undos? This would solve my only big gripe with vim's undo.

(Well, the other big gripe is that when I'm forced to use a program with crippled undo, e.g. any Microsoft product ever, I'm frustrated up the wall. The worst-in-class award goes to Word with Endnote, where many Endnote commands wipe your entire undo history. How people can use that on a permanent basis is beyond me.)

Re:How to recover lost Python source code if it's still resi... by calpaterson on Sunday March 12, 2017 @06:51AM (#13850161)
The equivalent emacs code is here: https://www.emacswiki.org/emacs/BackupDirectory
Re:How to recover lost Python source code if it's still resi... by akx on Sunday March 12, 2017 @04:56PM (#13853057)
Is it actually `undoleves` or is that a typo?
Re:How to recover lost Python source code if it's still resi... by mintplant on Monday March 13, 2017 @10:45AM (#13858192)
That should be `undolevels`, yes. Unfortunately I can't edit it now.
Re:How to recover lost Python source code if it's still resi... by cjbprime on Sunday March 12, 2017 @07:29AM (#13850253)
Interesting! I wonder if...

gdb -p `pidof python`

gcore foo.core

strings foo.core | grep -a200 -A200 knowntext

.. would also work.

Re:How to recover lost Python source code if it's still resi... by icebraining on Sunday March 12, 2017 @08:22AM (#13850367)
I don't think python keeps the text file in memory, since it compiles everything to bytecode on startup.