473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Pop quiz, hot shots!

In an attempt to get another discussion going about security in PHP, I've
put together this little exercise, the purpose of which is to illustrate how
bedevilingly tricky writing secure code can be sometimes.

Think you're up to the challenge? Here goes:

Bob is the author of Bobster, a online community web-app written in PHP. At
the core of it is its public message forums. As this sort of web-apps are
many aplenty, Bob is constantly adding new features in order to stay ahead
of the competition. One feature that users have oft requested is the ability
to enter messages in HTML. So he did a search on Google and found this nifty
Javascript HTML editor.

Bob is no bozo, of course. He is aware of the dangers of accepting HTML, the
biggest of which is the injection of malicious Javascript into pages of a
site. Since Bob knows this editor does not permit the entry of arbitrary
HTML code, he figured that a check on the HTTP referrer is a good first line
of defense, ensuring only form submissions that originate from the site are
processed. As a second line of defense, he added regular expression code
that looks for <script> tags in the message text.

The following is Bob's code:

$text = $_POST['msg'];
$user_id = $_SESSION['user_id'];
$referrer = parse_url($_SERVER['HTTP_REFERER']);

// the submission must come from a page on the server
if($referrer['host'] != $_SERVER['HTTP_HOST']) {

// remove Javscript from text
$text = preg_replace('/<script.*?>.*?<\/script>/si', '', $text);

// save the message
AddMessage($user_id, $text);
}

Now, analyse this code and note as many weaknesses as you can find, keeping
in mind that Bob's main objective is preventing Javascript injection. Extra
credits will be given for other security concerns, however. Those who answer
along the line of "he shouldn't accept HTML in the first place" will receive
no points. What interests us here is why not.
Jul 17 '05 #1
1 1680
Regarding this well-known quote, often attributed to Chung Leong's famous
"Fri, 9 Apr 2004 00:23:16 -0400" speech:
In an attempt to get another discussion going about security in PHP, I've
put together this little exercise, the purpose of which is to illustrate how
bedevilingly tricky writing secure code can be sometimes.

Think you're up to the challenge? Here goes:

Bob is the author of Bobster, a online community web-app written in PHP. At
the core of it is its public message forums. As this sort of web-apps are
many aplenty, Bob is constantly adding new features in order to stay ahead
of the competition. One feature that users have oft requested is the ability
to enter messages in HTML. So he did a search on Google and found this nifty
Javascript HTML editor.

Bob is no bozo, of course. He is aware of the dangers of accepting HTML, the
biggest of which is the injection of malicious Javascript into pages of a
site. Since Bob knows this editor does not permit the entry of arbitrary
HTML code, he figured that a check on the HTTP referrer is a good first line
of defense, ensuring only form submissions that originate from the site are
processed. As a second line of defense, he added regular expression code
that looks for <script> tags in the message text.
I'll bite. I need to test my security skills, anyhow.

The following is Bob's code:

$text = $_POST['msg'];
Well, Bob did learn one thing in PHP class... no autoglobals. There are
also no "assumed unset" variables in case autoglobals are on. Good Bob.
$user_id = $_SESSION['user_id'];
I'll assume that the session has already been started and validated?
$referrer = parse_url($_SERVER['HTTP_REFERER']);
FATAL!: Never trust the referrer. This header is given by the client, and
can be swapped out as easily as downloading a Mozilla plugin. This, the
Useragent, and the remote IP are completely useless as mission-critical
data. In fact, I wouldn't trust anything that the client gives me, short of
a good, solid set of $_SESSION[] variables I set earlier.

THE SOLUTION!: The code should work securely no matter what it's fed. The
Web is stateless, so the security should not rely on what happened in a
foggy past. If it's absolutely necessary, statefulness should depend on a
unique, irrelevant, quickly expiring, and heavily random session-ID.

