找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3530|回复: 1

ansi c x64 shellcode template

[复制链接]

30

主题

210

回帖

2778

积分

用户组: 版主

UID
1821
精华
7
威望
69 点
宅币
2159 个
贡献
206 次
宅之契约
0 份
在线时间
480 小时
注册时间
2016-7-12
发表于 2016-8-9 19:03:15 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
ansi c语言 x64的shellcode 模板 未详细测试

  1. #ifndef BYTE
  2. #define BYTE unsigned __int8
  3. #endif
  4. #ifndef WORD
  5. #define WORD unsigned __int16
  6. #endif
  7. #ifndef LONG
  8. #define LONG unsigned __int32
  9. #endif
  10. #ifndef DWORD
  11. #define DWORD unsigned __int32
  12. #endif
  13. #ifndef ULONGLONG
  14. #define ULONGLONG unsigned __int64
  15. #endif
  16. #ifndef IMAGE_NUMBEROF_DIRECTORY_ENTRIES
  17. #define IMAGE_NUMBEROF_DIRECTORY_ENTRIES    16

  18. #define IMAGE_DIRECTORY_ENTRY_EXPORT          0   // Export Directory
  19. #define IMAGE_DIRECTORY_ENTRY_IMPORT          1   // Import Directory
  20. #define IMAGE_DIRECTORY_ENTRY_RESOURCE        2   // Resource Directory
  21. #define IMAGE_DIRECTORY_ENTRY_EXCEPTION       3   // Exception Directory
  22. #define IMAGE_DIRECTORY_ENTRY_SECURITY        4   // Security Directory
  23. #define IMAGE_DIRECTORY_ENTRY_BASERELOC       5   // Base Relocation Table
  24. #define IMAGE_DIRECTORY_ENTRY_DEBUG           6   // Debug Directory
  25. //      IMAGE_DIRECTORY_ENTRY_COPYRIGHT       7   // (X86 usage)
  26. #define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE    7   // Architecture Specific Data
  27. #define IMAGE_DIRECTORY_ENTRY_GLOBALPTR       8   // RVA of GP
  28. #define IMAGE_DIRECTORY_ENTRY_TLS             9   // TLS Directory
  29. #define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG    10   // Load Configuration Directory
  30. #define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT   11   // Bound Import Directory in headers
  31. #define IMAGE_DIRECTORY_ENTRY_IAT            12   // Import Address Table
  32. #define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT   13   // Delay Load Import Descriptors
  33. #define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14   // COM Runtime descriptor

  34. #endif
  35. #ifndef _IMAGE_DOS_HEADER
  36. typedef struct _IMAGE_DOS_HEADER {      // DOS .EXE header
  37.     WORD   e_magic;                     // Magic number
  38.     WORD   e_cblp;                      // Bytes on last page of file
  39.     WORD   e_cp;                        // Pages in file
  40.     WORD   e_crlc;                      // Relocations
  41.     WORD   e_cparhdr;                   // Size of header in paragraphs
  42.     WORD   e_minalloc;                  // Minimum extra paragraphs needed
  43.     WORD   e_maxalloc;                  // Maximum extra paragraphs needed
  44.     WORD   e_ss;                        // Initial (relative) SS value
  45.     WORD   e_sp;                        // Initial SP value
  46.     WORD   e_csum;                      // Checksum
  47.     WORD   e_ip;                        // Initial IP value
  48.     WORD   e_cs;                        // Initial (relative) CS value
  49.     WORD   e_lfarlc;                    // File address of relocation table
  50.     WORD   e_ovno;                      // Overlay number
  51.     WORD   e_res[4];                    // Reserved words
  52.     WORD   e_oemid;                     // OEM identifier (for e_oeminfo)
  53.     WORD   e_oeminfo;                   // OEM information; e_oemid specific
  54.     WORD   e_res2[10];                  // Reserved words
  55.     LONG   e_lfanew;                    // File address of new exe header
  56. } IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;
  57. #endif

  58. #ifndef IMAGE_FILE_HEADER
  59. typedef struct _IMAGE_FILE_HEADER {
  60.     WORD    Machine;
  61.     WORD    NumberOfSections;
  62.     DWORD   TimeDateStamp;
  63.     DWORD   PointerToSymbolTable;
  64.     DWORD   NumberOfSymbols;
  65.     WORD    SizeOfOptionalHeader;
  66.     WORD    Characteristics;
  67. } IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
  68. #endif
  69. #ifndef IMAGE_DATA_DIRECTORY
  70. typedef struct _IMAGE_DATA_DIRECTORY {
  71.     DWORD   VirtualAddress;
  72.     DWORD   Size;
  73. } IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
  74. #endif
  75. #ifndef IMAGE_OPTIONAL_HEADER64

  76. typedef struct _IMAGE_OPTIONAL_HEADER64 {
  77.     WORD        Magic;
  78.     BYTE        MajorLinkerVersion;
  79.     BYTE        MinorLinkerVersion;
  80.     DWORD       SizeOfCode;
  81.     DWORD       SizeOfInitializedData;
  82.     DWORD       SizeOfUninitializedData;
  83.     DWORD       AddressOfEntryPoint;
  84.     DWORD       BaseOfCode;
  85.     ULONGLONG   ImageBase;
  86.     DWORD       SectionAlignment;
  87.     DWORD       FileAlignment;
  88.     WORD        MajorOperatingSystemVersion;
  89.     WORD        MinorOperatingSystemVersion;
  90.     WORD        MajorImageVersion;
  91.     WORD        MinorImageVersion;
  92.     WORD        MajorSubsystemVersion;
  93.     WORD        MinorSubsystemVersion;
  94.     DWORD       Win32VersionValue;
  95.     DWORD       SizeOfImage;
  96.     DWORD       SizeOfHeaders;
  97.     DWORD       CheckSum;
  98.     WORD        Subsystem;
  99.     WORD        DllCharacteristics;
  100.     ULONGLONG   SizeOfStackReserve;
  101.     ULONGLONG   SizeOfStackCommit;
  102.     ULONGLONG   SizeOfHeapReserve;
  103.     ULONGLONG   SizeOfHeapCommit;
  104.     DWORD       LoaderFlags;
  105.     DWORD       NumberOfRvaAndSizes;
  106.     IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
  107. } IMAGE_OPTIONAL_HEADER64, *PIMAGE_OPTIONAL_HEADER64;

  108. #endif

  109. #ifndef IMAGE_NT_HEADERS64
  110. typedef struct _IMAGE_NT_HEADERS64 {
  111.     DWORD Signature;
  112.     IMAGE_FILE_HEADER FileHeader;
  113.     IMAGE_OPTIONAL_HEADER64 OptionalHeader;
  114. } IMAGE_NT_HEADERS64, *PIMAGE_NT_HEADERS64;
  115. #endif
  116. #ifndef IMAGE_EXPORT_DIRECTORY

  117. typedef struct _IMAGE_EXPORT_DIRECTORY {
  118.     DWORD   Characteristics;
  119.     DWORD   TimeDateStamp;
  120.     WORD    MajorVersion;
  121.     WORD    MinorVersion;
  122.     DWORD   Name;
  123.     DWORD   Base;
  124.     DWORD   NumberOfFunctions;
  125.     DWORD   NumberOfNames;
  126.     DWORD   AddressOfFunctions;     // RVA from base of image
  127.     DWORD   AddressOfNames;         // RVA from base of image
  128.     DWORD   AddressOfNameOrdinals;  // RVA from base of image
  129. } IMAGE_EXPORT_DIRECTORY, *PIMAGE_EXPORT_DIRECTORY;
  130. #endif


  131. #ifndef UNICODE_STRING
  132. typedef struct
  133. {
  134.   __int16 u;
  135.   __int16 m;
  136.   __int32 r;//align 8
  137.   __int16* B;
  138. }UNICODE_STRING;
  139. #endif

  140. /***********************************************************************/
  141. /* shell code start */
  142. /* linker command must append /MERGE:S_CODE=S_DATA /SECTION:S_DATA,RWE */
  143. /***********************************************************************/

  144. int shell_start();
  145. int s_ldrLoadDll();
  146. void GetRing3Base();
  147. int strlen(char *);
  148. int strcmp(char *,char *);

  149. __int64 GetProcAddress(__int64 base,char* FuncName);

  150. #pragma alloc_text(S_CODE,shell_start)
  151. #pragma alloc_text(S_CODE,s_ldrLoadDll)
  152. #pragma alloc_text(S_CODE,GetRing3Base)
  153. #pragma alloc_text(S_CODE,GetProcAddress)
  154. #pragma alloc_text(S_CODE,strlen)
  155. #pragma alloc_text(S_CODE,strcmp)


  156. /*shellcode Global DATA*/
  157. #define SHELLCODE_SEG "S_DATA"

  158. #pragma data_seg(SHELLCODE_SEG)
  159. typedef int (*_imp__LdrLoadDll)(PathToFile,Flags,ModuleFileName,ModuleHandle);
  160. _imp__LdrLoadDll pLdrLoadDll=0;
  161. __int64         k_Base=0;
  162. __int64         n_Base=0;

  163. char   sLdrLoadDll[]="LdrLoadDll";

  164. __int16  sUser32[]=L"user32.dll";

  165. #pragma data_seg()

  166. /*shlleocde entry*/
  167. int shell_start()
  168. {
  169.   pLdrLoadDll=(_imp__LdrLoadDll)GetProcAddress(n_Base,(char*)&sLdrLoadDll);
  170.   
  171.   return s_ldrLoadDll();//Used by GetThreadExitCode
  172. }

  173. /*get kernel32 and ntdll base*/
  174. void GetRing3Base()
  175. {
  176. __int64 p;
  177.    p=*(__int64*)(*(__int64*)(*(__int64 *)(__readgsqword(0x30)+0x60)+0x18)+0x30);
  178.    n_Base=*(__int64*)(p+0x10);
  179.    k_Base=*(__int64*)(*(__int64*)(*(__int64*)p)+0x10);
  180. }

  181. int strlen(char* s)
  182. {
  183.   int i=0;
  184.   for(;s[i++];);
  185.   return i;
  186. }

  187. int strcmp(char* s1,char* s2)
  188. {
  189.   int t,ta,tb;
  190.   t|=-1;
  191.   ta=strlen(s1);
  192.   tb=strlen(s2);
  193.   if (ta==tb)
  194.   {
  195.     t=ta;
  196.     do
  197.     {
  198.       --t;
  199.     }while (t>=0 && s1[t]==s2[t]);
  200.     t++;
  201.   }
  202.   return t;
  203. }

  204. /* */
  205. __int64 GetProcAddress(__int64 base,char* FuncName)
  206. {
  207.   __int64 addr=0;
  208.   __int32* AddressOfNames;
  209.   __int32* AddressOfFunctions;
  210.   __int16* AddressOfNameOrdinals;
  211.   int i,n,t;
  212.   char* Dst;
  213.   char* Src;
  214.   
  215.   IMAGE_DOS_HEADER*         DOS_HEADER;
  216.   IMAGE_NT_HEADERS64*       NT_HEADER;
  217.   IMAGE_OPTIONAL_HEADER64*  OptionalHeader;
  218.   IMAGE_EXPORT_DIRECTORY*   Export;
  219.   
  220.   DOS_HEADER=(IMAGE_DOS_HEADER*)(__int64)base;
  221.   if (DOS_HEADER->e_magic!='ZM') goto done;
  222.   
  223.   NT_HEADER = (IMAGE_NT_HEADERS64*)((__int64)DOS_HEADER +(__int64)DOS_HEADER->e_lfanew);
  224.   
  225.   if (NT_HEADER->Signature!='EP') goto done;
  226.   
  227.   OptionalHeader=&NT_HEADER->OptionalHeader;
  228.   
  229.   if (OptionalHeader->Magic!=0x20B) goto done;//pe 64
  230.   
  231.   Export = (IMAGE_EXPORT_DIRECTORY*)(\
  232.             (__int64)DOS_HEADER + \
  233.             (__int64)(OptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress)\
  234.             );
  235.   t=Export->NumberOfNames;
  236.   AddressOfNameOrdinals =(__int16*)((__int64)DOS_HEADER + (__int64)Export->AddressOfNameOrdinals);
  237.   AddressOfNames        =(__int32*)((__int64)DOS_HEADER + (__int64)Export->AddressOfNames);
  238.   AddressOfFunctions    =(__int32*)((__int64)DOS_HEADER + (__int64)Export->AddressOfFunctions);
  239.   Src=FuncName;
  240.   for (i=0;i<t;i++)
  241.   {
  242.     Dst=(char*)((__int64)DOS_HEADER + AddressOfNames[i]);
  243.     if (Dst[0]==Src[0]&& strcmp(Src,Dst)==0)
  244.     {
  245.       n=AddressOfNameOrdinals[i];
  246.       addr=(__int64)DOS_HEADER+AddressOfFunctions[n];
  247.       goto done;
  248.     }
  249.   }
  250. done:
  251.   return addr;
  252. }

  253. /*main proc*/
  254. int s_ldrLoadDll()
  255. {
  256.   __int64* hMod;
  257.   UNICODE_STRING sMod;
  258.   sMod.u=sizeof(sUser32)-sizeof(__int16);
  259.   sMod.m=sizeof(sUser32);
  260.   sMod.B=(__int16*)&sUser32;
  261.   
  262.   return (*pLdrLoadDll)(0,0,&sMod,&hMod);
  263. }

  264. #pragma data_seg(SHELLCODE_SEG)
  265. int         shell_end=0; //end sign
  266. #pragma data_seg()

  267. /* shell code End */



  268. mainCRTStartup()
  269. {
  270.   GetRing3Base();
  271.   printf("kernel32 base 0x%0I64X\n ntdll base 0x%0I64X\n",k_Base,n_Base);
  272.   printf("shellcode length = %d\n",&shell_end-(int*)shell_start);
  273.   
  274.   printf("LdrLoadDll addr = 0x%0I64X\n",GetProcAddress(n_Base,"LdrLoadDll"));
  275.   
  276.   
  277.   system("pause");
  278. }
