|
22 | 22 | #include <conio.h> |
23 | 23 | #include <cassert> |
24 | 24 | #include <psapi.h> |
| 25 | +#include <iphlpapi.h> |
25 | 26 |
|
26 | 27 | #define windows_time_to_unix_epoch(x) ((x) - 116444736000000000LL) / 10000000LL |
27 | 28 | // The above macro converts Windows FILETIME to Unix epoch time in seconds. |
@@ -1668,7 +1669,61 @@ CloseHandle(hSnapshot); // we're only closing the handle until we finish messing |
1668 | 1669 | } |
1669 | 1670 | } |
1670 | 1671 |
|
1671 | | - |
| 1672 | +void FindProcessPorts(DWORD targetPid) { |
| 1673 | + // this function gets the ports that a process is listening to |
| 1674 | + // unfortunately, according to microsoft docs, this only works starting from windows xp sp2 :( |
| 1675 | + // so sorry for those of you using vanilla xp |
| 1676 | + // the docs in question: https://learn.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getextendedtcptable |
| 1677 | + |
| 1678 | + MIB_TCPTABLE_OWNER_PID* pTcpTable; |
| 1679 | + DWORD dwSize = 0; |
| 1680 | + DWORD dwRetVal = 0; |
| 1681 | + |
| 1682 | + dwRetVal = GetExtendedTcpTable(NULL, &dwSize, FALSE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0); |
| 1683 | + |
| 1684 | + if (dwRetVal == ERROR_INSUFFICIENT_BUFFER) { |
| 1685 | + pTcpTable = (MIB_TCPTABLE_OWNER_PID*)malloc(dwSize); |
| 1686 | + if (pTcpTable == NULL) { |
| 1687 | + return; |
| 1688 | + } |
| 1689 | + |
| 1690 | + dwRetVal = GetExtendedTcpTable(pTcpTable, &dwSize, FALSE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0); |
| 1691 | + |
| 1692 | + if (dwRetVal == NO_ERROR) { |
| 1693 | + // Collect all listening IP:port pairs first |
| 1694 | + std::vector<std::string> listening; |
| 1695 | + for (DWORD i = 0; i < pTcpTable->dwNumEntries; i++) { |
| 1696 | + if (pTcpTable->table[i].dwOwningPid == targetPid && |
| 1697 | + pTcpTable->table[i].dwState == MIB_TCP_STATE_LISTEN) { |
| 1698 | + struct in_addr addr; |
| 1699 | + addr.S_un.S_addr = pTcpTable->table[i].dwLocalAddr; |
| 1700 | + std::string ip = inet_ntoa(addr); |
| 1701 | + u_short port = ntohs(pTcpTable->table[i].dwLocalPort); |
| 1702 | + listening.push_back(ip + ":" + std::to_string(port)); |
| 1703 | + } |
| 1704 | + } |
| 1705 | + |
| 1706 | + if (!listening.empty()) { |
| 1707 | + if (IsVirtualTerminalModeEnabled()) { |
| 1708 | + std::cout << "\033[1;32mListening\033[0m: \n"; |
| 1709 | + } else { |
| 1710 | + std::cout << "Listening: \n"; |
| 1711 | + } |
| 1712 | + |
| 1713 | + |
| 1714 | + for (size_t i = 0; i < listening.size(); i++) { |
| 1715 | + std::cout << "\t\t" << listening[i]; |
| 1716 | + if (i < listening.size() - 1) { |
| 1717 | + std::cout << ",\n"; |
| 1718 | + } |
| 1719 | + } |
| 1720 | + std::cout << std::endl; |
| 1721 | + } |
| 1722 | + } |
| 1723 | + |
| 1724 | + free(pTcpTable); |
| 1725 | + } |
| 1726 | +} |
1672 | 1727 |
|
1673 | 1728 |
|
1674 | 1729 |
|
@@ -1870,6 +1925,11 @@ std::string FRAM = ""; // fram means formatted ram, i'm so creative at var namin |
1870 | 1925 | std::cout << "\nWhy It Exists:\n"; |
1871 | 1926 | } |
1872 | 1927 | PrintAncestry(pid); |
| 1928 | + |
| 1929 | + FindProcessPorts(pid); |
| 1930 | + |
| 1931 | + |
| 1932 | + |
1873 | 1933 |
|
1874 | 1934 |
|
1875 | 1935 | if (IsVirtualTerminalModeEnabled()) { |
|
0 commit comments