// the submission must come from a page on the server
if($referrer['host'] != $_SERVER['HTTP_HOST']) {

// remove Javscript from text
$text = preg_replace('/<script.*?>.*?<\/script>/si', '', $text);
FATAL!: Think onLoad, onMouseOver, and <A HREF="javascript:...">. There's
more than one way to load a script.

FATAL!: This doesn't drop the OBJECT or APPLET tags. Prepare to enter a
world of ActiveX and Java pain. Also, allowing unauthorized DIV, SPAN, or
STYLE tags (STYLE as a tag, not a parameter) can cause visual headaches on
the rest of the page.

WARNING!: Although it might not be HTML strict, some browsers might
interpret lines like: "< SCRIPT..." (with a space) or "<SCRIPT
LANGUAGE="JavaScript" SRC="sourcefile.js">" (SRC without a closing tag) as
parsable. It's best to just dump any SCRIPT tags, regardless.

WARNING!: There are a few other tags Bob should watch out for: OBJECT and
IFRAME tags can load an outside page in a small "internal frame", right
along with all it's malicious script. Browser tainting prevents the code
from doing much if it's offsite, but they can still run some annoying
attacks (ActiveX controls, popups, the JavaScript Looping Alert Box of
Doom). They *might* be able to hijack Bob's page, too, with a "Bookmarklet"
style <A HREF="javascript:javascriptCodeGoesHere()"> link. (Actually, the
OBJECT tag can load an ActiveX plugin, too, can't it... time to add another
"FATAL!")

THE SOLUTION!: Match the code against an Array of known ACCEPTABLE tags.
Anything else, convert the < and > to &lt; and &gt;. I'd recommend this
over just an empty-replace. Otherwise, entire great swaths of text might be
wiped out by having an errant < near the beginning and an errant > near the
end of a comment (as ASCII-Art arrows, perhaps).

Once acceptable tags are weeded out, process each tag. Tags such as <B> or
<I> should be abstracted without parameters, and complex tags should only
have the parameters Bob wants to allow. Some clever regexps should do the
trick. Remember that parameters can be signified by double-quotes, single
quotes, or by just being after an equal-sign.

Bob should also make sure all tags are closed, to prevent the "Didn't close
<I> and the whole page is italic" problem. Other visual checks would be
anti-page-widening filters and overlarge image-size checking.

// save the message
AddMessage($user_id, $text);
AddMessage(). We can only hope.

Remember, Bob. Don't store the data in a file with an extension that can
get parsed or CGI'd by Apache, and never, EVER just include() the file,
expecting innocuous HTML.
}

Now, analyse this code and note as many weaknesses as you can find, keeping
in mind that Bob's main objective is preventing Javascript injection. Extra
credits will be given for other security concerns, however. Those who answer
along the line of "he shouldn't accept HTML in the first place" will receive
no points. What interests us here is why not.

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sketcher | last post by:
Hi, I am trying to create a quiz, Code is as follows: <html> <head> <title>Quiz</title> </head> <BODY> <Center><TABLE cellSpacing=3 cellPadding=0 border=0>
5
by: Vandana Rola | last post by:
Hello Everyone, I am a beginner in Javascript.I want to create fun quiz tool using javascript (no database). The feature of that tool is every question has five choices. The user needs to select...
4
by: DAL | last post by:
I want to build my kid a program that cycles through questions (using a label for the question), and lets him choose one of two radio buttons for the right answer. How do I get every set of...
0
by: philip | last post by:
hello, i am now developing a quiz application for my school using ASP.NET and SQL SERVER 2005, here is a senario: It will have 20 students for taking a quiz in a classroom, they have to answer...
2
by: kenny | last post by:
I'm making a quiz to be posted on the internet. but I'm facing difficulties in finding a simple timer for the quiz (unlimited timing) which will keep on running regardless to the change of the page...
0
NoPeasHear
by: NoPeasHear | last post by:
I don't know what I am doing wrong... I used this tutorial... http://www.permadi.com/tutorial/flashMXQuiz/index.html It works with their quiz.xml file, but when I add an option for multiple...
3
by: Raqueeb Hassan | last post by:
Hello, I was helping one of my friend's school on setting up a online quiz system. They have the AMP systems to host php+mysql. The quiz script/software should have the following features: a....
1
by: korr | last post by:
Hi there, i'm trying to develop a quiz in flash. Searching on the net, I found a quiz in flashkit from sephiroth.it by Alessandro Crugnola. His quiz has a script that puts the questions and the...
5
nomad
by: nomad | last post by:
Hello Everyone: Just want to ask how easy would it be to build a quiz in Java. I have not use Java for a few months (5). Quiz would need the following: 1. T or F and mulitiple question, possible...
3
by: empiresolutions | last post by:
I am building a app that creates quizzes. This is how it goes - - Create Quiz - Provide up to 10 different types of Quiz Results - Give up to 50 Questions - Each Question has up to 10 possible...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.