![]() |
INJECTION TESTING & OTHER NASTIES-- PART II |
In Part I the basic issues of SQL Injections was presented. However, there is more to do and their are other equally nasty issues.
In the previous discussion I suggested that only:
0 1 2 3 4 5 6 7 8 9 . -
be allowed in a numeric field. This is a "white list" because it is what we "allow". Further, in the discussion I suggested that
& > < / \ ; % = " # ( ) @ and '
NOT be allowed in a character field. This is a "black list" because we "disallow" these characters.
Generally, "white lists" are preferred over "black lists". This is because a black list assumes you know ALL the bad things that could happen -- YOU DON'T. Don't ever assume you know more than the intruder.
In addition the maximum length of the input MUST be a part of your defense. This is to prevent overflow exploits where the intruder overwhelms the application with data and then exploits the error condition. Unexpected long strings may indicate the intruder is attempting to add malicious code. So check the lengths as well as the contents.
When we encounter non-white list data or strings longer than allowed -- what action do we take. We can either:
Here we REMOVE the offending characters and let the process continue -- hoping we have removed the threat
OR
Here -- when we detect bad input -- we return an error message and TAKE NO ACTION. We make the user clean up the mess. We just dispose of the problem.
There are obviously cases where the user has made an innocent typing mistake. With a benevolent approach, we tidy up their mess (i.e., remove the offending characters) and proceed with the work. Alternatively, in the malevolent approach, we spit the hated evil data back to the user and refuse to do work until they conform to our wishes (this is the extreme "all data is evil" viewpoint).
I am personally of the malevolent school. While I may have to suffer some complaints from users (e.g., "you are awfully picky") -- I sleep better. An alternative to viewing ourselves as the malevolent Attila the Hun is to ascribe our motives to the benevolent Benjamin Franklin and his line that "an ounce of prevention is worth a pound of cure". Besides we can always tell the user WHY we are forbidding them from entering goofy data with a few computer disaster stories where a crummy user data blew up the system and everbody lost their job. They generally buy these stories. Secondly I want to impress upon the malicious intruder that I am watching their every move and intend to deal with them harsely -- Teddy Roosevelt's "speak softly and carry a big stick" approach.
Here is some code snippets to do edit tasks and discussions of the options available:
Another common attack is to insert a "meta refresh" tag into a database via a form input field. When the data is subsequently displayed in the browser, the page is redirected to another page. The syntax of the meta tag is:
<meta http-equiv="refresh" content="integer;url=some web site">
If encountered on the HTML page, the page is "refreshed". The content parameter provides two items:
All the attacker needs to do is get the tag into the database, then wait and ROFLMAO as the site's content is corrupted.
A guest book table might have the follwing structure:
idn int IDENTITY (1,1), fname varchar(20), lname varchar(20), comm varchar(70), date_time varchar(25) primary key (idn))1. Click here to create table, and load some data
Zowie!. This in and of itself is NOT a big deal. The malicious refresh does exactly what it is suppose to do. The real issue if that the INSERT program used in Step 4 uses parameters! True, there is no server side white-listing or black-listing, but to those that say that parameterization is adequate for preventing injections are wrong on this count. It may prevent an SQL injection because the parameters are NOT executed. But it does NOT prevent a meta refresh injection.
So either a white-list with no "<" and no ">" OR a blacklist with "<" and ">" will prevent the problem.
This is a variation where the attacker inserts script code into a table that gets executed. Consider the following script:
<script>javascript:alert(String.fromCharCode(39,72,69,76,76,79,39));</script>
NOTE: String.fromCharCode(39,72,69,76,76,79,39) = 'HELLO'
1. Click here to create table, and load some data
2. Click here to see the resulting table (Pop-ups must be turned OFF to see the table)
3. Click the "Close this Window" link on the popped up page.
4. Now let us make an entry in the quest book:
Again this injection was successful even though the SQL was parameterized. So either a white-list with no "<" and no ">" OR a blacklist with "<" and ">" will prevent the problem.