Skip to content

Commit c0e42bc

Browse files
committed
added functionality to reap the child sshd processes
1 parent d3fd636 commit c0e42bc

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

src/monitor.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
from datetime import datetime
66
import threading
77
import time
8+
import subprocess
9+
import signal
10+
811

912
logging.basicConfig(
1013
encoding="utf-8",
@@ -70,9 +73,28 @@ def submit_attack(ip, user, password, evidence, ATTACKPOD_LOCAL_IP):
7073
except Exception as e:
7174
logging.error(f"[!] Got an exception while submitting the attack: {e}")
7275

76+
77+
def reap_children(signum, frame):
78+
try:
79+
while True:
80+
pid, _ = os.waitpid(-1, os.WNOHANG)
81+
if pid == 0:
82+
break
83+
logging.info(f"Reaped child process with PID {pid}")
84+
except ChildProcessError:
85+
pass
86+
87+
signal.signal(signal.SIGCHLD, reap_children)
88+
89+
7390
def run_sshd():
7491
while True:
75-
os.system("/sbin/sshd -D -E /var/log/ssh.log")
92+
try:
93+
process = subprocess.Popen(["/usr/sbin/sshd", "-D", "-E", "/var/log/ssh.log"])
94+
process.wait() # Wait for the process to terminate and reap it
95+
except Exception as e:
96+
logging.error(f"Error while running sshd: {e}")
97+
time.sleep(1) # Avoid tight loop if something goes wrong
7698

7799

78100
def rotate_sshd_keys():

0 commit comments

Comments
 (0)