Debug School

rakesh kumar
rakesh kumar

Posted on

How to detect suspicous activity and unauthorized users in linux server

How to detect unauthorized users
How to detect suspicious activity on a Linux server

To check if a user is authorized to be logged in at a given time on a Linux server, you typically need to inspect the system's configuration files related to user access, such as /etc/passwd, /etc/shadow, and potentially any custom configurations in /etc/security/limits.conf or similar files.

However, directly checking if a user is authorized to be logged in at a given time isn't straightforward with a single command. It involves considering various factors such as user account settings, group memberships, login restrictions, and potentially custom configurations.

Here's a general approach using several commands to gather relevant information:

Check User Account Information: Use the grep command to search for the user in the /etc/passwd file to gather basic user account information.

grep username /etc/passwd
Enter fullscreen mode Exit fullscreen mode

Check User's Allowed Login Times: You might need to examine custom configurations or settings related to allowed login times. This could involve looking at /etc/security/time.conf or similar files, depending on your system's configuration.

Inspect User's Group Memberships: Verify if the user is a member of any groups that have login restrictions or permissions set. This can be checked by examining the /etc/group file.

grep username /etc/group
Enter fullscreen mode Exit fullscreen mode

Review System Logs: Check system logs such as /var/log/auth.log or /var/log/secure for any messages related to the user's login attempts or authentication failures.

tail -n 50 /var/log/auth.log | grep username
Enter fullscreen mode Exit fullscreen mode

Check User's Shell: Ensure that the user's shell (as specified in /etc/passwd) allows login. Common shells include /bin/bash, /bin/sh, /bin/false, etc. If the shell is set to /bin/false, the user is not allowed to log in.

grep username /etc/passwd
Enter fullscreen mode Exit fullscreen mode

Verify Account Status: Check if the user's account is locked or expired, which might prevent them from logging in.

passwd -S username
Enter fullscreen mode Exit fullscreen mode

Keep in mind that the specific commands and files mentioned above might vary depending on your Linux distribution and system configuration. Additionally, determining user authorization for login at a given time often involves considering multiple factors and might require a deeper analysis of system settings and policies.

How to detect suspicious activity on a Linux server

To detect suspicious activity on a Linux server, you can use various commands and tools to monitor system logs, active processes, network connections, and user activities. Here's a command with examples that you can use to identify potential security issues:

Check System Logs for Suspicious Activities:

grep -i 'error\|fail\|warning\|unauthorized' /var/log/syslog
Enter fullscreen mode Exit fullscreen mode

This command searches for common keywords indicating errors, failures, warnings, or unauthorized access in the system log file (/var/log/syslog). Adjust the file path as necessary depending on your system's log configuration.

Monitor Active Processes:

ps aux --sort=-%cpu | head -n 10
Enter fullscreen mode Exit fullscreen mode

This command displays the top 10 processes consuming the most CPU resources. Anomalies such as unknown or suspicious processes with high resource usage could indicate malicious activity.

Inspect Network Connections:

netstat -tuln
Enter fullscreen mode Exit fullscreen mode

This command shows active network connections and listening ports. Look for unexpected connections, especially those from unfamiliar IP addresses or suspicious ports.

Check User Activity:

w
Enter fullscreen mode Exit fullscreen mode

The w command displays information about currently logged-in users and their activities. Look for unfamiliar users, unexpected login times, or unusual commands being executed.

Review Last Logins:

last
Enter fullscreen mode Exit fullscreen mode

The last command shows a list of recent login sessions. Check for any unexpected or unauthorized login attempts.

Scan for Rootkits and Malware:

rkhunter --check
Enter fullscreen mode Exit fullscreen mode

Running the Rootkit Hunter (rkhunter) tool can help detect rootkits and other malware on the system. Ensure that rkhunter is installed and properly configured before running this command.

Check File Integrity:

sudo debsums -s
Enter fullscreen mode Exit fullscreen mode

This command checks the integrity of installed packages by verifying the checksums of their files. Changes in file checksums could indicate unauthorized modifications or compromises.

Monitor Authentication Logs:

tail -n 50 /var/log/auth.log
Enter fullscreen mode Exit fullscreen mode

Review authentication logs for any unusual login attempts, authentication failures, or unauthorized access.

By regularly running these commands and monitoring system activity, you can detect and investigate suspicious behavior on your Linux server to enhance its security posture. Additionally, consider implementing intrusion detection systems (IDS), file integrity monitoring (FIM), and other security measures to further strengthen your server's defenses.

Top comments (0)