//Netstat -anb
#pragma once
#pragma once
#include <Windows.h>
#include <Psapi.h>
#include <Iprtrmib.h>
#include <iostream>
#pragma comment(lib,"Iphlpapi.Lib")
#pragma comment(lib,"Psapi.lib")
#pragma comment(lib,"WS2_32.lib")
using namespace std;
typedef struct{
DWORD dwState; //连接状态
DWORD dwLocalAddr; //本地地址
DWORD dwLocalPort; //本地端口
DWORD dwRemoteAddr; //远程地址
DWORD dwRemotePort; //远程端口
DWORD dwProcessId; //进程标识
}MIB_TCPEXROW,*PMIB_TCPEXROW;
typedef struct{
DWORD dwLocalAddr; //本地地址
DWORD dwLocalPort; //本地端口
DWORD dwProcessId; //进程标识
}MIB_UDPEXROW,*PMIB_UDPEXROW;
typedef struct{
DWORD dwState; //连接状态
DWORD dwLocalAddr; //本地地址
DWORD dwLocalPort; //本地端口
DWORD dwRemoteAddr; //远程地址
DWORD dwRemotePort; //远程端口
DWORD dwProcessId; //进程标识
DWORD Unknown; //待定标识
}MIB_TCPEXROW_VISTA,*PMIB_TCPEXROW_VISTA;
typedef struct{
DWORD dwNumEntries;
MIB_TCPEXROW table[ANY_SIZE];
}MIB_TCPEXTABLE,*PMIB_TCPEXTABLE;
typedef struct{
DWORD dwNumEntries;
MIB_TCPEXROW_VISTA table[ANY_SIZE];
}MIB_TCPEXTABLE_VISTA,*PMIB_TCPEXTABLE_VISTA;
typedef struct{
DWORD dwNumEntries;
MIB_UDPEXROW table[ANY_SIZE];
}MIB_UDPEXTABLE,*PMIB_UDPEXTABLE;
//enum OSVersion {XP,VISTA,OTHER};
typedef DWORD (WINAPI *PFNAllocateAndGetTcpExTableFromStack)(
PMIB_TCPEXTABLE *pTcpTabel,
bool bOrder,
HANDLE heap,
DWORD zero,
DWORD flags
);
typedef DWORD (WINAPI *PFNAllocateAndGetUdpExTableFromStack)(
PMIB_UDPEXTABLE *pUdpTable,
bool bOrder,
HANDLE heap,
DWORD zero,
DWORD flags
);
typedef DWORD (WINAPI *_InternalGetTcpTable2)(
PMIB_TCPEXTABLE_VISTA *pTcpTable_Vista,
HANDLE heap,
DWORD flags
);
typedef DWORD (WINAPI *_InternalGetUdpTableWithOwnerPid)(
PMIB_UDPEXTABLE *pUdpTable,
HANDLE heap,
DWORD flags
);
//************************************
// Method: GetProcInfo
// FullName: GetProcInfo
// Access: public
// Returns: DWORD if success,return PID;else,return -1
// Qualifier:
// Parameter: DWORD type UDP==0 TCP==1
// Parameter: DWORD port
// Parameter: LPCTSTR lpExeName
// Parameter: LPCTSTR lpExePath
//************************************
DWORD GetProcInfo(BOOL type,DWORD port,LPWSTR lpExeName,LPWSTR lpExePath)
{
HMODULE hModule = LoadLibrary(L"iphlpapi.dll");
if (hModule == NULL)
return -1;
if (type)
{
//声明未文档函数
PFNAllocateAndGetTcpExTableFromStack pAllcoteAndGetTcpExTableFromStack;
pAllcoteAndGetTcpExTableFromStack = (PFNAllocateAndGetTcpExTableFromStack)GetProcAddress(hModule,"AllocateAndGetTcpExTableFromStack");
if (pAllcoteAndGetTcpExTableFromStack != NULL)
{
PMIB_TCPEXTABLE pTcpExTable;
if (pAllcoteAndGetTcpExTableFromStack(&pTcpExTable,TRUE,GetProcessHeap(),2,2) != 0)
return -1;
for (UINT i=0;i<pTcpExTable->dwNumEntries;i++)
{
int temp = ntohs(0x0000FFFF&pTcpExTable->table[i].dwLocalPort);
if (port == temp)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,pTcpExTable->table[i].dwProcessId);
GetModuleBaseName(hProcess,NULL,lpExeName,MAX_PATH);
GetModuleFileNameEx(hProcess,NULL,lpExePath,MAX_PATH);
return pTcpExTable->table[i].dwProcessId;
}
}
return -1;
}
else
{
PMIB_TCPEXTABLE_VISTA pTcpTable_Vista;
_InternalGetTcpTable2 pGetTcpTable = (_InternalGetTcpTable2)GetProcAddress(hModule,"InternalGetTcpTable2");
if (pGetTcpTable == NULL)
return -1;
if (pGetTcpTable(&pTcpTable_Vista,GetProcessHeap(),1))
return -1;
for (UINT i=0;i<pTcpTable_Vista->dwNumEntries;i++)
{
if (port == ntohs(0x0000FFFF& pTcpTable_Vista->table[i] .dwLocalPort))
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,pTcpTable_Vista->table[i].dwProcessId);
GetModuleBaseName(hProcess,NULL,lpExeName,MAX_PATH);
GetModuleFileNameEx(hProcess,NULL,lpExePath,MAX_PATH);
return pTcpTable_Vista->table[i].dwProcessId;
}
}
return -1;
}
}
else
{
//声明未文档函数
//UDP端口信息结构体
PMIB_UDPEXTABLE pUdpExTable;
PFNAllocateAndGetUdpExTableFromStack pAllcoteAndGetUdpExTableFromStack;
pAllcoteAndGetUdpExTableFromStack = (PFNAllocateAndGetUdpExTableFromStack)GetProcAddress(hModule,"AllocateAndGetUdpExTableFromStack");
if (pAllcoteAndGetUdpExTableFromStack != NULL)
{
if (pAllcoteAndGetUdpExTableFromStack(&pUdpExTable,TRUE,GetProcessHeap(),2,2) != 0)
return -1;
for (UINT i=0;i<pUdpExTable->dwNumEntries;i++)
{
if (port == ntohs(0x0000FFFF&pUdpExTable->table[i].dwLocalPort))
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,pUdpExTable->table[i].dwProcessId);
GetModuleBaseName(hProcess,NULL,lpExeName,MAX_PATH);
GetModuleFileNameEx(hProcess,NULL,lpExePath,MAX_PATH);
return pUdpExTable->table[i].dwProcessId;
}
}
return -1;
}
else
{
_InternalGetUdpTableWithOwnerPid pGetUdpTable;
pGetUdpTable = (_InternalGetUdpTableWithOwnerPid)GetProcAddress(hModule,"InternalGetUdpTableWithOwnerPid");
if (pGetUdpTable != NULL)
{
if (pGetUdpTable(&pUdpExTable,GetProcessHeap(),1))
return -1;
for (UINT i=0;i<pUdpExTable->dwNumEntries;i++)
{
int temp=ntohs(0x0000FFFF&pUdpExTable->table[i].dwLocalPort);
if (temp==8000)
{
printf("debuging");
}
if (port == temp )
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,pUdpExTable->table[i].dwProcessId);
GetModuleBaseName(hProcess,NULL,lpExeName,MAX_PATH);
GetModuleFileNameEx(hProcess,NULL,lpExePath,MAX_PATH);
return pUdpExTable->table[i].dwProcessId;
}
}
}
return -1;
}
}
}
int main()
{
WCHAR szExeName[MAX_PATH] = {0};
WCHAR szExePath[MAX_PATH] = {0};
int type,port;
cout<<"TCP==1 UDP==0"<<endl
<<"Protocol:";
cin>>type;
cout<<"Port:";cin>>port;
DWORD dwPid = GetProcInfo(type,port,szExeName,szExePath);
if (dwPid != -1)
{
wcout<<dwPid<<"/t"<<szExeName<<"/t"<<szExePath<<endl;
}
else
{
cout<<"Error!"<<endl;
}
return 0;
}