Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing null byte vulnerability

Tags:

security

php

I am helping my friend to finish his module for a website. From my first impression looking at his modules, I found some very dangerous things, but he says that this method is secure.

Part of the code :

session_start();

  if(isset($_POST['foo'])) 
  {
    $_SESSION['foo'] = $_POST['foo'];
  }

  if(isset($_SESSION['foo']))
  {
    $foo['foo']  = $_SESSION['foo'];
  }

  if(is_file("inc/". $foo['foo'] . "/bar.php")) {
    // code
  }
  else {
    // code
  }

Note : file (inc/test/bar.php) exists;

I wanted to test his code, and I sent the following requests :

POST :: foo => test/bar.php%00

POST :: foo => test/bar.php\0

curl_setopt($ch, CURLOPT_POSTFIELDS, 'foo=test/bar.php' . chr(0x00));

But none of these methods worked. Is that code really secure? and how could someone send a null byte to bypass it's security. I want to demonstrate to my friend that his code is not secure.

like image 291
John Avatar asked May 03 '13 19:05

John


People also ask

What is null byte attack?

Null Byte Injection is an active exploitation technique used to bypass sanity checking filters in web infrastructure by adding URL-encoded null byte characters (i.e. %00, or 0x00 in hex) to the user-supplied data.

How does a null byte work?

In C/C++, a null byte represents the string termination point or delimiter character which means to stop processing the string immediately. Bytes following the delimiter will be ignored. If the string loses its null character, the length of a string becomes unknown until memory pointer happens to meet next zero byte.

What is null byte value?

Encoding. In all modern character sets, the null character has a code point value of zero. In most encodings, this is translated to a single code unit with a zero value. For instance, in UTF-8 it is a single zero byte.

What is null byte extension?

Null byte is a bypass technique for sending data that would be filtered otherwise. It relies on injecting the null byte characters ( %00 , \x00 ) in the supplied data. Its role is to terminate a string.


1 Answers

I've found this solution, in short, it seems your code is somewhat vulnerable, and the sanitizing method is this:

There are a number of ways to prevent Poison Null Byte injections within PHP. These include escaping the NULL byte with a backslash, however, the most recommended way to do so is to completely remove the byte by using code similar to the following:

$foo['foo']= str_replace(chr(0), '', $foo['foo']);

I'm also not an expert in null-byte attacks, but this makes sense. Even more details here.

like image 140
Shomz Avatar answered Nov 12 '22 08:11

Shomz