Nebula Level 18: Exploiting a C Program

Exploring file descriptor exhaustion, password file overwrites, and other vulnerabilities in Nebula Level 18.

Analyse the C program, and look for vulnerabilities in the program. There is an easy way to solve this level, an intermediate way to solve it, and a more difficult/unreliable way to solve it.

To do this level, log in as the level18 account with the password level18. Files for this level can be found in /home/flag18.

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <getopt.h>

struct {
  FILE *debugfile;
  int verbose;
  int loggedin;
} globals;

#define dprintf(...) if(globals.debugfile) \
  fprintf(globals.debugfile, __VA_ARGS__)
#define dvprintf(num, ...) if(globals.debugfile && globals.verbose >= num) \
  fprintf(globals.debugfile, __VA_ARGS__)

#define PWFILE "/home/flag18/password"

void login(char *pw)
{
  FILE *fp;

  fp = fopen(PWFILE, "r");
  if(fp) {
      char file[64];

      if(fgets(file, sizeof(file) - 1, fp) == NULL) {
          dprintf("Unable to read password file %s\n", PWFILE);
          return;
      }
                fclose(fp);
      if(strcmp(pw, file) != 0) return;
  }
  dprintf("logged in successfully (with%s password file)\n",
      fp == NULL ? "out" : "");

  globals.loggedin = 1;

}

void notsupported(char *what)
{
  char *buffer = NULL;
  asprintf(&buffer, "--> [%s] is unsupported at this current time.\n", what);
  dprintf(what);
  free(buffer);
}

void setuser(char *user)
{
  char msg[128];

  sprintf(msg, "unable to set user to '%s' -- not supported.\n", user);
  printf("%s\n", msg);

}

int main(int argc, char **argv, char **envp)
{
  char c;

  while((c = getopt(argc, argv, "d:v")) != -1) {
      switch(c) {
          case 'd':
              globals.debugfile = fopen(optarg, "w+");
              if(globals.debugfile == NULL) err(1, "Unable to open %s", optarg);
              setvbuf(globals.debugfile, NULL, _IONBF, 0);
              break;
          case 'v':
              globals.verbose++;
              break;
      }
  }

  dprintf("Starting up. Verbose level = %d\n", globals.verbose);

  setresgid(getegid(), getegid(), getegid());
  setresuid(geteuid(), geteuid(), geteuid());

  while(1) {
      char line[256];
      char *p, *q;

      q = fgets(line, sizeof(line)-1, stdin);
      if(q == NULL) break;
      p = strchr(line, '\n'); if(p) *p = 0;
      p = strchr(line, '\r'); if(p) *p = 0;

      dvprintf(2, "got [%s] as input\n", line);

      if(strncmp(line, "login", 5) == 0) {
          dvprintf(3, "attempting to login\n");
          login(line + 6);
      } else if(strncmp(line, "logout", 6) == 0) {
          globals.loggedin = 0;
      } else if(strncmp(line, "shell", 5) == 0) {
          dvprintf(3, "attempting to start shell\n");
          if(globals.loggedin) {
	              execve("/bin/sh", argv, envp);
              err(1, "unable to execve");
          }
          dprintf("Permission denied\n");
      } else if(strncmp(line, "logout", 4) == 0) {
          globals.loggedin = 0;
      } else if(strncmp(line, "closelog", 8) == 0) {
          if(globals.debugfile) fclose(globals.debugfile);
          globals.debugfile = NULL;
      } else if(strncmp(line, "site exec", 9) == 0) {
          notsupported(line + 10);
      } else if(strncmp(line, "setuser", 7) == 0) {
          setuser(line + 8);
      }
  }

  return 0;
}

There are a few ways of solving this:

  1. exhausting all file descriptors, because if the program can’t open the password file, it will login us automatically.
  2. using the site exec we can write data on any file, so we can edit the password file so we know the password.
  3. There is also a fformat string and overflow vulnerability, but there is ASLR and also FORTIFY_SOURCE, it is doable, TODO

Exhausting all file descriptors

We check the amount of file descriptors $ ulimit -n which is 1024 So the process will use already 3 (stdin, stdout and stderror). So we need to open 1021, so the nexst one won’t be able to open.

We could create a C program opening a random file, but it is not even needed, we can usen the login function, which opens the file but doesn’t close it. Then after login in, if we shell, it will be error

$ python -c "print 'login\n'*1021+'closelog\nshell'" | ./flag18 --debug /dev/tty

We need the --debug cause the shell executes sh with args from the program, so it breaks -d is not a sh argument, but —debug is, and this way, both commands are happy (-d is poorly parsed by the vulnerable program, but for sh it’s still --debug.

Have to use /dev/tty so we get the shell back to our terminal ( I guess)

Overwriting the password file

Here there is a vulnerability in the site exec that lets me write any file, so we an overwrite the password file and control the password to pop the shell. There is only one caveat, the \n that will be always present, and login will strip, we can’t input it. The way to fix it, is play with 2 file writes at the same time, we will do the first one with -v which will create level =1, and then another with -v*10 which will create level= 10, this way the first one (which we sent to bg with ctrl+z will have the write cursor just before the 0 of the second one, but now there is not new line.

Now we can just site exec a this way we will have Starting up. Verbose level = 10a