//
==========================================================================
//
shellcode 部分代码
//
具体编写参考 yuange 2001年的c语言直接直接生成 shellcode, 你用汇编也行
//
===============================================================================
//
manal jmp to main()
char jmp_main[] =
"\xE9\0\0\0\0"
;
char original_entry[] =
"original_entry"
;
char inject_dllname[0x100] =
"inject_dllname"
;
char loadlibrary_str[] =
"LoadLibraryA"
;
int main(int argc, char* argv[])
{
_IMPORT_AT *IAT = (_IMPORT_AT *)_relocal(&g_IAT);
//
Initialize
import
address table by loadlibraryA
InitializeIAT(_GetLoadLibraryA());
IAT->_LoadLibraryA((LPCSTR)_relocal(inject_dllname));
//
sub
return
point, 5
unsigned long *entry_esp = (unsigned long *)&argc;
entry_esp --;
*entry_esp -= 5;
//
revert hooked entry point code
void *pentry = (void *)(*entry_esp);
void *entry_c = (void *)_relocal(original_entry);
IAT->_WriteProcessMemory(
IAT->_GetCurrentProcess(), pentry, entry_c, 5, NULL);
return
0;
}
//
==========================================================================
//
//
注入完整代码
//
//
=======================================================================
CWinApp theApp;
using namespace std;
UCHAR *_FixShellcode(LPCSTR lpszInjectFileName, ULONG *pEntry, LPCSTR lpszShellFileName, ULONG *pSize)
{
//
//
lpszInjectFileName
//
HMODULE hModule = LoadLibraryEx(
lpszInjectFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);
ASSERT(hModule);
unsigned char *Image = (unsigned char *)hModule;
PIMAGE_DOS_HEADER pimg_dos_h = (PIMAGE_DOS_HEADER)Image;
PIMAGE_NT_HEADERS pimg_nt_h = (PIMAGE_NT_HEADERS)
(Image+pimg_dos_h->e_lfanew);
ULONG AddressOfEntryPoint = pimg_nt_h->OptionalHeader.ImageBase +
pimg_nt_h->OptionalHeader.AddressOfEntryPoint;
char entrycode[5];
memcpy(entrycode, (Image+pimg_nt_h->OptionalHeader.AddressOfEntryPoint), 5);
///
汗! 调试的 时候居然会崩溃 在 ntdll
FreeLibrary(hModule);
//
//
lpszShellFileName 读取编译好的 Shellcode
//
HMODULE hModule2 = LoadLibraryEx(
lpszShellFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);
ASSERT(hModule2);
UCHAR *Image2 = (UCHAR *)hModule2;
PIMAGE_DOS_HEADER pImageDosHeader = (PIMAGE_DOS_HEADER)Image2;
PIMAGE_NT_HEADERS pImageNtHeader = (PIMAGE_NT_HEADERS)
(Image2 + pImageDosHeader->e_lfanew);
PIMAGE_SECTION_HEADER pImageSecHeader = (PIMAGE_SECTION_HEADER)
(Image2 + pImageDosHeader->e_lfanew + sizeof(IMAGE_NT_HEADERS));
ULONG sizeOfShell = pImageSecHeader->SizeOfRawData;
UCHAR *c = new UCHAR[sizeOfShell];
UCHAR *sectionAddress = (UCHAR *)
(Image2 + pImageSecHeader->VirtualAddress);
ULONG EntryOff = pImageNtHeader->OptionalHeader.AddressOfEntryPoint -
pImageSecHeader->VirtualAddress;
memcpy(c, sectionAddress, sizeOfShell);
FreeLibrary(hModule2);
//
//
Fix shell code
//
char *c2 = (char *)c;
ASSERT(c2[0] ==
'\xE9'
);
*(ULONG *)(&c2[1]) = EntryOff - 5;
//
For debug
//c2
[0] =
'\xCC'
;
while
(1)
{
if
(stricmp(c2,
"original_entry"
) == 0)
{
//
保存挂钩处代码到 shellcode
memcpy(c2, entrycode, 5);
}
if
(stricmp(c2,
"inject_dllname"
) == 0)
{
//
复制 dll 完整路径 !..............................................
memset(c2, 0x0,0x100);
strcpy(c2,
"dll.dll"
);
break
;
}
c2 ++;
}
//
*pEntry = AddressOfEntryPoint;
*pSize = sizeOfShell;
return
c;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
//
initialize MFC and print and error on failure
if
(!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
//
TODO: change error code to suit your needs
cerr << _T(
"Fatal Error: MFC initialization failed"
) << endl;
nRetCode = 1;
}
const char szApplicationName[MAX_PATH] =
"C:\\Program Files\\汗!.exe"
;
char lpszModuleFileName[MAX_PATH] = {0};
GetModuleFileNameA(NULL, lpszModuleFileName, MAX_PATH);
CString szModuleFileName(lpszModuleFileName);
szModuleFileName = szModuleFileName.Left(szModuleFileName.ReverseFind(
'\\'
));
//
Shellcode PE 文件
szModuleFileName +=
"\\c.bin"
;
UCHAR *c = NULL;
ULONG AddressOfEntryPoint;
ULONG len ;
c = _FixShellcode(szApplicationName, &AddressOfEntryPoint, szModuleFileName, &len);
//
//
//
char szCommandLine[0x400] = {0};
strcpy(szCommandLine, szApplicationName);
STARTUPINFO StartupInfo = {0};
StartupInfo.cb = sizeof(STARTUPINFO);
PROCESS_INFORMATION ProcessInformation;
//
//
前面说了 你打开进程 暂停 获取 .Eip 也行 挂钩处发生变化!
//
if
(!CreateProcess(szApplicationName, szCommandLine, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &StartupInfo, &ProcessInformation))
{
//
没释放资源 懒
return
FALSE;
}
LPVOID dst = VirtualAllocEx(ProcessInformation.hProcess, NULL, len, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
//
call
char call_c[5] =
"\xE8"
;
*((ULONG *)&call_c[1]) = (ULONG)dst - AddressOfEntryPoint - 5;
WriteProcessMemory(ProcessInformation.hProcess, (LPVOID)AddressOfEntryPoint, call_c, 5, NULL);
WriteProcessMemory(ProcessInformation.hProcess, (LPVOID)dst, c, len, NULL);
BOOL bRetCode = ResumeThread(ProcessInformation.hThread);
CloseHandle(ProcessInformation.hProcess);
CloseHandle(ProcessInformation.hThread);
delete[] c;
return
nRetCode;
}