Dear Monks
I have this situation in which I have to create a table, but the user defines the column names and column definitions.
Anyway, I know now that the column name is handled by quote_identifier(..), but the definition, for example, a user might give me:
VARCHAR(100) ); DROP DATABASE mysql; --'
is a lot harder to validate. I couldn't find a function that does this.
So I wrote the following test script to detect this kind of SQL injection:
#! /usr/bin/perl -l
use strict ;
use warnings ;
# USER INPUT
my $SQL = 'VARCHAR(100) ); DROP DATABASE mysql; --';
validate_column_definition( $SQL ) ;
my $query = sprintf( 'CREATE TABLE test ( a %s, b INT )', $SQL ) ;
print $query ;
sub validate_column_definition {
my $SQL = shift ;
# remove all strings (DEFAULT 'str' OR COMMENT 'str')
$SQL =~ s/(['][^']+['])//g ; # remove quoted strings
$SQL =~ s/(["][^"]+["])//g ; # remove quoted strings
if ( $SQL =~ /#|--|\/\*|;|'|`|"/ ) { # detect invalid charact
+ers
print "SQL INJECTION: $SQL\n" ;
}
}
I think this could work, but I'm not sure; does it detect all possible attacks and doesn't it break legal SQL?
Any help would really be appreciated
cheers
LuCa
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.