4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonでdllinjection

Last updated at Posted at 2016-01-05

きっかけ

c言語でDLLInjectionの記事があったので、それを元にpythonで実行できるものを作ろうとした。

既にあった

こちらhttps://github.com/psychomario/pyinject

使ってみる

環境

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

dir dllInjection
dllinject.py//gitのからDLしたもの
use.py//下記で作成
spy.dll//後に説明

spy.dll

こちらの記事のものを流用
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;
}

このdllを作る。

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

use.py

次にgitからDLしたdllinject.pyを使うためのコードを書く。(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()

実行

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

cmd.png

result.png

注意点

DLLInjectionを成立させるためには、起動プロセスとDLLの種類が一致しないと成立しないです。
私の環境だとwow64+32bit dllでもだめでした。
http://furuya02.hatenablog.com/entry/20120114/1326484897

プロセス DLLの種類 結果
32bit 32bit
32bit 64bit ERROR_BAD_EXE_FORMAT
64bit 32bit ERROR_BAD_EXE_FORMAT
64bit 64bit
4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?