WINDOWS下獲得連接埠對應的進程名1

來源:互聯網
上載者:User
 

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
typedef struct _HandleInfo
{
USHORT wPid;
USHORT wCreatorBackTraceIndex;
BYTE   objType;
BYTE   handleAttibs;
USHORT handleOffset;
DWORD  dwKeObject;
ULONG  dwGrantedAccess;

}HANDLEINFO, *PHANDLEINFO;

int main(int argc, char* argv[])
{
WSADATA wd;
unsigned int ret = WSAStartup(0x0202, &wd);

typedef DWORD (WINAPI *PQuerySystemInformation)(DWORD, PBYTE, DWORD, PDWORD);

PQuerySystemInformation pQuerySystemInformation = (PQuerySystemInformation)GetProcAddress(GetModuleHandle("ntdll.dll"), "ZwQuerySystemInformation");
if(pQuerySystemInformation == NULL)
{
MessageBox(NULL, "Can't find NtQuerySystemInformation int Ntdll.dll!", "Failed", 0);
return 1;
}

BYTE *buf = new BYTE[20];
PHANDLEINFO phandleinfo = NULL;
DWORD dwRetSize = 0;
DWORD dwNum = 0;
unsigned int i;

ret = pQuerySystemInformation(16, buf,20,&dwRetSize);
if(0 != ret)
{
if(dwRetSize > 0)
{
//dwNum = dwRetSize/sizeof(HANDLEINFO);
delete [] buf;
buf = new BYTE[dwRetSize];
ret = pQuerySystemInformation(16, buf, dwRetSize, &dwRetSize);
if(0 != ret)
{
printf("Can't get any handles!/n");
goto end;
}
}
else
{
goto end;
}
}
dwNum = *(DWORD*)buf;
phandleinfo = (PHANDLEINFO)(buf+4);

for(i=0;i<dwNum;i++)
{
//static int iCount = 0;
//static WORD wPid = -1;
HANDLEINFO *pSeek = phandleinfo + i;
if((pSeek->objType==0x1a) && (pSeek->wPid))
{
//iCount++;
//wPid = pSeek->wPid;

HANDLE hSrcProcess;
hSrcProcess = OpenProcess(PROCESS_ALL_ACCESS,TRUE, pSeek->wPid);
if(hSrcProcess == NULL)
continue;

__try
{

 

SOCKET hSock;
ret = DuplicateHandle(hSrcProcess, (HANDLE)(pSeek->handleOffset), GetCurrentProcess(), (HANDLE *)&hSock, STANDARD_RIGHTS_REQUIRED,TRUE,0);

if(ret == 0)
{
int errcode = GetLastError();
continue;
}
sockaddr_in in = {0};
in.sin_family = AF_INET;
int dwSize = sizeof(in);
if(SOCKET_ERROR != getsockname(hSock, (sockaddr *)&in, &dwSize))
{
char name[0x100] = {0};

HMODULE hDll = LoadLibrary("psapi.dll");
typedef DWORD (WINAPI *PGETMODULEFILENAMEEX)(HANDLE, HMODULE, LPTSTR, DWORD);
//typedef BOOL (WINAPI *PENUMPROCESSMODULES)(HANDLE, HMODULE*, DWORD, LPDWORD);
PGETMODULEFILENAMEEX pfunc = (PGETMODULEFILENAMEEX)GetProcAddress(hDll, "GetModuleFileNameExA");
//PENUMPROCESSMODULES penum = (PENUMPROCESSMODULES)GetProcAddress(hDll, "EnumProcessModules");
//DWORD dwCb, dwRet;
//HMODULE module[1000];
//ret = penum(hSrcProcess

pfunc(hSrcProcess, NULL, name, 0x100);

FreeLibrary(hDll);

printf("socket:%4d  port:%4d  PID:%4d(%s)/n", pSeek->handleOffset, ntohs(in.sin_port), pSeek->wPid, name);

}
}
__finally
{
CloseHandle(hSrcProcess);
}
}

}

getchar();

end:
delete [] buf;
return 0;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.