Nebula exploit exercises walkthrough – level11

The /home/flag11/flag11 binary processes standard input and executes a shell command.

There are two ways of completing this level, you may wish to do both 🙂

Now it gets interesting. This is the first bit of code where it isn’t obvious what the intent is from a quick glance.

I think I have found three ways to get this to execute getflag, though one is just a variation of another.

The code reads from stdin, then checks for “Content-Length: “, reads a length integer, and then processes this.

There are a number of paths from this point. If the length is less than the buf length (1024), then fread is called. Then there is a bug.

This is what happens on this code path:

But later on:

From the man page of fread:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

The function fread() reads nmemb elements of data, each size bytes
long, from the stream pointed to by stream, storing them at the loca‐
tion given by ptr.

fread() and fwrite() return the number of items successfully read or
written (i.e., not the number of characters).

Whilet both read in the same data, the return values will be different. The first will return 1, the second will return the number of chars read.

This means the only way to get to process with the length less than 1024 is to set the length to 1. This restricts our options a fair bit.

We’ll try it out though:

As expected, the value we pass (E, arbitrary choice) gets “processed” to become D. system is then called, but because we can only provide a single character, we can’t null terminate the command, and we get some random values after.

We can see these values vary each time we run it:

One thing that does happen though is that, by chance, we end up with a null being in the right place:

This is pure luck. The rest of buffer is uninitialized and nulls are common in uninitialised memory.

If we now symbolic link D to /bin/getflag, and alter the path so it runs D when the null is in the right place:

Hmmph. Why is it not the flag account? I think this is a bug – the call to system isn’t preceded by setresuid/setresgid, so anything it runs will run as the real UID (level11) instead of the effective UID (flag11).

Co-incidentally, I had recently read of a technique to fill uninitialised memory. It’s virtually useless in the real world – using uninitalised memory is indicative of much bigger issues. It’s interesting though, so let’s try it here.

This technique uses an environment variable called LD_PRELOAD. This is commonly used to override library functions for debugging (or exploits!). When the linker starts up, it reads the entirity of LD_PRELOAD onto the stack and then doesn’t clean up afterwards. This means we can initialise the memory to something under out control:

i.e. fill the stack with one thousand /bin/getflags.

Then when we run flag11 with length of 1, it will almost certainly have this in the buffer already:

Again the same issue with suid/system, but I think it counts.

Now we need to come back to the length being 1024 or more. What happens here?

There is a really simple encryption function:

We can easily build the reverse of this in Python and output a string:

(note, that I terminated the command with newline (x0a) to start with, which was causing this to fail)

We can then pipe this into the executable, to run the command:



Whilst playing around with this level, I thougut there might be something I could do with the random path/filename that is used when the content length is 2014 or greater.

The filename is normally of the form:

As seen from strace. This is PID (process ID) with a “random” string.

We can gain control of this string, the filename, and stop it from being deleted. This uses LD_PRELOAD, but for it’s genuine use.

First, we must check that the executable is dynamically linked:


Now we need to create a c file to override the functions we want – random(), unlink() and getpid():

The we compile it into a library, set LD_PRELOAD, and then run the executable:

And now we have control of the filename, and it is preserved rather than deleted.

Not of any real use, but a handy technique.

5 thoughts on “Nebula exploit exercises walkthrough – level11

  1. Permalink  ⋅ Reply


    June 13, 2014 at 9:04am

    Heh, I initially thought you may be able to specify a negative Content-Length and overflow buf on the stack, but the (length < sizeof(buf)) test fails when length is promoted to an unsigned.

    You shouldn't be able to use LD_PRELOAD with setuid binaries like that – otherwise you could just override e.g. random() to call system("getflag") and you'd be done. Do you know what's going on there?

    bob@bob:/tmp$ id
    uid=1000(bob) gid=1000(bob) groups=1000(bob)
    bob@bob:/tmp$ cat prog.c
    int main() {
        printf("%i\n", random());
    bob@bob:/tmp$ cat lib.c
    int random() { return 123; }
    bob@bob:/tmp$ gcc -o prog prog.c
    prog.c: In function ‘main’:
    prog.c:3:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
    bob@bob:/tmp$ gcc -shared -fPIC lib.c -o lib.o
    bob@bob:/tmp$ export LD_PRELOAD=$PWD/lib.o
    bob@bob:/tmp$ ./prog
    bob@bob:/tmp$ sudo chown dan:dan prog; sudo chmod 4755 prog
    bob@bob:/tmp$ ./prog

    • Permalink  ⋅ Reply


      June 13, 2014 at 9:12am

      Ah, I can see why it works with strace; from strace(1):

      -u username Run command with the user ID, group ID, and supplementary groups of username. This option is only useful when running as root and enables the correct execution of setuid and/or setgid binaries. Unless this option is used setuid and setgid programs are executed without effective privileges.

      • Permalink  ⋅ Reply


        October 29, 2016 at 6:46pm

        Could you further explain why it works? I still share your first impression that it LD_PRELOAD shouldn’t work with a suid binary.

        • Permalink  ⋅ Reply


          November 5, 2016 at 5:05pm

          Nvm, I got it.

          “Unless this option is used setuid and setgid programs are executed without effective privileges.”

          This option isn’t set, so LD_PRELOAD works due to the lack of elevated privileges.

  2. Permalink  ⋅ Reply


    March 26, 2016 at 8:52am

    The problem of this task is in experiments by the author:

    From the disassemler:

    int process(char *command, int length)
    gid_t gid;
    uid_t uid;
    unsigned int key;
    int i;

    key = (unsigned __int8)length;
    for ( i = 0; i < length; ++i )
    command[i] ^= key;
    key -= command[i];
    gid = getgid(); // w00t ??
    uid = getuid();
    return system(command);

Leave a Reply

Your email will not be published. Name and Email fields are required.