插入代码
```
/
/
doInject.cpp : 此文件包含
"main"
函数。程序执行将在此处开始并结束。
/
/
DWORD getTargetProcessID ( const char
*
targetProcName ) {
/
/
PROCESSENTRY32
is
used to
open
and
get information about a running process..
PROCESSENTRY32 entry;
entry.dwSize
=
sizeof ( PROCESSENTRY32 );
/
/
We use a th32snapprocess to iterate through
all
running processes.
HANDLE hSnap
=
CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS, NULL );
/
/
Success check oon the snapshot tool.
if
( !hSnap ) {
printf (
"Snapshot tool failed to open\n"
);
}
/
/
If a first process exist (there are running processes), iterate through
/
/
all
running processes.
DWORD ProcID
=
NULL;
if
( Process32First ( hSnap, &entry ) ) {
do {
/
/
If the current process entry
is
the target process, store its
ID
.
if
( !strcmp ( entry.szExeFile, targetProcName ) ) {
ProcID
=
entry.th32ProcessID;
}
}
while
( Process32Next ( hSnap, &entry ) && !ProcID );
/
/
Move on to the
next
running process.
}
else
{
/
/
If there was no first process, notify the user.
printf (
"No running processes found"
);
}
return
ProcID;
}
bool
inject ( const char
*
targetProcName, const wchar_t
*
dllName ) {
try
{
/
/
Get the process
id
of the target process.
DWORD targetProcID
=
getTargetProcessID ( targetProcName );
if
( !targetProcID ) {
throw
"Target process Was not found"
;
}
/
/
Get a static address of the LoadLibrary function as a thread
-
start
-
routine function.
LPTHREAD_START_ROUTINE funcLoadLibrary
=
( LPTHREAD_START_ROUTINE ) GetProcAddress ( GetModuleHandleA (
"Kernel32.dll"
),
"LoadLibraryW"
);
if
( !funcLoadLibrary ) {
printf(
"Failed to retrieve a static function pointer to `LoadLbraryA\n`"
);
}
/
/
Open
the target process.
HANDLE hProcess
=
OpenProcess ( PROCESS_QUERY_INFORMATION |
PROCESS_CREATE_THREAD |
PROCESS_VM_OPERATION |
PROCESS_VM_WRITE | PROCESS_VM_READ, FALSE, targetProcID );
if
( hProcess
=
=
INVALID_HANDLE_VALUE ) {
printf(
"Failed to open target process"
);
}
DWORD dwSize
=
( lstrlenW ( dllName )
+
1
)
*
sizeof ( wchar_t );
/
/
Virtually allocate memory
for
the path of the dll
in
the target process.
LPVOID pszLibFileRemote
=
( PWSTR ) VirtualAllocEx ( hProcess, NULL, dwSize, MEM_COMMIT, PAGE_READWRITE );
if
( !pszLibFileRemote ) {
printf(
"Failed to allocate memory in the target process\n"
);
}
/
/
Write the dll path to the target process using WPM.
SIZE_T writeSize_
=
0
;
DWORD n
=
WriteProcessMemory ( hProcess, pszLibFileRemote, ( PVOID ) dllName, dwSize, &writeSize_ );
wchar_t tempBuffer_ [
256
]
=
{
0
};
SIZE_T readSize
=
0
;
ReadProcessMemory ( hProcess, pszLibFileRemote, tempBuffer_, dwSize, &readSize );
/
/
Create a remote thread
in
the target process with LoadLibrary to load our dll into the target process.
HANDLE hRemoteThread
=
CreateRemoteThread ( hProcess, NULL,
0
, funcLoadLibrary, pszLibFileRemote,
0
, NULL );
if
( !hRemoteThread || hRemoteThread
=
=
INVALID_HANDLE_VALUE ) {
printf(
"Failed to load dll into target process"
);
}
/
/
Wait until the remote thread
is
done loading the dll.
WaitForSingleObject ( hRemoteThread, INFINITE );
} catch( const char
*
err ) {
std::cout <<
"An erro occurred: "
<< err << std::endl;
return
false;
}
return
true;
}
int
main(){
wchar_t fullname [MAX_PATH]
=
{
0
};
GetFullPathNameW ( L
"InjectedDll.dll"
, MAX_PATH, fullname, NULL );
bool
success
=
inject ( (
"notepad.exe"
), fullname );
if
( success ){
printf (
"inject succesfully!!!"
);
}
}
int
main ( const
int
argc, char
*
argv[ ] ) {
char
*
lpDLLName;
char
*
lpProcessName;
char lpFullDLLPath [MAX_PATH];
if
( argc
=
=
3
) {
lpDLLName
=
argv [
1
];
lpProcessName
=
argv [
2
];
}
else
{
printf (
"[HELP] inject.exe <dll> <process>\n"
);
return
-
1
;
}
const DWORD dwProcessID
=
getTargetProcessID ( lpProcessName );
if
( dwProcessID
=
=
( DWORD )
-
1
) {
printf (
"An error is occured when trying to find the target process.\n"
);
return
-
1
;
}
printf (
"[DLL Injector]\n"
);
printf (
"Process : %s\n"
, lpProcessName );
printf (
"Process ID : %i\n\n"
, (
int
) dwProcessID );
const DWORD dwFullPathResult
=
GetFullPathNameA ( lpDLLName, MAX_PATH, lpFullDLLPath, nullptr );
if
( dwFullPathResult
=
=
0
) {
printf (
"An error is occured when trying to get the full path of the DLL.\n"
);
return
-
1
;
}
const HANDLE hTargetProcess
=
OpenProcess ( PROCESS_ALL_ACCESS, FALSE, dwProcessID );
if
( hTargetProcess
=
=
INVALID_HANDLE_VALUE ) {
printf (
"An error is occured when trying to open the target process.\n"
);
return
-
1
;
}
printf (
"[PROCESS INJECTION]\n"
);
printf (
"Process opened successfully.\n"
);
const LPVOID lpPathAddress
=
VirtualAllocEx ( hTargetProcess, nullptr, lstrlenA ( lpFullDLLPath )
+
1
, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
if
( lpPathAddress
=
=
nullptr ) {
printf (
"An error is occured when trying to allocate memory in the target process.\n"
);
return
-
1
;
}
printf (
"Memory allocate at 0x%X\n"
, ( UINT ) ( uintptr_t ) lpPathAddress );
const DWORD dwWriteResult
=
WriteProcessMemory ( hTargetProcess, lpPathAddress, lpFullDLLPath, lstrlenA ( lpFullDLLPath )
+
1
, nullptr );
if
( dwWriteResult
=
=
0
) {
printf (
"An error is occured when trying to write the DLL path in the target process.\n"
);
return
-
1
;
}
printf (
"DLL path writen successfully.\n"
);
const HMODULE hModule
=
GetModuleHandleA (
"kernel32.dll"
);
if
( hModule
=
=
INVALID_HANDLE_VALUE || hModule
=
=
nullptr )
return
-
1
;
const FARPROC lpFunctionAddress
=
GetProcAddress ( hModule,
"LoadLibraryA"
);
if
( lpFunctionAddress
=
=
nullptr ) {
printf (
"An error is occured when trying to get \"LoadLibraryA\" address.\n"
);
return
-
1
;
}
printf (
"LoadLibraryA address at 0x%X\n"
, ( UINT ) ( uintptr_t ) lpFunctionAddress );
const HANDLE hThreadCreationResult
=
CreateRemoteThread ( hTargetProcess, nullptr,
0
, ( LPTHREAD_START_ROUTINE ) lpFunctionAddress, lpPathAddress,
0
, nullptr );
if
( hThreadCreationResult
=
=
INVALID_HANDLE_VALUE ) {
printf (
"An error is occured when trying to create the thread in the target process.\n"
);
return
-
1
;
}
WaitForSingleObject ( hThreadCreationResult, INFINITE );
printf (
"DLL Injected !\n"
);
return
0
;
}