Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Server-side programming

Reply
Thread Tools

Server-side programming

 
 
Timo Virkkala
Guest
Posts: n/a
 
      09-19-2003
I'm creating a system with Python CGIs, that connect to a database. I'm
wondering about input validation. Of course I will check the length of
the passed parameters, to (hopefully) prevent any DOS attacks. What else
do I need to check? Do I need to remove any SQL from the inputs?
Anything else I might have overlooked?

--
Timo Virkkala |

"In the battle between you and the world, bet on the world."

 
Reply With Quote
 
 
 
 
Alan Kennedy
Guest
Posts: n/a
 
      09-21-2003
Timo Virkkala wrote:
> I'm creating a system with Python CGIs, that connect to a database. I'm
> wondering about input validation. Of course I will check the length of
> the passed parameters, to (hopefully) prevent any DOS attacks. What else
> do I need to check? Do I need to remove any SQL from the inputs?
> Anything else I might have overlooked?


You might not need to remove SQL from your field values. Doing so
would probably be a non-trivial string parsing exercise.

Most "SQL injection" attacks would be where a cracker hopes that you
are going to embed the contents of "username" and "password" fields
right into a string containing an SQL query, like so

mySQLString = """
select *
from users
where uname = "%s" and password = "%s"
""" % (username, password)

If the query returns a non-zero number of rows, then that
username/password combination is deemed to be valid.

The problem comes when the cracker deliberately subverts the content
of the fields, supplying values like these, for example

username='alan'
password='" or 0=0 or password="'

Which when embedded into the SQL query string gives the following
final SQL query:

select *
from users
where uname = "alan" and password = "" or 0=0 or password=""

Which will return at least 1 row, assuming that "alan" is a valid
username.

Now, trying to parse the syntax of the password field, looking for SQL
keywords such as "or", could be complex: there are quite a few
possible ways in which the query can be textually subverted.

AFAIK, the most effective way to prevent such attacks is to disable
any quote characters that may be present in the password, so that they
are treated as a part of the password string, not as delimiters in the
SQL query string. For example

import re
password = re.escape(password)

Which for the values given above would now give an SQL query of

select *
from users
where uname = "alan" and password = "\"\ or\ 0\=0\ or\ password\=\""

Does anyone know of a more effective approach to preventing SQL
injection attacks?

Another potential attack is the "Cross Site Scripting (XSS) Attack",
whereby the attacker inserts javascript into a field value, which is
then embedded into the HTML transmitted by a web app to another user,
for example as a post in a message board.

This hostile javascript can do any number of nasty things to users
browsers including stealing cookies, or url-rewritten session IDs, so
that the innocent users login session can be hijacked and abused.

Here is an article about XSS attacks.

http://www.cgisecurity.net/articles/xss-faq.shtml

AFAIK, the most effective solution to preventing XSS attacks is to ban
HTML/tags/javascript from being inserted into text strings that will
be displayed as part of a HTML page. This could be done by

1. Parsing the string as HTML, and stripping out <script> tags.
2. Escaping (in the HTML sense) all field inputs, to disable markup
special characters such as "<", ">", etc.

Does anyone know of other potential textual attacks against web pages,
input forms and field values?

It would be really nice to have a central, python focussed, repository
of these attack techniques, and how they can be prevented with python
code. Does anyone know of such a page?

If we get enough information from this thread, I might start up a page
about the subject.

regards,

--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
 
Reply With Quote
 
 
 
 
Alan Kennedy
Guest
Posts: n/a
 
      09-21-2003
Timo Virkkala wrote:
> I'm creating a system with Python CGIs, that connect to a database. I'm
> wondering about input validation. Of course I will check the length of
> the passed parameters, to (hopefully) prevent any DOS attacks. What else
> do I need to check? Do I need to remove any SQL from the inputs?
> Anything else I might have overlooked?


You might not need to remove SQL from your field values. Doing so
would probably be a non-trivial string parsing exercise.

Most "SQL injection" attacks would be where a cracker hopes that you
are going to embed the contents of "username" and "password" fields
right into a string containing an SQL query, like so

mySQLString = """
select *
from users
where uname = "%s" and password = "%s"
""" % (username, password)

If the query returns a non-zero number of rows, then that
username/password combination is deemed to be valid.

The problem comes when the cracker deliberately subverts the content
of the fields, supplying values like these, for example

username='alan'
password='" or 0=0 or password="'

Which when embedded into the SQL query string gives the following
final SQL query:

select *
from users
where uname = "alan" and password = "" or 0=0 or password=""

Which will return at least 1 row, assuming that "alan" is a valid
username.

Now, trying to parse the syntax of the password field, looking for SQL
keywords such as "or", could be complex: there are quite a few
possible ways in which the query can be textually subverted.

AFAIK, the most effective way to prevent such attacks is to disable
any quote characters that may be present in the password, so that they
are treated as a part of the password string, not as delimiters in the
SQL query string. For example

import re
password = re.escape(password)

Which for the values given above would now give an SQL query of

