当前位置:网站首页>Shellcode writing (unfinished)
Shellcode writing (unfinished)
2022-07-28 15:20:00 【[email protected]】
One : basic
about shellcode Come on , It's independent , It can be attached to various processes , So every sentence of his code is independent , Only with shellcode The code in is related , It is isolated from external code .
It's generating exe When , The compiler turns the function into the entry address that really realizes the function of this function ( Absolute address ), because shellcode Is attached to other processes , Each process is different , This address may be for other functions , So the use of absolute address does not meet . So you need to call dynamically , Get the address of the function first , In the process of execution .
Similar to this :
GetProcAddress(LoadLibraryA(“kernel32.dll”), “CreateFileA”);
however ,LoadLibraryA And GetProcAddress You also need to get the address of , stay windows Under the operating system , Every process will be right ntdll、kernel32、kernelbase Do an internal loading of the system , So just look for the address loaded internally , You can get LoadLibraryA(“kernel32.dll”) The address of .
1, obtain LoadLibraryA(“kernel32.dll”)
FS The register points to the of the currently active thread TEB structure ( Thread structure )
The offset explain
000 Point to SEH Chain pointer
004 Top of thread stack
008 At the bottom of the thread stack
00C SubSystemTib
010 FiberData
014 ArbitraryUserPointer
018 FS The mirror address of the segment register in memory
020 process PID
024 Threads ID
02C Pointer to thread local storage
030 PEB Structure address ( Process structure )
034 Last error number
Stole a picture :
Modules are in memory order :
__declspec(naked) DWORD getKernel32()
{
__asm
{
mov eax,fs:[30h] // obtain PEB
mov eax,[eax+0ch] //PEB_LDR_DATA
mov eax,[eax+14h] // obtain inmemoryodermodulelist, The first module
mov eax,[eax]
mov eax,[eax] // The third module kernel32.dll
mov eax,[eax+10h] // obtain Baseaddress,kernel The base of
ret
}
}
2, obtain GetProcAddress Address
GetProcAddress This function is also in kernel32 in ,
FARPROC _GetProcAddress(HMODULE hModuleBase)
{
PIMAGE_DOS_HEADER lpDosHeader = (PIMAGE_DOS_HEADER)hModuleBase;
PIMAGE_NT_HEADERS32 lpNtHeader = (PIMAGE_NT_HEADERS)((DWORD)hModuleBase + lpDosHeader->e_lfanew);
if (!lpNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size){
return NULL;
}
if (!lpNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress) {
return NULL;
}
PIMAGE_EXPORT_DIRECTORY lpExports = (PIMAGE_EXPORT_DIRECTORY)((DWORD)hModuleBase + (DWORD)lpNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
PDWORD lpdwFunName = (PDWORD)((DWORD)hModuleBase + (DWORD)lpExports->AddressOfNames);
PWORD lpword = (PWORD)((DWORD)hModuleBase + (DWORD)lpExports->AddressOfNameOrdinals);
PDWORD lpdwFunAddr = (PDWORD)((DWORD)hModuleBase + (DWORD)lpExports->AddressOfFunctions);
DWORD dwLoop = 0;
FARPROC pRet = NULL;
for (; dwLoop <= lpExports->NumberOfNames - 1; dwLoop++) {
char* pFunName = (char*)(lpdwFunName[dwLoop] + (DWORD)hModuleBase);
if (pFunName[0] == 'G'&&
pFunName[1] == 'e'&&
pFunName[2] == 't'&&
pFunName[3] == 'P'&&
pFunName[4] == 'r'&&
pFunName[5] == 'o'&&
pFunName[6] == 'c'&&
pFunName[7] == 'A'&&
pFunName[8] == 'd'&&
pFunName[9] == 'd'&&
pFunName[10] == 'r'&&
pFunName[11] == 'e'&&
pFunName[12] == 's'&&
pFunName[13] == 's')
{
pRet = (FARPROC)(lpdwFunAddr[lpword[dwLoop]] + (DWORD)hModuleBase);
break;
}
}
return pRet;
}
3, String fragmentation
A string constant is a fixed address , Mentioned before , To avoid using absolute addresses , Use character arrays instead , And finally remember to add truncated characters , Avoid crossing borders .
4, Function generation location
In a single file , The order of function address is related to the order of function definition during code editing .
In multiple files , In this vcxproj below .
Two , To write shellcode
Basic framework :
#include "pch.h"
#include <windows.h>
#include <stdio.h>
FARPROC getProcAddress(HMODULE hModuleBase);
DWORD getKernel32();
int EntryMain()
{
// Declaration definition GetProcAddress
typedef FARPROC(WINAPI *FN_GetProcAddress)(
_In_ HMODULE hModule,
_In_ LPCSTR lpProcName
);
// obtain GetProcAddress Real address
FN_GetProcAddress fn_GetProcAddress = (FN_GetProcAddress)getProcAddress((HMODULE)getKernel32());
// Declaration definition CreateFileA
typedef HANDLE(WINAPI *FN_CreateFileA)(
__in LPCSTR lpFileName,
__in DWORD dwDesiredAccess,
__in DWORD dwShareMode,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
__in DWORD dwCreationDisposition,
__in DWORD dwFlagsAndAttributes,
__in_opt HANDLE hTemplateFile
);
// Future replacements , Get all addresses dynamically
//FN_CreateFileA fn_CreateFileA = (FN_CreateFileA)GetProcAddress(LoadLibrary("kernel32.dll"), "CreateFileA");
// The string with quotation marks is scattered
char xyCreateFile[] = {
'C','r','e','a','t','e','F','i','l','e','A',0 };
// Get dynamic CreateFile The address of
FN_CreateFileA fn_CreateFileA = (FN_CreateFileA)fn_GetProcAddress((HMODULE)getKernel32(), xyCreateFile);
char xyNewFile[] = {
'1','.','t','x','t','\0' };
fn_CreateFileA(xyNewFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
}
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/209/202207281415334995.html
边栏推荐
猜你喜欢

crmeb pro2.2即将增加的功能都有哪些?

HJS-DE1/2时间继电器

Crmeb v4.3 deployment process

Xiaobai can understand the 35 necessary questions in MySQL interview

svg 验证码识别体验

The second 1024, come on!

Publish raspberry pie web page with cpolar (release of apache2 web page)

JWY-32B电压继电器
![PMP [agile textbook + full truth simulation question]. After the exam on June 25, agile has become the top priority](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
PMP [agile textbook + full truth simulation question]. After the exam on June 25, agile has become the top priority
![What is the difference between UTF-8, utf-16 and UTF-32 character encoding? [graphic explanation]](/img/a9/336390db64d871fa1655800c1e0efc.png)
What is the difference between UTF-8, utf-16 and UTF-32 character encoding? [graphic explanation]
随机推荐
模板注入总结
每日一题(回溯)
Collation of MySQL error prone knowledge points (to be updated)
No files or folders found to process
边缘技术和小程序容器在智能家居中的应用
mysql 8.0常用(持续更新)
Mlx90640 infrared thermal imager sensor module development notes (VIII)
An idea of modifying vertex height with shader vertex shader
Introduction to mqtt protocol
一文看懂CRMEB开源在线教育知知识付费系统
Iframe tag
苹果iPhone手机APP应用图标隐藏怎么找回恢复显示在iPhone苹果手机桌面显示被隐藏的应用APP图标到iPhone苹果手机桌面?
3564. 日期类
DataTables warning: table id=campaignTable - Cannot reinitialise DataTable.解决
Install MOSEK, license installation location search
使用cpolar发布树莓派网页(apache2的安装测试)
Knowledge payment open source system
Wonderful frog -- how simple can it be to abandon the float and use the navigation bar set by the elastic box
使用cpolar发布树莓派网页(apache2网页的发布)
JY-7GA/1电压继电器