Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
B
bsc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexlab
bsc
Commits
5ceef5ea
Commit
5ceef5ea
authored
Oct 19, 2025
by
Kilo Code
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add process tree monitoring capability
parent
fd78e572
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
159 additions
and
36 deletions
+159
-36
network_monitor.c
network_monitor.c
+159
-36
No files found.
network_monitor.c
View file @
5ceef5ea
...
@@ -26,6 +26,17 @@
...
@@ -26,6 +26,17 @@
#include <tlhelp32.h>
#include <tlhelp32.h>
#include <psapi.h>
#include <psapi.h>
// Structure to track monitored processes
typedef
struct
{
DWORD
pid
;
HANDLE
hProcess
;
char
processName
[
MAX_PATH
];
}
MonitoredProcess
;
#define MAX_PROCESSES 256
MonitoredProcess
monitoredProcesses
[
MAX_PROCESSES
];
int
processCount
=
0
;
// #pragma comment(lib, "ws2_32.lib")
// #pragma comment(lib, "ws2_32.lib")
// #pragma comment(lib, "iphlpapi.lib")
// #pragma comment(lib, "iphlpapi.lib")
...
@@ -41,6 +52,79 @@ int is_internal_ip(DWORD ip) {
...
@@ -41,6 +52,79 @@ int is_internal_ip(DWORD ip) {
return
0
;
return
0
;
}
}
// Function to add a process to the monitoring list
void
AddMonitoredProcess
(
DWORD
pid
,
HANDLE
hProcess
,
const
char
*
processName
)
{
if
(
processCount
>=
MAX_PROCESSES
)
return
;
monitoredProcesses
[
processCount
].
pid
=
pid
;
monitoredProcesses
[
processCount
].
hProcess
=
hProcess
;
strcpy
(
monitoredProcesses
[
processCount
].
processName
,
processName
);
processCount
++
;
}
// Function to remove a terminated process from monitoring
void
RemoveMonitoredProcess
(
DWORD
pid
)
{
for
(
int
i
=
0
;
i
<
processCount
;
i
++
)
{
if
(
monitoredProcesses
[
i
].
pid
==
pid
)
{
CloseHandle
(
monitoredProcesses
[
i
].
hProcess
);
// Shift remaining processes
for
(
int
j
=
i
;
j
<
processCount
-
1
;
j
++
)
{
monitoredProcesses
[
j
]
=
monitoredProcesses
[
j
+
1
];
}
processCount
--
;
break
;
}
}
}
// Function to check if a process is already being monitored
int
IsProcessMonitored
(
DWORD
pid
)
{
for
(
int
i
=
0
;
i
<
processCount
;
i
++
)
{
if
(
monitoredProcesses
[
i
].
pid
==
pid
)
return
1
;
}
return
0
;
}
// Function to inject DLL into a process
BOOL
InjectDLLIntoProcess
(
DWORD
pid
)
{
HANDLE
hProcess
=
OpenProcess
(
PROCESS_ALL_ACCESS
,
FALSE
,
pid
);
if
(
!
hProcess
)
return
FALSE
;
if
(
!
InjectDLL
(
hProcess
,
"ssl_hook.dll"
))
{
CloseHandle
(
hProcess
);
return
FALSE
;
}
CloseHandle
(
hProcess
);
return
TRUE
;
}
// Function to find and monitor child processes
void
MonitorChildProcesses
(
DWORD
parentPid
)
{
HANDLE
hSnapshot
=
CreateToolhelp32Snapshot
(
TH32CS_SNAPPROCESS
,
0
);
if
(
hSnapshot
==
INVALID_HANDLE_VALUE
)
return
;
PROCESSENTRY32
pe32
;
pe32
.
dwSize
=
sizeof
(
PROCESSENTRY32
);
if
(
Process32First
(
hSnapshot
,
&
pe32
))
{
do
{
if
(
pe32
.
th32ParentProcessID
==
parentPid
&&
!
IsProcessMonitored
(
pe32
.
th32ProcessID
))
{
// Found a child process, inject DLL and add to monitoring
if
(
InjectDLLIntoProcess
(
pe32
.
th32ProcessID
))
{
HANDLE
hProcess
=
OpenProcess
(
PROCESS_QUERY_INFORMATION
|
PROCESS_VM_READ
,
FALSE
,
pe32
.
th32ProcessID
);
if
(
hProcess
)
{
AddMonitoredProcess
(
pe32
.
th32ProcessID
,
hProcess
,
pe32
.
szExeFile
);
printf
(
"Now monitoring child process: %s (PID: %lu)
\n
"
,
pe32
.
szExeFile
,
pe32
.
th32ProcessID
);
}
}
}
}
while
(
Process32Next
(
hSnapshot
,
&
pe32
));
}
CloseHandle
(
hSnapshot
);
}
BOOL
InjectDLL
(
HANDLE
hProcess
,
const
char
*
dllPath
)
{
BOOL
InjectDLL
(
HANDLE
hProcess
,
const
char
*
dllPath
)
{
LPVOID
pRemoteBuf
=
VirtualAllocEx
(
hProcess
,
NULL
,
strlen
(
dllPath
)
+
1
,
MEM_COMMIT
,
PAGE_READWRITE
);
LPVOID
pRemoteBuf
=
VirtualAllocEx
(
hProcess
,
NULL
,
strlen
(
dllPath
)
+
1
,
MEM_COMMIT
,
PAGE_READWRITE
);
if
(
!
pRemoteBuf
)
return
FALSE
;
if
(
!
pRemoteBuf
)
return
FALSE
;
...
@@ -127,7 +211,12 @@ int main() {
...
@@ -127,7 +211,12 @@ int main() {
ResumeThread
(
pi
.
hThread
);
ResumeThread
(
pi
.
hThread
);
DWORD
pid
=
pi
.
dwProcessId
;
DWORD
rootPid
=
pi
.
dwProcessId
;
// Add the root process to monitoring
AddMonitoredProcess
(
rootPid
,
pi
.
hProcess
,
program
);
printf
(
"Now monitoring root process: %s (PID: %lu)
\n
"
,
program
,
rootPid
);
Sleep
(
2000
);
// Wait for process to potentially establish connections
Sleep
(
2000
);
// Wait for process to potentially establish connections
char
internal_log_path
[
512
];
char
internal_log_path
[
512
];
...
@@ -138,7 +227,7 @@ int main() {
...
@@ -138,7 +227,7 @@ int main() {
FILE
*
internal_log
=
fopen
(
internal_log_path
,
"a"
);
FILE
*
internal_log
=
fopen
(
internal_log_path
,
"a"
);
FILE
*
external_log
=
fopen
(
external_log_path
,
"a"
);
FILE
*
external_log
=
fopen
(
external_log_path
,
"a"
);
// Initial snapshot of connections
// Initial snapshot of connections
for all monitored processes
PMIB_TCPTABLE_OWNER_PID
tcpTable
;
PMIB_TCPTABLE_OWNER_PID
tcpTable
;
DWORD
size
=
0
;
DWORD
size
=
0
;
GetExtendedTcpTable
(
NULL
,
&
size
,
FALSE
,
AF_INET
,
TCP_TABLE_OWNER_PID_ALL
,
0
);
GetExtendedTcpTable
(
NULL
,
&
size
,
FALSE
,
AF_INET
,
TCP_TABLE_OWNER_PID_ALL
,
0
);
...
@@ -146,11 +235,14 @@ int main() {
...
@@ -146,11 +235,14 @@ int main() {
if
(
GetExtendedTcpTable
(
tcpTable
,
&
size
,
FALSE
,
AF_INET
,
TCP_TABLE_OWNER_PID_ALL
,
0
)
==
NO_ERROR
)
{
if
(
GetExtendedTcpTable
(
tcpTable
,
&
size
,
FALSE
,
AF_INET
,
TCP_TABLE_OWNER_PID_ALL
,
0
)
==
NO_ERROR
)
{
for
(
DWORD
i
=
0
;
i
<
tcpTable
->
dwNumEntries
;
i
++
)
{
for
(
DWORD
i
=
0
;
i
<
tcpTable
->
dwNumEntries
;
i
++
)
{
MIB_TCPROW_OWNER_PID
row
=
tcpTable
->
table
[
i
];
MIB_TCPROW_OWNER_PID
row
=
tcpTable
->
table
[
i
];
if
(
row
.
dwOwningPid
==
pid
)
{
// Check if this connection belongs to any monitored process
for
(
int
p
=
0
;
p
<
processCount
;
p
++
)
{
if
(
row
.
dwOwningPid
==
monitoredProcesses
[
p
].
pid
)
{
DWORD
localIP
=
row
.
dwLocalAddr
;
DWORD
localIP
=
row
.
dwLocalAddr
;
DWORD
remoteIP
=
row
.
dwRemoteAddr
;
DWORD
remoteIP
=
row
.
dwRemoteAddr
;
char
log_entry
[
256
];
char
log_entry
[
256
];
sprintf
(
log_entry
,
"Connection: Local %lu.%lu.%lu.%lu:%u -> Remote %lu.%lu.%lu.%lu:%u State:%lu
\n
"
,
sprintf
(
log_entry
,
"[%s] Connection: Local %lu.%lu.%lu.%lu:%u -> Remote %lu.%lu.%lu.%lu:%u State:%lu
\n
"
,
monitoredProcesses
[
p
].
processName
,
(
localIP
>>
24
)
&
0xFF
,
(
localIP
>>
16
)
&
0xFF
,
(
localIP
>>
8
)
&
0xFF
,
localIP
&
0xFF
,
ntohs
(
row
.
dwLocalPort
),
(
localIP
>>
24
)
&
0xFF
,
(
localIP
>>
16
)
&
0xFF
,
(
localIP
>>
8
)
&
0xFF
,
localIP
&
0xFF
,
ntohs
(
row
.
dwLocalPort
),
(
remoteIP
>>
24
)
&
0xFF
,
(
remoteIP
>>
16
)
&
0xFF
,
(
remoteIP
>>
8
)
&
0xFF
,
remoteIP
&
0xFF
,
ntohs
(
row
.
dwRemotePort
),
(
remoteIP
>>
24
)
&
0xFF
,
(
remoteIP
>>
16
)
&
0xFF
,
(
remoteIP
>>
8
)
&
0xFF
,
remoteIP
&
0xFF
,
ntohs
(
row
.
dwRemotePort
),
row
.
dwState
);
row
.
dwState
);
...
@@ -161,6 +253,8 @@ int main() {
...
@@ -161,6 +253,8 @@ int main() {
fprintf
(
external_log
,
"%s"
,
log_entry
);
fprintf
(
external_log
,
"%s"
,
log_entry
);
}
}
printf
(
"%s"
,
log_entry
);
printf
(
"%s"
,
log_entry
);
break
;
}
}
}
}
}
}
}
...
@@ -171,33 +265,52 @@ int main() {
...
@@ -171,33 +265,52 @@ int main() {
// Continuous monitoring loop
// Continuous monitoring loop
DWORD
lastStatusTime
=
GetTickCount
();
DWORD
lastStatusTime
=
GetTickCount
();
const
DWORD
STATUS_INTERVAL
=
5000
;
// 5 seconds
const
DWORD
STATUS_INTERVAL
=
5000
;
// 5 seconds
DWORD
lastChildCheckTime
=
GetTickCount
();
const
DWORD
CHILD_CHECK_INTERVAL
=
2000
;
// 2 seconds
while
(
1
)
{
while
(
1
)
{
DWORD
currentTime
=
GetTickCount
();
DWORD
currentTime
=
GetTickCount
();
// Check for new child processes periodically
if
(
currentTime
-
lastChildCheckTime
>=
CHILD_CHECK_INTERVAL
)
{
// Check all monitored processes for new children
for
(
int
p
=
0
;
p
<
processCount
;
p
++
)
{
MonitorChildProcesses
(
monitoredProcesses
[
p
].
pid
);
}
lastChildCheckTime
=
currentTime
;
}
// Periodic status logging
// Periodic status logging
if
(
currentTime
-
lastStatusTime
>=
STATUS_INTERVAL
)
{
if
(
currentTime
-
lastStatusTime
>=
STATUS_INTERVAL
)
{
// Get current TCP connections for the monitored process
// Aggregate data from all monitored processes
DWORD
totalSocketCount
=
0
;
DWORD
totalThreadCount
=
0
;
DWORD
totalMemoryUsage
=
0
;
// Get current TCP connections for all monitored processes
size
=
0
;
size
=
0
;
GetExtendedTcpTable
(
NULL
,
&
size
,
FALSE
,
AF_INET
,
TCP_TABLE_OWNER_PID_ALL
,
0
);
GetExtendedTcpTable
(
NULL
,
&
size
,
FALSE
,
AF_INET
,
TCP_TABLE_OWNER_PID_ALL
,
0
);
tcpTable
=
(
PMIB_TCPTABLE_OWNER_PID
)
malloc
(
size
);
tcpTable
=
(
PMIB_TCPTABLE_OWNER_PID
)
malloc
(
size
);
DWORD
socketCount
=
0
;
if
(
GetExtendedTcpTable
(
tcpTable
,
&
size
,
FALSE
,
AF_INET
,
TCP_TABLE_OWNER_PID_ALL
,
0
)
==
NO_ERROR
)
{
if
(
GetExtendedTcpTable
(
tcpTable
,
&
size
,
FALSE
,
AF_INET
,
TCP_TABLE_OWNER_PID_ALL
,
0
)
==
NO_ERROR
)
{
for
(
DWORD
i
=
0
;
i
<
tcpTable
->
dwNumEntries
;
i
++
)
{
for
(
DWORD
i
=
0
;
i
<
tcpTable
->
dwNumEntries
;
i
++
)
{
if
(
tcpTable
->
table
[
i
].
dwOwningPid
==
pid
)
{
for
(
int
p
=
0
;
p
<
processCount
;
p
++
)
{
socketCount
++
;
if
(
tcpTable
->
table
[
i
].
dwOwningPid
==
monitoredProcesses
[
p
].
pid
)
{
totalSocketCount
++
;
break
;
}
}
}
}
}
}
}
// Get process information
// Get process information
for all monitored processes
HANDLE
hProcess
=
OpenProcess
(
PROCESS_QUERY_INFORMATION
|
PROCESS_VM_READ
,
FALSE
,
pid
);
for
(
int
p
=
0
;
p
<
processCount
;
p
++
)
{
DWORD
threadCount
=
0
;
DWORD
threadCount
=
0
;
DWORD
memoryUsage
=
0
;
DWORD
memoryUsage
=
0
;
if
(
hProcess
)
{
PROCESS_MEMORY_COUNTERS
pmc
;
PROCESS_MEMORY_COUNTERS
pmc
;
if
(
GetProcessMemoryInfo
(
hProcess
,
&
pmc
,
sizeof
(
pmc
)))
{
if
(
GetProcessMemoryInfo
(
monitoredProcesses
[
p
].
hProcess
,
&
pmc
,
sizeof
(
pmc
)))
{
memoryUsage
=
pmc
.
WorkingSetSize
/
1024
;
// KB
memoryUsage
=
pmc
.
WorkingSetSize
/
1024
;
// KB
totalMemoryUsage
+=
memoryUsage
;
}
}
// Get thread count
// Get thread count
...
@@ -207,14 +320,14 @@ int main() {
...
@@ -207,14 +320,14 @@ int main() {
te32
.
dwSize
=
sizeof
(
THREADENTRY32
);
te32
.
dwSize
=
sizeof
(
THREADENTRY32
);
if
(
Thread32First
(
hSnapshot
,
&
te32
))
{
if
(
Thread32First
(
hSnapshot
,
&
te32
))
{
do
{
do
{
if
(
te32
.
th32OwnerProcessID
==
pid
)
{
if
(
te32
.
th32OwnerProcessID
==
monitoredProcesses
[
p
].
pid
)
{
threadCount
++
;
threadCount
++
;
}
}
}
while
(
Thread32Next
(
hSnapshot
,
&
te32
));
}
while
(
Thread32Next
(
hSnapshot
,
&
te32
));
}
}
CloseHandle
(
hSnapshot
);
CloseHandle
(
hSnapshot
);
}
}
CloseHandle
(
hProcess
)
;
totalThreadCount
+=
threadCount
;
}
}
// Get system time for logging
// Get system time for logging
...
@@ -222,25 +335,35 @@ int main() {
...
@@ -222,25 +335,35 @@ int main() {
GetSystemTime
(
&
st
);
GetSystemTime
(
&
st
);
// Print status to console
// Print status to console
printf
(
"[%04d-%02d-%02d %02d:%02d:%02d] STATUS - Sockets: %lu, Threads: %lu, Memory: %lu KB
\n
"
,
printf
(
"[%04d-%02d-%02d %02d:%02d:%02d] STATUS -
Processes: %d,
Sockets: %lu, Threads: %lu, Memory: %lu KB
\n
"
,
st
.
wYear
,
st
.
wMonth
,
st
.
wDay
,
st
.
wHour
,
st
.
wMinute
,
st
.
wSecond
,
st
.
wYear
,
st
.
wMonth
,
st
.
wDay
,
st
.
wHour
,
st
.
wMinute
,
st
.
wSecond
,
socketCount
,
threadCount
,
m
emoryUsage
);
processCount
,
totalSocketCount
,
totalThreadCount
,
totalM
emoryUsage
);
free
(
tcpTable
);
free
(
tcpTable
);
lastStatusTime
=
currentTime
;
lastStatusTime
=
currentTime
;
}
}
// Check if process is still running
// Check for terminated processes and remove them
for
(
int
p
=
processCount
-
1
;
p
>=
0
;
p
--
)
{
DWORD
exitCode
;
DWORD
exitCode
;
if
(
GetExitCodeProcess
(
pi
.
hProcess
,
&
exitCode
)
&&
exitCode
!=
STILL_ACTIVE
)
{
if
(
GetExitCodeProcess
(
monitoredProcesses
[
p
].
hProcess
,
&
exitCode
)
&&
exitCode
!=
STILL_ACTIVE
)
{
printf
(
"Monitored process has terminated.
\n
"
);
printf
(
"Process %s (PID: %lu) has terminated.
\n
"
,
monitoredProcesses
[
p
].
processName
,
monitoredProcesses
[
p
].
pid
);
RemoveMonitoredProcess
(
monitoredProcesses
[
p
].
pid
);
}
}
// Exit if no processes are left to monitor
if
(
processCount
==
0
)
{
printf
(
"All monitored processes have terminated.
\n
"
);
break
;
break
;
}
}
Sleep
(
1000
);
// Check every second
Sleep
(
1000
);
// Check every second
}
}
CloseHandle
(
pi
.
hProcess
);
// Clean up any remaining process handles
CloseHandle
(
pi
.
hThread
);
for
(
int
p
=
0
;
p
<
processCount
;
p
++
)
{
CloseHandle
(
monitoredProcesses
[
p
].
hProcess
);
}
return
0
;
return
0
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment