#!/bin/bash

# Check distribution and version
echo "### Distribution and Version ###"
if [ -f /etc/os-release ]; then
    . /etc/os-release
    echo "Name: $NAME"
    echo "Version: $VERSION"
    echo "ID: $ID"
    echo "ID_LIKE: $ID_LIKE"
    echo "PRETTY_NAME: $PRETTY_NAME"
else
    echo "Distribution information not found."
fi

echo ""
echo "### Kernel Version ###"
uname -r

# Check for package manager
echo ""
echo "### Package Manager ###"
if command -v apt &> /dev/null; then
    echo "Package manager: APT"
elif command -v yum &> /dev/null; then
    echo "Package manager: YUM"
elif command -v dnf &> /dev/null; then
    echo "Package manager: DNF"
elif command -v zypper &> /dev/null; then
    echo "Package manager: Zypper"
elif command -v pacman &> /dev/null; then
    echo "Package manager: Pacman"
else
    echo "Package manager not found."
fi

# Check for iptables or ufw
echo ""
echo "### Firewall Status ###"
if command -v iptables &> /dev/null; then
    echo "iptables is installed."
    sudo iptables -L -v || echo "Insufficient permissions to view iptables rules."
elif command -v ufw &> /dev/null; then
    echo "ufw is installed."
    sudo ufw status verbose || echo "Insufficient permissions to view ufw status."
else
    echo "No firewall found (iptables or ufw)."
fi

# Check for SELinux
echo ""
echo "### SELinux Status ###"
if command -v sestatus &> /dev/null; then
    sudo sestatus || echo "Insufficient permissions to view SELinux status."
else
    echo "SELinux not found."
fi

# Check for other security tools
echo ""
echo "### Other Security Tools ###"
if command -v apparmor_status &> /dev/null; then
    echo "AppArmor is installed."
    sudo apparmor_status || echo "Insufficient permissions to view AppArmor status."
else
    echo "AppArmor not found."
fi

echo ""
echo "### Installed Security Packages ###"
# List some key security packages that might be installed
key_packages=("fail2ban" "clamav" "rkhunter" "chkrootkit" "auditd" "apparmor" "selinux-utils" "firewalld" "gufw" "libpam-tmpdir")
for package in "${key_packages[@]}"; do
    if dpkg -l | grep -qw $package; then
        echo "$package is installed."
    else
        echo "$package is not installed."
    fi
done

echo ""
echo "### Common Shell Tools ###"
# List common shell tools
common_tools=("mosh" "zip" "git" "docker" "fzf" "mc" "tmux" "htop")
for tool in "${common_tools[@]}"; do
    if command -v $tool &> /dev/null; then
        echo "$tool is installed."
    else
        echo "$tool is not installed."
    fi
done

echo ""
echo "### Python Versions ###"
for python in python{,2,3} python{2,3}.{5,6,7,8,9,10,11,12}; do
    if command -v $python &> /dev/null; then
        echo "$python is installed: $($python --version 2>&1)"
    fi
done

echo ""
echo "### PHP Versions ###"
if command -v php &> /dev/null; then
    php -v
else
    echo "PHP is not installed."
fi

echo ""
echo "### GCC Version ###"
if command -v gcc &> /dev/null; then
    gcc --version | head -n 1
else
    echo "GCC is not installed."
fi

echo ""
echo "### Installed Databases ###"
databases=("mysql" "mariadb" "postgresql" "mongodb" "redis" "sqlite3" "couchdb")
for db in "${databases[@]}"; do
    if command -v $db &> /dev/null; then
        echo "$db is installed."
    else
        echo "$db is not installed."
    fi
done

echo ""
echo "### User Group Memberships and Permissions ###"
echo "Groups: $(groups)"
echo "User Permissions: $(id)"

# Output network interfaces
echo ""
echo "### Network Interfaces ###"
ip a

# Output listening ports
echo ""
echo "### Listening Ports ###"
sudo netstat -tuln || echo "Insufficient permissions to view listening ports."

echo ""
echo "### Disk Usage ###"
df -h

echo ""
echo "### Memory Usage ###"
free -h

echo ""
echo "### CPU Info ###"
lscpu

echo ""
echo "### Environment Variables ###"
printenv

echo ""
echo "### Crontab Jobs ###"
sudo crontab -l || echo "Insufficient permissions to view root crontab."
sudo ls /etc/cron.* -R || echo "Insufficient permissions to view cron jobs."

echo ""
echo "### Running Processes ###"
ps aux --sort=-%mem | head -n 20

# Pull and run Lynis if git is installed
if command -v git &> /dev/null; then
    echo ""
    echo "### Cloning and Running Lynis ###"
    git clone https://github.com/CISOfy/lynis.git
    cd lynis
    sudo ./lynis audit system || echo "Insufficient permissions to run Lynis audit."
    cd ..
    rm -rf lynis
else
    echo "Git is not installed. Skipping Lynis installation."
fi

# Save the output to a file
output_file="system_info_$(date +%Y%m%d_%H%M%S).txt"
exec > >(tee -i $output_file)
exec 2>&1

echo ""
echo "### d3fenderz/linux_security Notes and Tests ###"
# Adding notes and tests from d3fenderz/linux_security
echo "Refer to the following guide for detailed Linux security insights and tests:"
echo "https://github.com/d3fenderz/linux_security"
echo ""
echo "### Useful Commands for Security Inspection ###"
echo "uname -a"
echo "df -h"
echo "less -r /etc/passwd"
echo "sudo less -r /etc/shadow || echo 'Insufficient permissions to view shadow file.'"
echo "w -huis"
echo "last -Fw"
echo "lsof -i TCP"
echo "netstat -ano"
echo "ps aux"
echo "ls -alh \$HOME/.*history"
echo "less -r \$HOME/.bash_history"
echo "sudo lsof -r -i :22 || echo 'Insufficient permissions to view SSH connections.'"
echo "journalctl -S -4h"
