-
Notifications
You must be signed in to change notification settings - Fork 21
/
IPtools.cpp
161 lines (148 loc) · 4.57 KB
/
IPtools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include "IPtools.h"
#include "helpers.h"
#include <string.h>
HRESULT GetRDPClientAddress(_In_ int RDPPort, _Outptr_result_nullonfailure_ PWSTR *IPaddress)
{
//PrintLn("GetRDPClientAddress");
HRESULT hr = E_NOTIMPL;
*IPaddress = nullptr;
PMIB_TCPTABLE2 pTcpTable;
ULONG ulSize = 0;
DWORD dwRetVal = 0;
//char szLocalAddr[128];
char szRemoteAddr[128];
PWSTR ptAddr;
struct in_addr IpAddr;
int i;
pTcpTable = (MIB_TCPTABLE2 *)MALLOC(sizeof(MIB_TCPTABLE2));
if (pTcpTable == NULL) {
PrintLn("GetRDPClientAddress: Error allocating memory\n");
return 1;
}
ulSize = sizeof(MIB_TCPTABLE);
// Make an initial call to GetTcpTable2 to
// get the necessary size into the ulSize variable
if ((dwRetVal = GetTcpTable2(pTcpTable, &ulSize, TRUE)) ==
ERROR_INSUFFICIENT_BUFFER) {
FREE(pTcpTable);
pTcpTable = (MIB_TCPTABLE2 *)MALLOC(ulSize);
if (pTcpTable == NULL) {
PrintLn("GetRDPClientAddress: Error allocating memory\n");
return 1;
}
}
// Make a second call to GetTcpTable2 to get
// the actual data we require
if ((dwRetVal = GetTcpTable2(pTcpTable, &ulSize, TRUE)) == NO_ERROR) {
for (i = 0; i < (int)pTcpTable->dwNumEntries; i++) {
if ((ntohs((u_short)pTcpTable->table[i].dwLocalPort) == RDPPort) &&
(pTcpTable->table[i].dwState == MIB_TCP_STATE_ESTAB)) {
/*
PrintLn("\n\tTCP[%d] State: %ld - ", i,
pTcpTable->table[i].dwState);
switch (pTcpTable->table[i].dwState) {
case MIB_TCP_STATE_CLOSED:
PrintLn("CLOSED\n");
break;
case MIB_TCP_STATE_LISTEN:
PrintLn("LISTEN\n");
break;
case MIB_TCP_STATE_SYN_SENT:
PrintLn("SYN-SENT\n");
break;
case MIB_TCP_STATE_SYN_RCVD:
PrintLn("SYN-RECEIVED\n");
break;
case MIB_TCP_STATE_ESTAB:
PrintLn("ESTABLISHED\n");
break;
case MIB_TCP_STATE_FIN_WAIT1:
PrintLn("FIN-WAIT-1\n");
break;
case MIB_TCP_STATE_FIN_WAIT2:
PrintLn("FIN-WAIT-2 \n");
break;
case MIB_TCP_STATE_CLOSE_WAIT:
PrintLn("CLOSE-WAIT\n");
break;
case MIB_TCP_STATE_CLOSING:
PrintLn("CLOSING\n");
break;
case MIB_TCP_STATE_LAST_ACK:
PrintLn("LAST-ACK\n");
break;
case MIB_TCP_STATE_TIME_WAIT:
PrintLn("TIME-WAIT\n");
break;
case MIB_TCP_STATE_DELETE_TCB:
PrintLn("DELETE-TCB\n");
break;
default:
PrintLn("UNKNOWN dwState value\n");
break;
}
IpAddr.S_un.S_addr = (u_long)pTcpTable->table[i].dwLocalAddr;
strcpy_s(szLocalAddr, sizeof(szLocalAddr), inet_ntoa(IpAddr));
PrintLn("\tTCP[%d] Local Addr: %s\n", i, szLocalAddr);
PrintLn("\tTCP[%d] Local Port: %d \n", i,
ntohs((u_short)pTcpTable->table[i].dwLocalPort));
*/
IpAddr.S_un.S_addr = (u_long)pTcpTable->table[i].dwRemoteAddr;
strcpy_s(szRemoteAddr, sizeof(szRemoteAddr), inet_ntoa(IpAddr));
if (strstr( "0.0.0.0", szRemoteAddr) == 0) {
//PrintLn("Remote Addr:");
//PrintLn(szRemoteAddr);
//PrintLn(ntohs((u_short)pTcpTable->table[i].dwLocalPort));
//PrintLn(ntohs((u_short)pTcpTable->table[i].dwRemotePort));
//if (strstr("91.199.25.153", szRemoteAddr) != 0) {
size_t len = strlen(szRemoteAddr);
ptAddr = static_cast<PWSTR>(CoTaskMemAlloc(sizeof(wchar_t) * (len + 1)));
MultiByteToWideChar(
CP_ACP,
0,
szRemoteAddr,
-1,
ptAddr,
128);
*IPaddress = ptAddr;
hr = 0;
}
//*IPaddress = szRemoteAddr;
/*
PrintLn("\tTCP[%d] Remote Port: %d\n", i,
ntohs((u_short)pTcpTable->table[i].dwRemotePort));
PrintLn("\tTCP[%d] Owning PID: %d\n", i, pTcpTable->table[i].dwOwningPid);
PrintLn("\tTCP[%d] Offload State: %ld - ", i,
pTcpTable->table[i].dwOffloadState);
switch (pTcpTable->table[i].dwOffloadState) {
case TcpConnectionOffloadStateInHost:
PrintLn("Owned by the network stack and not offloaded \n");
break;
case TcpConnectionOffloadStateOffloading:
PrintLn("In the process of being offloaded\n");
break;
case TcpConnectionOffloadStateOffloaded:
PrintLn("Offloaded to the network interface control\n");
break;
case TcpConnectionOffloadStateUploading:
PrintLn("In the process of being uploaded back to the network stack \n");
break;
default:
PrintLn("UNKNOWN Offload state value\n");
break;
}*/
}
}
}
else {
PrintLn("\tGetTcpTable2 failed with %d\n", dwRetVal);
FREE(pTcpTable);
return 1;
}
if (pTcpTable != NULL) {
FREE(pTcpTable);
pTcpTable = NULL;
}
return hr;
}