How to modify external process using Python - python

Beginner here. I am trying to figure out how to modify a running process on a linux system using Python.
Example: I have a python program that takes in as an argument a PID. My goal is to use this PID and get info about the running process with that PID.
(1) Find where it is located in memory
(2) Where is the instruction pointer
(3) Modify the program such that the next executed instruction is something else
(4) Return the pointer back to the next legitimate instruction
(5) Let the original process execute as it should have
I am trying to develop a POC to show how a small piece of code can be injected into a running process to just print 'hello' to stdout and not disturb the rest of the process.
I looked up trace and some other modules but they all seem to do with following the currently executing python process. Also looked at pyhook, but its mainly to trap signals from keyboards etc.. additionally, I looked up pygdb a bit.
Can anyone please point me to some modules that might be useful, or some code samples. I tried googling for "python inspect process PID" etc.. did not get anything very useful.
Any help is very appreciated.
Thanks!

Also a newer python user. Can you do all these things with just the command line? If so then you could use os.system('[command]') or the subprocess module. For example you could use the pmap command to get the memory mappings. As for 2-5 I have no experience there. Good Luck.

Related

Run python interactive console as a subprocess in swift with sending commands and receiving results after launched

I'm making my first steps in macOS app development.
I'm trying to write an app on Swift that would keep python interactive console open.
Sometimes I would like to send to python commands and return the results back to swift, but not closing python to keep all variables for the next command I will send.
Is there any way to do that?
As far as I understand, I can't use the Process() because the input pipe automatically closes when I run the task.
I probably need to use pseudo terminals with pty and tty, but I don't fully understand the idea and where to learn about it. (or, maybe, I'm wrong and there is another way)
If you actually want to use python code from swift, I would strongly advise you to avoid using this method. It is very bug prone and potentially limiting and inefficient. You better use some wrapper of the python-c-api, or write some small server in python to receive requests from swift.
If you still want to do that, an easy way to go about it would be to use python itself to spawn python inside a pty:
python -c "import pty, sys; pty.spawn(sys.argv[1:])" python
This will start a python console that reads and writes to stdio instead of of /dev/tty.

How do I trace my python program from start of execution to finish?

I have a function written in python, and I want to inspect how it runs step by step from start to finish. How do I go about doing this?
I am using PyCharm as an IDE, but I don't know if it has a tracing feature.
Any tips or resources that are newbie friendly on this issue?
Thanks very much in advance!
What you're looking for is a profiler. Luckily, PyCharm is really powerful and comes with a wealth of debugging/profiling tools.
If you're running your code within PyCharm, simply set a breakpoint on the first line within the function you wish to examine, then step through it using their interface.
If you're running your code via the commandline, I highly recommend familiarizing yourself with Python's debugging module, pdb. All you need to do to examine your function is temporarily add the line:
import pdb;pdb.set_trace()
.. as the first line of your function. When you run it and it hits this line, you can step through the execution on the commandline using simple directives like 'n' for next line.

Using python to talk to a terminal program back/forth

So I've been using subprocess and pexpect
to try to interact with a separate program running in the terminal. I need to feed it a command, with arguments, and be able to receive it's response and potentially send it more commands.
With subprocess, I have only been able to launch a terminal, but not feed it commands. Or I can pass ONE line of command to an emulated terminal within python. The issue it that it's one-and-done and I can't really interact with it.
pexpect seems to only be able to initiate one command, and then respond to the terminal in an automated fashion, I couldn't find relevant and up to date documentation that went over what I needed.
Are there better modules to use for this? Or am I using them the wrong way?
-Thanks,
-Sean
pexpect is your best candidate, as far as I'm aware.
It's documentation matches version on pypi - 3.2 as for now.
If you would like to run bunch of commands one after another you can try to divide commands with ";" or "&", depends on your usage.
Btw. please take a look at example section.

Multiple Windows with a Multithreaded pyqt application

I wrote a small multiprocessing application and then wrote a PyQt front end for it. When I run the script by calling it from the command line with Python (or by calling run from the Spyder IDE), it runs exactly as I would expect and works nicely.
But if I try to use Py2Exe to make an executable to give it to a friend, it starts behaving oddly. When the users hits the botton that really starts the process and invokes the multithreading portion, it spawns multiple Qt windows that look like the original. It then essentially locks up. Closing one of the new windows that it spawns causes it to reopen that window. Attempting to close the original generates a message that it is not responding.
I would appreciate any help or suggestions about where to look.
I'm not positive about this without looking at your code, but there are some extra considerations when using Py2Exe with multithreading.
Take a look at this link and maybe it has something to do with your problem.
Someone has a similar sounding issue here

How to get pycassaShell working in windows?

EDIT: I got it working, I went into the pycassa directory and typed python pycassaShell but the 2nd part of my question (at the bottom there) is still valid: how do I run a script in pycassaShell?
I recently installed Cassandra and pycassa and followed the instruction from here.
They work fine, except I cant get pycassaShell to load. When I type pycassaShell at the command prompt, I get
'pycassaShell' is not recognized as an internal or external command,
operable program or batch file.
Do I need to set up a path for it?
Also, does anyone know if you can run ddl scripts using pycassaShell? It is for this reason that I want to try it out. At the moment, I'm doing all my ddl in the cassandra CLI, I'd like to be able to put it in a script to automate it.
You probably don't want to be running scripts with pycassaShell. It's designed more as an interactive environment to quickly try things out. For serious scripts, I recommend just writing a normal python script that imports pycassa and sets up the connection pool and column families itself; it should only be an extra 5 or so lines.
However, there is an (undocumented, I just noticed) optional -f or --file flag that you can use. It will essentially run execfile() on that script after startup completes, so you can use the SYSTEM_MANAGER and CF variables that are already set up in your script. This is intended primarily to be used as a prep script for your environment, similar to how you might use a .bashrc file (I don't know of a Windows equivalent).
Regarding DDL statements, I suggest you look at the SystemManager class.

Categories