# level 2

<pre class="language-c"><code class="lang-c"><strong>#include &#x3C;stdio.h>
</strong>#include &#x3C;stdlib.h>
#include &#x3C;signal.h>
#include &#x3C;unistd.h>

void catcher(int a)
{
    setresuid(geteuid(),geteuid(),geteuid());
    printf("WIN!\n");
    system("/bin/sh");
    exit(0);
}

int main(int argc, char **argv)
{
	puts("source code is available in level02.c\n");

    if (argc != 3 || !atoi(argv[2]))
        return 1;
    signal(SIGFPE, catcher);
    return abs(atoi(argv[1])) / atoi(argv[2]);
}
</code></pre>

The `catcher()` function is what we want to call.

The `main()` function returns the division of the second and third argument, first being the program name itself.

The program makes `signal` syscall which sets a handler function for a signal. This handle function gets called when the signal is received.

```c
sighandler_t signal(int signum, sighandler_t _handler);
```

In our case the signal is `SIGFPE` and the handler function is `catcher()`.

If we look at the man-page, it tells us when a `SIGFPE` signal is generated.

```
Integer division by zero has undefined result.  On some architectures it will generate a SIGFPE signal.  
(Also dividing the most negative integer by -1 may generate **SIGFPE**.)
```

So `SIGFPE` is generated during divisions, and there is a division occurring in our program.

We cannot perform division by zero because our `argv[2]` is the divisor and the program doesn't allow for it to be zero.

However, `argv[2]` can be -1. Which means we just have to figure out what the most negative integer is and pass it as `argv[1]`

```
level2@io:/levels$ ./level02 -4294967296 -1
source code is available in level02.c

WIN!
sh-4.3$
```

We can cat the password for the next level now.

```
sh-4.3$ cat /home/level3/.pass
```


---

# 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://kunalwalavalkar.gitbook.io/write-ups/netgarage-io/level-2.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.