复制代码



  1. @echo off
  2. :re
  3. cls
  4. echo /*********************************************/
  5. echo /                                             /
  6. echo /*********************************************/
  7. .\tools\AMD64\cl.exe .\src\hello_world.c /Fa"Debug\hello_world.asm" /Fo"Debug\hello_world.obj" /c /MD

  8. echo /*********************************************/
  9. echo /                                             /
  10. echo /*********************************************/


  11. .\tools\AMD64\link.exe .\Debug\hello_world.obj /MERGE:S_CODE=S_DATA /SECTION:S_DATA,RWE /LIBPATH:".\lib\win7\amd64" /LIBPATH:".\lib\Crt\amd64"  /OUT:"Debug\hello_world_amd64_win7.exe" /NOLOGO /SUBSYSTEM:CONSOLE /MACHINE:AMD64 "kernel32.lib"
  12. echo /*********************************************/
  13. echo /                                             /
  14. echo /*********************************************/


  15. pause
  16. goto re
  17. ;/driver /base:0x10000 /align:32 /subsystem:native
复制代码

本帖被以下淘专辑推荐:

回复

使用道具 举报

1111

主题

1651

回帖

7万

积分

用户组: 管理员

一只技术宅

UID
1
精华
244
威望
743 点
宅币
24235 个
贡献
46222 次
宅之契约
0 份
在线时间
2296 小时
注册时间
2014-1-26
发表于 2016-12-6 15:32:47 来自手机 | 显示全部楼层
前面那些定义,其实可以直接用windows.h的
回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-19 20:52 , Processed in 0.035203 second(s), 32 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表