# Linux

## Enumeration

* OS Version
* Kernel Version
* Running Services
  * ```shell-session
    $ ps aux | grep root
    ```
* Installed Packages and Versions
* Logged in users
  * ```shell-session
    $ ps au
    ```
* User Home Directories
  * ```shell-session
    $ ls /home
    ```
  * ```shell-session
    $ ls -la /home/stacey.jenkins/
    ```
  * SSH Directory Contents
    * ```shell-session
      $ ls -l ~/.ssh
      ```
  * Bash history
    * ```shell-session
      $ history
      ```
* Sudo Privileges
  * ```shell-session
    $ sudo -l
    ```
* Configuration Files
  * search usernames and passwords in `*.conf` and `*.config`
* Readable Shadow File
* Password hashes in /etc/passwd
  * ```shell-session
    $ cat /etc/passwd
    ```
* Cron Jobs
  * ```shell-session
    $ ls -la /etc/cron.daily/
    ```
* Unmounted File systems and Additional Drives
  * ```shell-session
    $ lsblk
    ```
* SETUID and SETGID Permissions
* Writable Directories
  * ```shell-session
    $ find / -path /proc -prune -o -type d -perm -o+w 2>/dev/null
    ```
* Writable Files
  * ```shell-session
    $ find / -path /proc -prune -o -type f -perm -o+w 2>/dev/null
    ```

## Environment Enumeration

* What user are we running
  * ```shell-session
    $ whoami
    ```
* What groups does our user belong to
  * ```shell-session
    $ id
    ```
* What is the server named. can we gather anything from the naming convention?
  * ```shell-session
    $ hostname 
    ```
* What subnet did we land in, does the host have additional NICs in other subnets?
  * ```shell-session
    $ ifconfig # or ip -a
    ```
* What's the CPU type/version
  * ```shell-session
    $ lscpu
    ```
* Can our user run anything with sudo (as another user as root) without needing a password?
  * ```shell-session
    $ sudo -l 
    ```
* What OS and version is the machine
  * ```shell-session
    $ cat /etc/os-release
    $ cat /proc/version # or uname -a
    ```
* Is the $PATH misconfigured? Can we leverage anything in the $PATH?
  * ```shell-session
    $ echo $PATH
    ```
* Are there any stored credentials in the ENV?
  * ```shell-session
    $ env
    ```
* What login shells exists on the server
  * ```shell-session
    $ cat /etc/shells
    ```
* Are there any harddisks, usb drives, optical drives?
  * ```shell-session
    $ lsblk
    ```
* Are there any active/queued print jobs that can we gain access to some sensitive info?
  * ```shell-session
    $ lpstat
    ```
* Can we mount an unmounted drive?
  * ```shell-session
    $ cat /etc/fstab
    ```
* Mounted file systems?
  * ```shell-session
    $ df -h
    ```
* Unmounted file systems?
  * ```shell-session
    $ cat /etc/fstab | grep -v "#" | column -t
    ```
* Any interesting other networks are available via which interface.
  * ```shell-session
    $ route # or netstat -rn
    ```
* What other hosts does the target has been communicating
  * ```shell-session
    $ arp -a
    ```
* Other users?
  * ```shell-session
    $ cat /etc/passwd
    $ cat /etc/passwd | cut -f1 -d: # create a users.txt dictionary
    ```
* Users with login shells?
  * ```shell-session
    $ grep "*sh$" /etc/passwd
    ```
* Existing groups?
  * ```shell-session
    $ cat /etc/group
    ```
* List members of a group
  * ```shell-session
    $ getent group sudo
    ```
* All hidden files?
  * ```shell-session
    $ find / -type f -name ".*" -exec ls -l {} \; 2>/dev/null | grep htb-student
    ```
* All hidden folders?
  * ```shell-session
    $ find / -type d -name ".*" -ls 2>/dev/null
    ```
* Temporary Files?
  * ```shell-session
    $ ls -l /tmp /var/tmp /dev/shm
    ```
* Find a file containing a string?
  * ```shell-session
    $ find / -name *.sh 2>/dev/null | xargs cat | grep STRING
    ```
* Find config files?
  * ```shell-session
    $ find / ! -path "*/proc/*" -iname "*config*" -type f 2>/dev/null
    ```

{% hint style="info" %}
We should also check to see if any defenses are in place and we can enumerate any information about them. Some things to look for include:

