-
使用Release模式。近来编译器的Debug模式可能产生逆序的函数,并且会插入许多与位置相关的调用。
-
禁用优化。编译器会默认优化那些没有使用的函数,而那可能正是我们所需要的。
-
禁用栈缓冲区安全检查(/Gs)。在函数头尾所调用的栈检查函数,存在于二进制文件的某个特定位置,导致输出的函数不能重定位,这对shellcode是无意义的。
以上为在vs中的设置.
#include "stdafx.h"
#include <windows.h>#include <winbase.h>
#include <stdio.h>
FILE *output_file1 = NULL;
void xLog(const CHAR *szfmt, ...)
{
CHAR buff[2048] = {0};
memset(buff,L'\0',2048);
va_list arglist;
va_start( arglist, szfmt );
vsprintf_s( buff, 2048*sizeof(CHAR), szfmt, arglist );
va_end( arglist );
OutputDebugStringA(buff);
fwrite(buff,strlen(buff)+1,1, output_file1);
}
void shell_code()
{
for (int i =0; i < 100; i++);
}
void __declspec(naked) END_SHELLCODE(void) {}
int _tmain(int argc, _TCHAR* argv[])
{
output_file1 = fopen("shellcode2.bin", "w");
if (output_file1 == NULL)
{
return 0;
}
BYTE* p = (BYTE*)&shell_code;
int icount = (int)END_SHELLCODE - (int)shell_code;
for(int i=0;i<icount;i++)
{
xLog("0x%0.2X,",p[i]);
if (( i> 0) && ((i+1)%10==0) )
{
xLog("\r\n");
}
}
fclose(output_file1);
shell_code();
return 0;
}