Skip to content

Commit 645ee73

Browse files
Merge pull request #75 from supervoidcoder/add-listening-entry
This adds the `Listening:` entry in win-witr just like witr! It uses iphlpapi.h to find out what ports are linked to each PID. If you run this as admin, you can even find some interesting stuff about existing Windows processes! If you see a high port number on a process such as `lsass.exe`, say, "49664", that's normal! Even I only learned that today... Have fun inspecting which processes are torrenting 10 terabytes of uncensored "ram sticks breaking" footage!
2 parents 9d57f22 + 7d554e2 commit 645ee73

1 file changed

Lines changed: 61 additions & 1 deletion

File tree

main.cpp

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <conio.h>
2323
#include <cassert>
2424
#include <psapi.h>
25+
#include <iphlpapi.h>
2526

2627
#define windows_time_to_unix_epoch(x) ((x) - 116444736000000000LL) / 10000000LL
2728
// 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
16681669
}
16691670
}
16701671

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+
}
16721727

16731728

16741729

@@ -1870,6 +1925,11 @@ std::string FRAM = ""; // fram means formatted ram, i'm so creative at var namin
18701925
std::cout << "\nWhy It Exists:\n";
18711926
}
18721927
PrintAncestry(pid);
1928+
1929+
FindProcessPorts(pid);
1930+
1931+
1932+
18731933

18741934

18751935
if (IsVirtualTerminalModeEnabled()) {

0 commit comments

Comments
 (0)