TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Pyrasite - Inject code into a running Python process

174 pointsby packetlssover 13 years ago

5 comments

onedognightover 13 years ago
TL;DR The code is mostly this with a nice wrapper.<p><pre><code> gdb_cmds = [ 'PyGILState_Ensure()', # Allow payloads to import modules alongside them 'PyRun_SimpleString("import sys; sys.path.insert(0, \\"%s\\");")' % os.path.dirname(self.filename), 'PyRun_SimpleString("execfile(\\"%s\\")")' % self.filename, 'PyGILState_Release($1)', ] self._run('gdb -p %d -batch %s' % (self.pid, ' '.join(["-eval-command='call %s'" % cmd for cmd in gdb_cmds])))</code></pre>
kevingaddover 13 years ago
If you're on Windows, an old project of mine has a more sophisticated take on this:<p><a href="https://github.com/kevingadd/shootblues" rel="nofollow">https://github.com/kevingadd/shootblues</a><p>You can inject a whole collection of scripts into one or more processes hosting a Python interpreter, load/unload scripts at runtime, and communicate between host processes via an RPC mechanism.<p>The only interesting use for it I ever came up with was modding the EVE Online user interface. There are a bunch of scripts that use it at <a href="https://github.com/kevingadd/shootbluesscripts" rel="nofollow">https://github.com/kevingadd/shootbluesscripts</a> - some of them are kind of interesting, like a gateway that lets you interact with scripts via Jabber messages, and a HTML5 remote desktop that lets you play the game from a remote browser.
评论 #3015413 未加载
burgerbrainover 13 years ago
That's pretty wild. Obviously you can do similar with straight up gdb and C programs, but does this sort of thing exist with other languages? I'd be fairly interested in something like this for racket and ruby.
评论 #3014904 未加载
评论 #3015127 未加载
评论 #3015247 未加载
评论 #3014986 未加载
评论 #3015968 未加载
评论 #3016574 未加载
johnthedebsover 13 years ago
This looks like a great little trick, but I can't get it to work on OS X Lion. Has anyone else on Lion gotten this to work?<p>I've got Xcode 4.1 installed with dev tools, and it seems like the version of gdb provided doesn't support the -eval-command flag which breaks the script.<p>I'm wondering if someone else can confirm this problem, and whether there is an easy way to to install a supported version of gdb.
Davertronover 13 years ago
Can someone describe why this sort of thing would be useful? I'm not criticizing, I just can't think of a use case for this.
评论 #3015021 未加载
评论 #3015134 未加载
评论 #3017133 未加载
评论 #3015250 未加载