* [Exec Shield](https://en.wikipedia.org/wiki/Exec_Shield)
* [iptables](https://linux.die.net/man/8/iptables)
* [AppArmor](https://apparmor.net/)
* [SELinux](https://www.redhat.com/en/topics/linux/what-is-selinux)
* [Fail2ban](https://github.com/fail2ban/fail2ban)
* [Snort](https://www.snort.org/faq/what-is-snort)
* [Uncomplicated Firewall (ufw)](https://wiki.ubuntu.com/UncomplicatedFirewall)

Often we will not have the privileges to enumerate the configurations of these protections but knowing what, if any, are in place, can help us not to waste time on certain tasks.
{% endhint %}

***

## Internals Enumeration

* Network interfaces
  * ```shell-session
    $ ip a
    ```
* Hosts
  * ```shell-session
    $ cat /etc/hosts
    ```
* User's last login
  * ```shell-session
    $ lastlog
    ```
* Logged in users
  * ```shell-session
    $ w # or who or finger
    ```
* Command history
  * ```shell-session
    $ history
    ```
  * Finding history files
    * {% code overflow="wrap" %}
      ```shell-session
      $ find / -type f \( -name *_hist -o -name *_history \) -exec ls -l {} \; 2>/dev/null
      ```
      {% endcode %}
* Cron
  * ```shell-session
    $ ls -la /etc/cron.daily/
    ```
* Proc
  * ```shell-session
    $ find /proc -name cmdline -exec cat {} \; 2>/dev/null | tr " " "\n"
    ```

***

## Services Enumeration

* Installed Packages
  * ```shell-session
    $ apt list --installed | tr "/" " " | cut -d" " -f1,3 | sed 's/[0-9]://g' | tee -a installed_pkgs.list
    ```
* Sudo Version
  * ```shell-session
    $ sudo -V
    ```
* Binaries
  * ```shell-session
    $ ls -l /bin /usr/bin/ /usr/sbin/
    ```
* GTFObins
  * {% code overflow="wrap" %}
    ```shell-session
    $ for i in $(curl -s https://gtfobins.github.io/ | html2text | cut -d" " -f1 | sed '/^[[:space:]]*$/d');do if grep -q "$i" installed_pkgs.list;then echo "Check GTFO for: $i";fi;done
    ```
    {% endcode %}
* Trace System Calls
  * &#x20;diagnostic tool on Linux-based operating systems to track and analyze system calls and signal processing.
  * ```shell-session
    $ strace ping -c1 10.129.112.20
    ```
* Configuration Files
  * {% code overflow="wrap" %}
    ```shell-session
    $ find / -type f \( -name *.conf -o -name *.config \) -exec ls -l {} \; 2>/dev/null
    ```
    {% endcode %}
* Scripts
  * ```shell-session
    $ find / -type f -name "*.sh" 2>/dev/null | grep -v "src\|snap\|share"
    ```
* Running Services by User
  * ```shell-session
    $ ps aux | grep root
    ```

***

## Credential Hunting

{% hint style="info" %}
These may be found in configuration files (`.conf`, `.config`, `.xml`, etc.), shell scripts, a user's bash history file, backup (`.bak`) files, within database files or even in text files.
{% endhint %}

* In wpconfig.php
  * ```shell-session
    $ cat wp-config.php | grep 'DB_USER\|DB_PASSWORD'
    ```
* \*config files
  * ```shell-session
    $  find / ! -path "*/proc/*" -iname "*config*" -type f 2>/dev/null
    ```
* SSH Keys
  * ```shell-session
    $  ls ~/.ssh
    ```

***

## Linux Hardening

* Updates and patching
  * Use the unattended-upgrades package installed in linux
* Configuration management
  * Audit writable files and directories and any binaries set with the SUID bit.
  * Ensure that any cron jobs and sudo privileges specify any binaries using the absolute path.
  * Do not store credentials in cleartext in world-readable files.
  * Clean up home directories and bash history.
  * Ensure that low-privileged users cannot modify any custom libraries called by programs.
  * Remove any unnecessary packages and services that potentially increase the attack surface.
  * Consider implementing [SELinux](https://www.redhat.com/en/topics/linux/what-is-selinux), which provides additional access controls on the system.
* User management
  * We should limit the number of user accounts and admin accounts on each system, ensure that logon attempts (valid/invalid) are logged and monitored. It is also a good idea to enforce a strong password policy, rotate passwords periodically, and restrict users from reusing old passwords by using the /etc/security/opasswd file with the PAM module. We should check that users are not placed into groups that give them excessive rights not needed for their day-to-day tasks and limit sudo rights based on the principle of least privilege.
  * Templates exist for configuration management automation tools such as [Puppet](https://puppet.com/use-cases/configuration-management/), [SaltStack](https://github.com/saltstack/salt), [Zabbix](https://en.wikipedia.org/wiki/Zabbix) and [Nagios](https://en.wikipedia.org/wiki/Nagios) to automate such checks and can be used to push messages to a Slack channel or email box as well as via other methods. Remote actions (Zabbix) and Remediation Actions (Nagios) can be used to find and auto correct these issues over a fleet of nodes. Tools such as Zabbix also feature functions such as checksum verification, which can be used for both version control and to confirm sensitive binaries have not been tampered with. For example, via the [vfs.file.cksum](https://www.zabbix.com/documentation/4.0/manual/config/items/itemtypes/zabbix_agent) file.
* Audit
  * One useful tool for auditing Unix-based systems (Linux, macOS, BDS, etc.) is [Lynis](https://github.com/CISOfy/lynis).
