-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindSocketHandles.c
executable file
·185 lines (148 loc) · 6.98 KB
/
FindSocketHandles.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* exports FindSocketHandles() */
/* REQUIRES windows.h */
#include "FindSocketHandles.h"
tNTQO MyNtQueryObject; // <-- global variables
tNTQSI MyNtQuerySystemInformation; // allowing us to dynamically load NTDLL
BOOL FindNtShit(void) {
// funct to load up all the NTAPIs we need with the globals above
MyNtQueryObject = (tNTQO) GetProcAddress(GetModuleHandle("NTDLL.DLL"), "NtQueryObject");
if (MyNtQueryObject == NULL) {
return FALSE;
}
MyNtQuerySystemInformation = (tNTQSI) GetProcAddress(GetModuleHandle("NTDLL.DLL"), "NtQuerySystemInformation");
if (MyNtQuerySystemInformation == NULL) {
return FALSE;
}
return TRUE;
}
int FindPIDsWithSocketHandles(POPEN_SOCK_HANDLE_INFO_EX pOSHIEx, DWORD dwSize, DWORD *dwRequired) {
// this is the exported deal
// if dwSize can accomodate all data, pOSHIEx will contain a OPEN_SOCK_HANDLE_INFO_EX struct
// else cbNeeded will be made to the size required of the function
POPEN_SOCK_HANDLE_INFO_EX pMyOSHIEx;
void* pTmpStore; // because realloc can't garantee that data isn't corrupted
DWORD dwRetBufSiz = 0;
//
PSYSTEM_HANDLE_INFORMATION_EX pshiEx;
HANDLE hProcess, hObj = NULL;
DWORD cbNeeded = 0, retVal = 0;
DWORD i = 0, ObjNameCurBufsz = 0, dwWarnCount = 0, dwNextIndex = 0, j = 0;
POBJECT_NAME_INFORMATION pObjNameInfo;
BOOL bEntryExists = 0;
pshiEx = malloc(sizeof (SYSTEM_HANDLE_INFORMATION_EX));
pMyOSHIEx = malloc(sizeof (OPEN_SOCK_HANDLE_INFO_EX));
pTmpStore = malloc(sizeof (OPEN_SOCK_HANDLE_INFO_EX));
dwRetBufSiz = sizeof (OPEN_SOCK_HANDLE_INFO_EX);
if (pshiEx == NULL || pMyOSHIEx == NULL || pTmpStore == NULL) {
return FSH_ERROR_NOMEM;
}
ZeroMemory(pMyOSHIEx, sizeof (OPEN_SOCK_HANDLE_INFO_EX));
/* a bit of init */
FindNtShit();
//DebugMode(TRUE), in our case we've already done it in main.c
/* note; if god mode fails, the results will be restricted to processes we have access to */
MyNtQuerySystemInformation(SystemHandleInformation,
pshiEx,
sizeof (SYSTEM_HANDLE_INFORMATION_EX),
&cbNeeded); //get size for handles
pshiEx = (PSYSTEM_HANDLE_INFORMATION_EX) realloc(pshiEx, cbNeeded);
if (pshiEx == NULL) {
return FSH_ERROR_NOMEM;
}
MyNtQuerySystemInformation(SystemHandleInformation,
pshiEx,
cbNeeded, // get handles
&cbNeeded);
pObjNameInfo = malloc(sizeof (OBJECT_NAME_INFORMATION));
ObjNameCurBufsz = sizeof (OBJECT_NAME_INFORMATION);
if (pObjNameInfo == NULL) {
return FSH_ERROR_NOMEM;
}
for (i = 0; i < pshiEx->NumberOfHandles; ++i) {
// this iterates through enumerated handles
hProcess = OpenProcess(PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, // irrelevent due to debug mode
FALSE,
pshiEx->Information[i].ProcessId);
if (hProcess != NULL) /* bring their handles into our process space */ {
if (DuplicateHandle(hProcess,
(HANDLE) pshiEx->Information[i].Handle, // mingw ddk declares this as a USHORT
GetCurrentProcess(), // and thus the compiler warns us of a cast to
&hObj, // a different size
STANDARD_RIGHTS_REQUIRED,
FALSE,
0) != 0) {
// get object info
ZeroMemory(pObjNameInfo, ObjNameCurBufsz);
retVal = MyNtQueryObject(hObj,
ObjectNameInformation,
pObjNameInfo,
ObjNameCurBufsz,
&cbNeeded);
// i've had issues with this API
// seems it uses threads and locks itself up sometimes after multiple calls
// it seems to work to just bosh in a SleepEx to wait, or create a new thread with a timeout
// if this does lock itself up, the process becomes unkillable (in my experience :\)
if (cbNeeded > ObjNameCurBufsz || retVal != 0) {
pObjNameInfo = (POBJECT_NAME_INFORMATION) realloc(pObjNameInfo, cbNeeded);
if (pObjNameInfo == NULL) {
return FSH_ERROR_NOMEM;
}
ObjNameCurBufsz = cbNeeded;
retVal = MyNtQueryObject(hObj,
ObjectNameInformation,
pObjNameInfo,
ObjNameCurBufsz,
&cbNeeded);
}
// at this point we have enough info to query the driver for info on the connection
// such as state/endpoint etc
if (lstrcmpW(pObjNameInfo->Name.Buffer, L"\\Device\\Tcp") == 0) {
// in our case we don't want to add the same pid twice
bEntryExists = 0;
for (j = 0; j <= pMyOSHIEx->NumberOfEntries; j++) {
if (pMyOSHIEx->OpenSockHandleInfo[j].dwPid == pshiEx->Information[i].ProcessId) {
bEntryExists = 1;
break;
}
}
if (!bEntryExists) {
// add our entry to our local copy of retdata
pTmpStore = realloc(pTmpStore, dwRetBufSiz); // resize ret buffer
if (pTmpStore == NULL) {
return FSH_ERROR_NOMEM;
}
CopyMemory(pTmpStore, pMyOSHIEx, dwRetBufSiz);
dwRetBufSiz += sizeof (OPEN_SOCK_HANDLE_INFO);
pMyOSHIEx = realloc(pMyOSHIEx, dwRetBufSiz);
if (pTmpStore == NULL) {
return FSH_ERROR_NOMEM;
}
CopyMemory(pMyOSHIEx, pTmpStore, dwRetBufSiz - sizeof (OPEN_SOCK_HANDLE_INFO));
pMyOSHIEx->OpenSockHandleInfo[dwNextIndex].dwPid = pshiEx->Information[i].ProcessId; // fill buff info
dwNextIndex++;
pMyOSHIEx->NumberOfEntries++;
}
//done adding entry
}
} else {
dwWarnCount++;
} // get these alot, they are
}// 5 ERROR_ACCESS_DENIED <bemused face>
else {
dwWarnCount++;
}
//close handles
CloseHandle(hObj);
CloseHandle(hProcess);
}
free(pObjNameInfo);
free(pshiEx);
free(pTmpStore);
if (dwSize >= dwRetBufSiz) {
CopyMemory(pOSHIEx, pMyOSHIEx, dwRetBufSiz);
} else {
*dwRequired = dwRetBufSiz;
}
free(pMyOSHIEx);
return dwWarnCount;
}