-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinuxKick.py
92 lines (82 loc) · 4.7 KB
/
LinuxKick.py
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
from DontEdit import * # Import everything from the module DontEdit
import psutil # Import psutil for process and system information utilities
import os # Import os for interacting with the operating system
# Check if the current user is root (root has euid 0)
try:
if os.geteuid() == ROOT: # Assuming ROOT is defined as 0 or in DontEdit module
# Function to retrieve all active SSH connections
def get_ssh_connections():
ssh_info = [] # List to store information about SSH connections
# Iterate over all processes
for proc in psutil.process_iter(['pid', 'name']):
# Check if the process is the SSH daemon (sshd)
if proc.info['name'] == 'sshd':
# Get network connections for this process
connections = proc.connections(kind='inet')
# Check for established connections
for conn in connections:
if conn.status == psutil.CONN_ESTABLISHED:
# Append SSH connection info including PID, local, and remote address
ssh_info.append({
'pid': proc.info['pid'],
'local_address': conn.laddr,
'remote_address': conn.raddr
})
return ssh_info # Return the list of SSH connections
if __name__ == "__main__":
# Get the list of active SSH sessions
ssh_sessions = get_ssh_connections()
if ssh_sessions:
print(f"Active SSH sessions:")
# Print out the PID, local, and remote addresses for each SSH session
for session in ssh_sessions:
print(f"PID: {session['pid']}, Local: {session['local_address']}, Remote: {session['remote_address']}")
# Ask the user if they want to terminate the SSH sessions
print("Do you want to terminate these SSH sessions? (y/kick/n/help/nslookup/who)")
ssh_sessions_kill = input(">>> ") # Get user input
# If the user types 'y', terminate the SSH sessions
if ssh_sessions_kill == "y":
for session in ssh_sessions:
try:
# Try to terminate the process by its PID
process = psutil.Process(session['pid'])
process.terminate()
print(f"Process with PID {GREEN}{session['pid']}{RESET} has been terminated.")
except psutil.NoSuchProcess:
# If the process no longer exists, inform the user
print(f"Process with PID {RED}{session['pid']}{RESET} does not exist.")
# If the user chooses 'n', exit without terminating the SSH sessions
elif ssh_sessions_kill == "n":
print("SSH sessions will not be terminated.")
# If the user types 'help', show the available options
elif ssh_sessions_kill == "help":
print("Options: y (terminate) or kick (terminate), n (do not terminate), nslookup (run nslookup command), who (show connected users).")
# If the user types 'nslookup', run the nslookup command
elif ssh_sessions_kill in nslookupCommand:
try:
print("To exit this command just type CTRL-C")
os.system("nslookup")
pass
except KeyboardInterrupt:
get_ssh_connections()
# If the user types 'who', run the who command to see logged-in users
elif ssh_sessions_kill in connected:
try:
print("To exit this command just type CTRL-C")
os.system("who")
exit()
except KeyboardInterrupt:
get_ssh_connections()
# If the user input is invalid, do nothing and print a message
else:
print("\nInvalid input. SSH sessions will not be terminated.")
else:
# If no SSH sessions are found, print an appropriate message
print(f"{RED}\nNo SSH sessions found.{RESET}")
else:
# If the user is not running the script as root, print an error message
print(f"{RED}\nPLEASE USE ROOT PRIVILEGES{RESET}")
# If the user presses CTRL-C, handle it gracefully
except KeyboardInterrupt:
print("\nExiting program...")
exit()