473,804 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Server-side programming

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 | wt@nic.fi

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

Jul 18 '05 #1
4 2533
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(passw ord)

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
Jul 18 '05 #2
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(passw ord)

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
Jul 18 '05 #3
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
Jul 18 '05 #4
At some point, Alan Kennedy <al****@hotmail .com> 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(passw ord)

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)phy sics(dot)mcmast er(dot)ca
Jul 18 '05 #5

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

Similar topics

2
4604
by: Phil | last post by:
I am using a Pascal like language (Wealth-Lab) on W2K and call this server: class HelloWorld: _reg_clsid_ = "{4E797C6A-5969-402F-8101-9C95453CF8F6}" _reg_desc_ = "Python Test COM Server" _reg_progid_ = "Python.TestServer" _public_methods_ = _public_attrs_ = _readonly_attrs_ =
6
4461
by: Nathan Sokalski | last post by:
I want to set up SQL Server on Windows XP Pro so that I can use the database capabilities of ASP and IIS. I am probably using some incorrect settings, but I am not sure what they are. Here is what I am currently doing: When I run sqlservr.exe I see the following: 2003-12-19 15:51:28.20 server Microsoft SQL Server 2000 - 8.00.760 (Intel X8 6)
9
669
by: Grim Reaper | last post by:
My work let me put SQL Server 7.0 Enterprise Edition on my laptop. I have never setup a server from the beginning, so I am a little new at creating server groups. Alright, I am trying to create a server group. I right click on the "SQL Server Group" and make a name of "TEST" and put in the subgroup of "SQL Server Group". Next, I try to register the "TEST" server group. I right click on the "TEST" server group and New Server Group...
0
2823
by: Chris Halcrow | last post by:
Hi I've spent ALL DAY trying to re-install SQL Server 2000 on Windows XP. I continually get the error 'cannot configure server' just at the end of the installation. I've tried the following: - Removing SQL server from 'Program Files' folder following an unsuccessful attempt to re-install, and entirely removing the registry entry 'HKEY_CURRENT_USER > SOFTWARE > Microsoft > MSSQLServer', as well as the corresponding entry under...
0
4550
by: Zorba.GR | last post by:
IBM DB2 Connect Enterprise Edition v8.2, other IBM DB2 (32 bit, 64 bit) (MULTiOS, Windows, Linux, Solaris), IBM iSoft Commerce Suite Server Enterprise v3.2.01, IBM Tivoli Storage Resource Manager Express Edition v1.3.2 Win, IBM Tivoli System Automation v1.2.0 Linux, IBM Tivoli Workload Scheduler Virtualized Data Centers v8.2 , other IBM Tivoli CDs, WEBSPHERE EVERYPLACE MOBILE PORTAL v5.0 - ALTIUM , other IBM WebSphere Business CDs...
22
3309
by: EP | last post by:
When running my asp.net hosting service (asp.net without IIS), on server 2003 with IIS not installed, I get the following when trying to process a request. "System.DllNotFoundException: Unable to load DLL (aspnet_isapi.dll)." Of course the dll is able to be found, it's still in the framework directory and for grins I even put it in my service's local directory. This is apparantly server 2003 not allowing asp.net to be run if IIS was not...
2
4934
by: Mike | last post by:
Hi, I am strugling with a simple problem which I can't seem to resolve. I have an asp.net page which contains a server-control (flytreeview, which is a kind of a tree to be exact). The tree is being updated by some other process through remoting. When the page loads, I init the tree, and in my browser I can see the initialized tree. The problem is that every time that I receive update to tree from the remote process,
4
7305
by: coosa | last post by:
Hi, I was installing SQL Server on my machine and during installation my PC freezed. It happens frequently on my machine. So i tried after restarting to install it again and since then i always get the same error message: "An error occurred while creating one or more registry entries. Please see C:\WINDOWS\sqlstp.log for details. The problem could be caused by a low registry quota condition" I have tried to clean the registry and i...
1
6647
by: Peter | last post by:
I've purchased VS.NET 2005 Standard and have tried to install SQL Server 2005 Express, but get the following error in the error log. Please could someone help me.... Microsoft SQL Server 2005 Express Edition x86: Component Microsoft SQL Server 2005 Express Edition x86 returned an unexpected value. ***EndOfSession***? Microsoft SQL Server 2005 Express Edition x86: Component Microsoft SQL Server 2005 Express Edition x86 returned an...
14
3050
by: Developer | last post by:
Hello All, i have recently installed VS2005 and was trying to install SQL sever 2000. I have Win XP' SP2. But when I tried installing, it only installed client tools and not the database. Can anyone please help me with this as I want to install SQL server and also wouold be grateful, if you can suggest me any workaround to dealwith this problem.(Like should I install any new OS etc). Any help would be appreciated.
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9177
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.