select *
from users
where uname = "alan" and password = "\"\ or\ 0\=0\ or\ password\=\""

Does anyone know of a more effective approach to preventing SQL
injection attacks?

Another potential attack is the "Cross Site Scripting (XSS) Attack",
whereby the attacker inserts javascript into a field value, which is
then embedded into the HTML transmitted by a web app to another user,
for example as a post in a message board.

This hostile javascript can do any number of nasty things to users
browsers including stealing cookies, or url-rewritten session IDs, so
that the innocent users login session can be hijacked and abused.

Here is an article about XSS attacks.

http://www.cgisecurity.net/articles/xss-faq.shtml

AFAIK, the most effective solution to preventing XSS attacks is to ban
HTML/tags/javascript from being inserted into text strings that will
be displayed as part of a HTML page. This could be done by

1. Parsing the string as HTML, and stripping out <script> tags.
2. Escaping (in the HTML sense) all field inputs, to disable markup
special characters such as "<", ">", etc.

Does anyone know of other potential textual attacks against web pages,
input forms and field values?

It would be really nice to have a central, python focussed, repository
of these attack techniques, and how they can be prevented with python
code. Does anyone know of such a page?

If we get enough information from this thread, I might start up a page
about the subject.

regards,

--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
 
Reply With Quote
 
Neil Hodgson
Guest
Posts: n/a
 
      09-21-2003
Alan Kennedy:

> AFAIK, the most effective way to prevent such attacks is to disable
> any quote characters that may be present in the password, so that they
> are treated as a part of the password string, not as delimiters in the
> SQL query string. For example
> ...
> Does anyone know of a more effective approach to preventing SQL
> injection attacks?


Separate your parameters from the SQL and rely on the database to perform
the substitution like this:
c.execute( \
"select * from users where uname=:1 and pw=:2", \
(username, password))

This may also improve performance by allowing the database to cache the
preparation of the statement as it stays constant.

Neil


 
Reply With Quote
 
David M. Cooke
Guest
Posts: n/a
 
      09-21-2003
At some point, Alan Kennedy <> wrote:

> Timo Virkkala wrote:
>> I'm creating a system with Python CGIs, that connect to a database. I'm
>> wondering about input validation. Of course I will check the length of
>> the passed parameters, to (hopefully) prevent any DOS attacks. What else
>> do I need to check? Do I need to remove any SQL from the inputs?
>> Anything else I might have overlooked?

>
> You might not need to remove SQL from your field values. Doing so
> would probably be a non-trivial string parsing exercise.
>
> Most "SQL injection" attacks would be where a cracker hopes that you
> are going to embed the contents of "username" and "password" fields
> right into a string containing an SQL query, like so
>
> mySQLString = """
> select *
> from users
> where uname = "%s" and password = "%s"
> """ % (username, password)
>
> If the query returns a non-zero number of rows, then that
> username/password combination is deemed to be valid.
>

[snipped useful info on doing SQL injections]
> AFAIK, the most effective way to prevent such attacks is to disable
> any quote characters that may be present in the password, so that they
> are treated as a part of the password string, not as delimiters in the
> SQL query string. For example
>
> import re
> password = re.escape(password)
>
> Which for the values given above would now give an SQL query of
>
> select *
> from users
> where uname = "alan" and password = "\"\ or\ 0\=0\ or\ password\=\""
>
> Does anyone know of a more effective approach to preventing SQL
> injection attacks?


The problem is you're trying to create the entire query string. This
means that *you* must be sure what is valid and what's invalid; what
needs quoting, and what doesn't. Are you sure the above using
re.escape will work properly for SQL queries, since it was designed
for regular expressions? Are all corner cases covered? etc.

The best way to do this is to allow the database module to do the
quoting; this is explicictly supported by Python's DB-API v2
specificiation (available as PEP 246 [1]). Most (if not all that
you'll probably use...) database modules for python conform to this.

Here's a concrete example using PySQLite: [untested code]

import sqlite
db = sqlite.connect('database.db')
cursor = db.cursor()

cursor.execute('''select * from users
where uname = %(uname)s and
password = %(password)s''',
{'uname' : uname, 'password' : password})

row = cursor.fetchone()
if row is None:
print "Access denied"

Note how the parameters are passed as separate arguments to
cursor.execute; the sqlite module takes care of escaping them. Note
that not all database modules support this style of quoting; check out
PEP 249 and the documentation of your specific module.

[1] http://python.org/peps/pep-0249.html

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C (functional programming) VS C++ (object oriented programming) Joe Mayo C Programming 168 10-22-2007 01:00 AM
Can Your Programming Language Do This? Joel on functional programming and briefly on anonymous functions! Casey Hawthorne Python 4 08-04-2006 05:23 AM
Wireless PEAP/MSCHAPV2 client programming question Jim Howard Wireless Networking 6 07-02-2005 11:53 AM
systems programming versus application programming Matt Java 35 07-22-2004 08:10 AM
XSLT programming cameron Firefox 0 01-04-2004 10:51 PM



Advertisments