Coder Profile - Show off your skills, get a coder profile.
 
 
 
Null Byte Poison - How it works...
Security
Hi ya guys,

Have you ever heard of "null byte" poison? Well if not... read on...

How to create a null byte

Hold down Ctrl + Shift and press the key with the @ symbol on it. In notepad++ for example it creates a little box with the word "nul" in it. That is a null byte.

What is a null byte

It's the first character in the ascii table i.e. 0 or chr(0) for example would return a null byte (in php).

Practical Purpose

A null byte in many languages is used to detect the end of a string. As opposed to storing an integer value in the first byte or two of the string stating the total length. A null byte on the other hand would just be placed at the end of the string... in just a single byte (saving space and does not need to keep count of the total characters in a string).

PHP, C, and many other languages use null bytes to indicate the end of a string.

So whats "null byte" poison?

It's when someone enters a string and places a null byte somewhere in it. This then chops off the text at the end of the string making it invisible to string manipulation functions... however it is still there in the processing stage! Sorry if that don't make much sense... so i did an example to help you understand...

Example of a null byte attach in PHP

For the sake of this example... pretend the # is a "Null Byte".

For this example pretend someone has uploaded a file that contains PHP code to delete a website! The filename is all that interests us here. The filename has been specifically crafted to contain null byte poison:

Name: delete.zip#.php

Then there is some code that checks the extension of the uploaded file to see if it is allowed to be uploaded. The function that checks the extension however only sees "delete.zip"!!! Because the function saw the null byte after .zip it cuts off processing the rest of the name. And so the extension return for that would be ".zip" instead of ".php".

Now this is where the problem is... because it saw .zip it allows the file to be uploaded and converted to the file system... however when saved on the file system on the server... the null byte is ignored and it is saved as: delete.zip.php

If the file is saved in a public php executable directory... the user only has to access the file via the web browser to execute the PHP contained in the script!

Thats generally how "Null Byte" poison works.

How do i protect against it

By simply replacing the null byte inside the string... with nothing! Here is an example in PHP:
Code Copy / Restore
  1. $filename = str_replace(chr(0), '', $string);
And it will just leave the last null byte on the end of the variable to keep it functioning properly

Kind regards,
Scott


Posted By VBAssassin
Please login to rate coding articles.

Click here to register a free account with us.
Comments
Please login to post comments.
 
Craige     Posted 23 Days Ago
 
 
Xixeon: A null byte in your browser: urlencode("\0") ==
"%00".

Scott: I'm trying to produce the Poison Null Byte on my local, PHP enabled
server in relation to file_exists, but I don't know how to save the file with a
null byte in the name. Care to shed some light on that matter?

My Code:

null.gif.php?file=null.gif
<?php
if (file_exists($_GET['file']) )
{
echo "passed. ", $_GET['file'], " exists";
}
else
{
echo "Failed. ", $_GET['file'], " does not exist";
}
?>

(this file checks for the existance of itself, obviously.)

So in theory, I know how it works, but I would like to know how to produce it in
practice. I know I want the file name to be "null.gif\0.php", but I
just don't know how to produce that file name.
 
VBAssassin     Posted 151 Days Ago
 
 
Yes you can type them. Open up notepad++ (since that shows a null byte as a little
icon). The hold down control and press @. You may need to hold down shift as well to
get the @ character.

That will type a null byte. Other ways are using a language to produce it such as
in PHP: chr(0)

Kind regards,
Scott
 
Xikeon     Posted 151 Days Ago
 
 
Hi,

Thanks for this article. It was very helpfull, I never even heard of this..

Though, how do people use it then? You can't copy a null byte? And in a
browser I can't type it..

Greetings
Mike
Page 1 of 1
More Articles By This Author
Null Byte Poison - How it works...
About Computer Viruses
Recently Posted "Security" Articles
Basic Steganography
How to prevent cookie stealers
Null Byte Poison - How it works...
Secure Hashing
DIC - Sick of XOR? Here's a better idea.
True Security
Polyalphabetic substitution
Shift cipher
About Computer Viruses
Creating Secure Passwords
Recently Rated "Security" Articles
Null Byte Poison - How it works...
Basic Steganography
Creating Secure Passwords
About Computer Viruses
How to prevent cookie stealers
Secure Hashing
True Security
DIC - Sick of XOR? Here's a better idea.
Polyalphabetic substitution
Shift cipher
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Internet & Web Sites (1)
Life In General (2)
Operating Systems (3)
Other (2)
Programming (48)
Security (10)
Software Development (4)
Web Development (12)
search Search Inside
Security
 
 
Part of the MyPingle Network
Development Blog :: Make A Donation :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents
Version 1.44.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved