顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2014-09-14

C# 注入DLL (Inject DLL by CSharp)

解說:http://www.javaworld.com.tw/roller/qing/entry/2007_5_18_DLL_injection
參考這篇 http://blog.csdn.net/intcry/article/details/6318596 程式碼

















看完一定會再找其他的,看這篇.http://wenku.baidu.com/view/cfdcc20ef12d2af90242e626.html
然後先去了解createremotethread是在幹什的。

通常都是注入一個非託管的dll(不是運行在CLR上),下面這是一個C++上的標準DLL,帶有DllMain(入口函數),不是普通的類庫DLL,而C#無法產生一個帶有入口函數的DLL。

以下這是要注入的非託管DLL代碼(打開txt檔寫入資料)
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved) {
/* open file */
FILE *file;
fopen_s(&file, "C:\temp.txt", "a+");
switch(Reason) {
case DLL_PROCESS_ATTACH:
fprintf(file, "DLL attach function called.n");
break;
case DLL_PROCESS_DETACH:
fprintf(file, "DLL detach function called.n");
break;
case DLL_THREAD_ATTACH:
fprintf(file, "DLL thread attach function called.n");
break;
case DLL_THREAD_DETACH:
fprintf(file, "DLL thread detach function called.n");
break;
}
/* close file */
fclose(file);
return TRUE;
}
DllMain entry point :
An optional entry point into a dynamic-link library (DLL). When the system starts or terminates a process or thread, it calls the entry-point function for each loaded DLL using the first thread of the process. The system also calls the entry-point function for a DLL when it is loaded or unloaded using the LoadLibrary and FreeLibraryfunctions.


總結:
用什麼來寫加載DLL不會差太多。
要注入非託管DLL,直接注入。
要注入託管DLL,先注入非託管的DLL來加載CLR再去加載託管DLL。

完整的C++程式碼和說明提供C++使用者參考。


2014-09-12

C# AutoASM & Read and Write Process Memory

首先,這篇是用來對Procss寫入Assembly和讀寫記憶體的應用,對如何
取得Process程序裡的基址(baseAddress)和偏移(offset)就在另一篇另外說
名。

(1)如果有用過CE(Cheat Engine),就會知道可以直接寫入Assembly對程序進
行Hook的動作,只改寫原本的AOB(Array Of  Bytes )或者是在另外跳躍至
自行宣告的記憶體函數來進行更多動作。在這一方面,我採用了原本CE
的AutoAssembler.dll,裡面已經帶有可以直接寫入Asm的函數,需要做的就
是去引入dll並且宣告,然後在自己的程序中就能直接對其他程序寫入ASM

(因為C#中沒辦法使用直接使用像_asm這種東西,可以看這個說明,自行用
C++做出可以讓C#調用的DLL,就像AutoAssembler.dll或者這個這個。)

首先,我們先使用AutoASM來寫入

















在C#中宣告加以調用
[DllImportAttribute("ceautoassembler.dll", EntryPoint = "CEInitialize")]
        public static extern bool CEInitialize
        (
            int PassedPID,
            IntPtr handle
        );

[DllImportAttribute("ceautoassembler.dll", EntryPoint = "CEAutoAsm")]
        public static extern bool CEAutoAsm
        (
            string script,
            bool AllocID,
            int Alloc 
        );
可以很清楚的看出他要我們傳進哪些訊息。
在使用kernel32.dll裡的OpenProcess傳進pid和其他參數,取得Handle。

再來只要建一個TextBox讓使用者將Script貼上,再傳進CEAutoAsm就是
以前常見的打勾掛之一。

(2)直接使用kernel32.dll裡的ReadProcessMemory和WritePorcessMemory
下去寫入Bytes,寫入可以在其他地方用asm方式寫入看他的byte,記錄從多少
變為多少。讀取則是可以讀出byte後去翻譯成asm或者是其他資料型態(int,float)

















然而這只使用nop去替代原本那一行而已,如果要加入更多動作應該要再跳躍到空白
記憶體去寫入AOB。那要怎找出空白記憶體呢? 在AutoASM中是用Alloc下去分配空白
記憶體給你,我想在這邊也許也可以先用AutoASM先分配記憶體,再去寫入指令。
又或者,去查找有哪邊的AOB是 00 00 00這種連續一大排的,然後空間夠你寫入動作