Vc ++ network security programming example (20) Trojan guard detects Data ports and processes

Source: Internet
Author: User

When we conduct system security protection, we need to prevent Trojans, and there is a network connection to the Trojan. We detect the data port and process mode to prevent Trojans.

We use VC ++ to detect Data ports and processes. For more information, see code implementation and annotations.

# Include <windows. h> <br/> # include <Tlhelp32.h> <br/> # include <winsock. h> <br/> # include <stdio. h> <br/> # pragma comment (lib, "ws2_32.lib") </p> <p> // TCP-related structure <br/>. <br/> typedef struct tagMIB_TCPEXROW {<br/> DWORD dwState; // connection status. <br/> DWORD dwLocalAddr; // address of the local computer. <br/> DWORD dwLocalPort; // local computer port. <br/> DWORD dwRemote Addr; // Remote Computer address. <br/> DWORD dwRemotePort; // remote computer port. <br/> DWORD dwProcessId; <br/>} MIB_TCPEXROW, * PMIB_TCPEXROW; </p> <p> typedef struct tagMIB_TCPEXTABLE {<br/> DWORD dwNumEntries; <br/> MIB_TCPEXROW table [100]; // array variable of any size. <br/>} MIB_TCPEXTABLE, * PMIB_TCPEXTABLE; </p> <p> // UDP-related structure <br/>. <br/> typedef struct tagMIB_UD PEXROW {<br/> DWORD dwLocalAddr; // address of the local computer. <br/> DWORD dwLocalPort; // local computer port. <br/> DWORD dwProcessId; <br/>} MIB_UDPEXROW, * PMIB_UDPEXROW; </p> <p> typedef struct tagMIB_UDPEXTABLE {<br/> DWORD dwNumEntries; <br/> MIB_UDPEXROW table [100]; // array variable of any size. <br/>} MIB_UDPEXTABLE, * PMIB_UDPEXTABLE; </p> <p> // use the lplpapi. in dll Number prototype definition. <br/> typedef DWORD (WINAPI * PALLOCATE_AND_GET_TCPEXTABLE_FROM_STACK) (<br/> PMIB_TCPEXTABLE * pTcpTable, // connect table buffer. <br/> BOOL bOrder, <br/> HANDLE heap, <br/> DWORD zero, <br/> DWORD flags <br/> ); </p> <p> typedef DWORD (WINAPI * PALLOCATE_AND_GET_UDPEXTABLE_FROM_STACK) (<br/> PMIB_UDPEXTABLE * pUdpTable, // connect table buffer. <br/> BOOL bOrder, <br/> HANDLE heap, <br/> DWORD zero, <br/> DWORD flags <br/> ); </p> <p> st Atic metadata <br/> pAllocateAndGetTcpExTableFromStack = NULL; </p> <p> static PALLOCATE_AND_GET_UDPEXTABLE_FROM_STACK <br/> pAllocateAndGetUdpExTableFromStack = NULL; </p> <p> // --------------------------------------------------------------------------- <br/>/< br/> // Possible TCP endpoint status. <br/> // <br/> static char TcpState [] [32] ={< br/> TEXT ("??? "), <Br/> TEXT (" CLOSED "), <br/> TEXT (" LISTENING "), <br/> TEXT (" SYN_SENT "), <br/> TEXT ("SYN_RCVD"), <br/> TEXT ("ESTABLISHED"), <br/> TEXT ("FIN_WAIT1 "), <br/> TEXT ("FIN_WAIT2"), <br/> TEXT ("CLOSE_WAIT"), <br/> TEXT ("CLOSING "), <br/> TEXT ("LAST_ACK"), <br/> TEXT ("TIME_WAIT"), <br/> TEXT ("DELETE_TCB") <br/> }; </p> <p> // --------------------------------------------------------------------------- <br/> // generate an IP address String. <br/> // <br/> PCHAR GetIP (unsigned int ipaddr) <br/> {<br/> static char pIP [20]; <br/> unsigned int nipaddr = htonl (ipaddr); <br/> sprintf (pIP, "% d. % d. % d. % d ", <br/> (nipaddr> 24) & 0xFF, <br/> (nipaddr> 16) & 0xFF, <br/> (nipaddr> 8) & 0xFF, <br/> (nipaddr) & 0xFF); <br/> return pIP; <br/>}</p> <p> // ------------------------------------------------------------------------- <br/>/< br/> // obtain the full file name by the process number. <br // <Br/> char * ProcessPidToName (DWORD ProcessId) <br/>{< br/> HANDLE hProcessSnap = creatw.lhelp32snapshot (TH32CS_SNAPPROCESS, 0 ); <br/> PROCESSENTRY32 processEntry = {0}; <br/> processEntry. dwSize = sizeof (PROCESSENTRY32); <br/> static char ProcessName [256]; </p> <p> lstrcpy (ProcessName, "Idle "); <br/> if (hProcessSnap = INVALID_HANDLE_VALUE) <br/> return ProcessName; </p> <p> BOOL bRet = Process32First (HProcessSnap, & processEntry); </p> <p> while (bRet) <br/>{< br/> if (processEntry. th32ProcessID = ProcessId) <br/>{< br/> MODULEENTRY32 me32 = {0 }; <br/> me32.dwSize = sizeof (MODULEENTRY32 ); <br/> HANDLE hModuleSnap = createconlhelp32snapshot <br/> (TH32CS_SNAPMODULE, processEntry. th32ProcessID); </p> <p> Module32First (hModuleSnap, & me32); // obtain the full path. <br/> lstrcpy (ProcessName, me32.szExePath); <br/> CloseHa Ndle (hProcessSnap); <br/> return ProcessName; <br/>}</p> <p> bRet = Process32Next (hProcessSnap, & processEntry ); <br/>}</p> <p> CloseHandle (hProcessSnap); <br/> return ProcessName; <br/>}</p> <p> // display the association between processes, ports, and file names. <br/>. <br/> // <br/> void DisplayPort () <br/> {<br/> DWORD I; <br/> PMIB_TCPEXTABLE TCPExTable; <br/> PMIB_UDPEX TABLE UDPExTable; <br/> char szLocalAddress [256]; <br/> char szRemoteAddress [256]; </p> <p> if (pAllocateAndGetTcpExTableFromStack (<br/> & TCPExTable, TRUE, GetProcessHeap (), 2, 2 )) <br/>{< br/> printf ("AllocateAndGetTcpExTableFromStack Error! \ N "); <br/> return; <br/>}</p> <p> if (pAllocateAndGetUdpExTableFromStack <br/> (& UDPExTable, TRUE, GetProcessHeap (), 2, 2) <br/>{< br/> printf ("AllocateAndGetUdpExTableFromStack Error !. \ N "); <br/> return; <br/>}</p> <p> // obtain the TCP list. <br/> printf ("%-6 s %-22 s %-22 s %-11 s % s \ n", <br/> TEXT ("Proto "), <br/> TEXT ("Local Address"), <br/> TEXT ("Foreign Address"), <br/> TEXT ("State "), <br/> TEXT ("Process"); </p> <p> for (I = 0; I <TCPExTable-> dwNumEntries; I ++) <br/>{< br/> sprintf (szLocalAddress, "% s: % d", <br/> GetIP (TCPExTable-> table [I]. dwLocalAddr), <br/> htons (WORD) TCPExTable-> table [I]. dwLocalPort) ); </P> <p> sprintf (szRemoteAddress, "% s: % d", <br/> GetIP (TCPExTable-> table [I]. dwRemoteAddr), <br/> htons (WORD) TCPExTable-> table [I]. dwRemotePort); </p> <p> printf ("%-6 s %-22 s %-22 s %-11 s % s: % d \ n ", TEXT ("TCP"), <br/> szLocalAddress, szRemoteAddress, <br/> TcpState [TCPExTable-> table [I]. dwState], <br/> ProcessPidToName (TCPExTable-> table [I]. dwProcessId), <br/> TCPExTable-> table [I]. dwProcessId); <br/>}</p> <p> // obtain the U DP list. <br/> for (I = 0; I <UDPExTable-> dwNumEntries; I ++) <br/>{< br/> sprintf (szLocalAddress, "% s: % d ", <br/> GetIP (UDPExTable-> table [I]. dwLocalAddr), <br/> htons (WORD) UDPExTable-> table [I]. dwLocalPort); </p> <p> sprintf (szRemoteAddress, "% s ","*:*"); </p> <p> printf ("%-6 s %-22 s %-33 s % s: % d \ n", TEXT ("UDP "), <br/> szLocalAddress, szRemoteAddress, <br/> ProcessPidToName (UDPExTable-> table [I]. dwProcessId), <br/> UDPExTable-> table [I]. dwProcessId ); <br/>}</p> <p> // functions <br/> // main functions of processes and port-related programs. <br/> // <br/> void main () <br/> {<br/> WSADATA WSAData; <br/> if (WSAStartup (MAKEWORD (1, 1), & WSAData) <br/>{< br/> printf ("WSAStartup error! \ N "); <br/> return; <br/>}</p> <p> HMODULE hIpDLL = LoadLibrary (" iphlpapi. dll "); <br/> if (! HIpDLL) <br/> return; </p> <p> pAllocateAndGetTcpExTableFromStack = <br/> GetProcAddress (hIpDLL, "AllocateAndGetTcpExTableFromStack "); </p> <p> pAllocateAndGetUdpExTableFromStack = <br/> (callback) <br/> GetProcAddress (hIpDLL, "AllocateAndGetUdpExTableFromStack "); </p> <p> // display the association between processes and ports. <br/> DisplayPort (); </p> <p> FreeLibrary (hIpDLL); <br/> WSACleanup (); </p> <p> getchar (); // pause. <br/>}< br/>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.