dll injection in python

Trigger

There was an article about DLL Injection in c language, so I tried to make something that can be executed in python based on it.

http://inaz2.hatenablog.com/entry/2015/08/08/223643

Already there

Here https://github.com/psychomario/pyinject

Try using

environment

windows7 pro service pack 1 (64bit) python2.7 notepad(64bit) gcc 4.8.3

dir dllInjection
dllinject.py//DL from git
use.py//Created below
spy.dll//Explained later

spy.dll Diverted from this article http://inaz2.hatenablog.com/entry/2015/08/08/223643

spy.c


#include <windows.h>
#pragma comment(lib, "user32")

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
	char filename[MAX_PATH];
	switch (fdwReason) {
		case DLL_PROCESS_ATTACH:
			GetModuleFileName(NULL, filename, sizeof(filename));
			MessageBox(NULL, filename, "Hello from", MB_SYSTEMMODAL);
			break;
	}
	return TRUE;
}

Make this dll.

gcc -c spy.c //spy.o
gcc -shared -o spy.dll spy.o //spy.dll

use.py Next, write the code to use dllinject.py downloaded from git. (A level that can be console ...)

use.py


import sys
import dllinject
pid = int(sys.argv[1])
proc=dllinject.Process(pid=pid)
proc.inject("C:\\Users\\'hoge'\\Desktop\\dllInjection\\spy.dll")
proc.terminate()

Run

>C:¥Windows¥notepad.exe
>tasklist
...
notepad.exe  1988 Console  1  9,384K
...
>python use.py 1988

cmd.png

result.png

important point

In order to establish DLL Injection, it cannot be established unless the startup process and the DLL type match. In my environment, wow64 + 32bit dll didn't work either. http://furuya02.hatenablog.com/entry/20120114/1326484897

process DLL type result
32bit 32bit
32bit 64bit ERROR_BAD_EXE_FORMAT
64bit 32bit ERROR_BAD_EXE_FORMAT
64bit 64bit

Recommended Posts

dll injection in python
DI (Dependency Injection) in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
[Python] [Windows] Serial communication in Python using DLL
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python