request.getParameter(“username”) + “’
AND Password=’” +
request.getParameter(“password”) + “’”
stmt = Conn.prepareStatement(sql)
Rs = stmt.executeQuery()
Exploiting the problem is much simpler if you can access the source of the web
page. You should not be able to see this data, however there are many bugs that
allow you to view the source, and I’m sure there are still lots that have not yet been
discovered.
The problem with our ASP code is that we are concatenating our SQL statement
together without parsing out any single quotes. Parsing out single quotes is a good
first step, but its recommended that you actually use parameterized SQL statements
instead.
4 有效的输入
如果用户和密码设置为:
– Username: Bob
– Password: Hardtoguesspassword
sql语句: – SELECT * FROM WebUsers WHERE
Username=’Bob’ AND
Password=’Hardtoguess’
这是我们一个典型的检验机制在登陆到web site上时,然后通过select语句和数据库进行匹配,如果匹配建立,用户被鉴别。如果在我们的代码中记录集合为空,将准备一个无效的username或者password,登陆被拒绝。
5 黑客的输入
代替password的输入:
– Aa’ OR ‘A’=‘A‘
相应的sql语句:
– SELECT * FROM WebUsers WHERE
Username=’Bob’ AND Password=’Aa’ OR
‘A’=‘A’
黑客已经进入了数据库。
6 Selecting from other Tables
• To select data other than the rows from the
table being selected from
• UNION the SQL Statement with the
DBA_USERS view.
这是另一个例子取得数据从其他的表中,这与当前的查询无直接联系。最好的方法是查找屏幕中包含选项的动态列表。如果这个sql只是注意一个单值,黑客不能得到其他数据。
而且一些小技巧单一查询变成2个查询或者是把他们组合起来,这有点难道,你要匹配列数还有列的数据类型。然后一些服务器提供你一个错误的消息,使得这项是可行的。一些Error类似为:
Number of columns does not match
Or
2nd column in UNION statement does not match the type of the first statement.
7Sample ASP Page
Dim sql
Sql = “SELECT * FROM PRODUCT WHERE
ProductName=’” & product_name & “’”
Set rs = Conn.OpenRecordset(sql)
‘ return the rows to the browser
Once again we have the ASP page. An attacker does not really need this, but it does
make our lives easier for demonstration purposes. Once again we are not using
parameterized queries, but instead are concatenating a string to build our SQL
statement.
8 有效的输入:
• Set the product_name to :
– DVD Player
• The SQL Statement is now:
– SELECT * FROM PRODUCT WHERE
ProductName=’DVD Player’
9 黑客输入:
• Set the product_name to :
– test’ UNION select username, password from
dba_users where ‘a’ = ‘a
• The SQL Statement is now:
– SELECT * FROM PRODUCT WHERE
ProductName=’test’ UNION select username,
password from dba_users where ‘a’=‘a’
黑客可以从password的拷贝中获得一些杂乱的信息,来进行暴力破解。通过添加UNION的命令和第二语句,来得到dba_users表的内容。
10 防止SQl的注入
验证用户的输入
解析避免单一查询为双重查询
使用对象参数来设在参数
- Bind variables
回顾升级你的CGI脚步,ASP page,etc… 建议你对web设计者制订程序的方针,主要着重使用参数化查许和对sql语句的无连接字符串。
11 SQL Injection demo
ASP page, IIS web server ,Oracle database
五 Database Vulnerabilities
1 数据库安全问题
sqlnet.log
普遍的oracle安全问题
PL/SQL 的脆弱性。
主机操作系统
– Known Issues Installing Oracle
-Lockdown Protection Procedures
2 sqlnet.log
当来自一个机器的连接失败后会在目录下建立一个文件,记录失败的连接。
得到一些信息: username, IP,address, date, etc…
3 普遍的oracle安全问题
默认的passwords:
– SYS, SYSTEM, DBSNMP, OUTLN,MDSYS,SCOTT
Password的管理特性没有激活,通过pfiles文件执行复用参数。
– No password lockout by default
– No password expiration by default
Public角色有对ALL_USERS视图的允许权限。
4 PL/SQL的脆弱性
动态SQL的问题
– EXECUTE IMMEDIATE
– DBMS_SQL
允许用户传递sql语句中的参数危险性
这些问题和sql 注入问题几乎一样。
There are two ways to create SQL Statements on the fly in PL/SQL code – Execute
immediate and through the package DBMS_SQL.
5 动态sql语句的例子
CREATE PROCEDURE BAD_CODING_EXAMPLE ( NEW_PASSWORD
VARCHAR2 ) AS
TEST VARCHAR2;
BEGIN
-- DO SOME WORK HERE
EXECUTE IMMEDIATE 'UPDATE ' || TABLE_NAME || ' SET ' ||
COLUMN_NAME || ' = ''' || NEW_PASSWORD || '''‘ WHERE USERNAME=
= ''' || CURRENT_USER_NAME || ''';
END BAD_CODING_EXAMPLE;
有效的输入 from any OCI connection, ODBC connection, SQL*Plus, etc…
– EXEC BAD_CODING_EXAMPLE( ‘testabc’ );
• SQL Created
– UPDATE APPLICATION_USERS SET PASSWORD = ‘testabc’
WHERE USERNAME = ‘aaron’
这个要求有一个有效的帐户,和对过程的可执行。
黑客的输入
– EXEC BAD_CODING_EXAMPLE( ‘testabc’’, ADMIN=1,
FULL_NAME=‘’TEST’ );
SQL Created
– UPDATE APPLICATION_USERS SET PASSWORD = ‘testabc‘,
ADMIN=1, FULL_NAME=‘TEST’ WHERE USERNAME =
‘aaron’
通过在输入中附加一条语句,使得ADMIN列更新,使得用户成为应用程序的管理员。
注意我们应该附近另一列在末尾来出来最后的单一请求。
6 Getting to the operating system
在NT中oracle以localSystem身份,作为系统权限的一部分。
在Unix为oracle user来运行,对oralce的所用文件有权限。
进程:– UTL_FILE, UTL_HTTP
系统权利类似于create library
一但用于了对数据库的权限,就可以进入操作系统,可以使用很多进程– UTL_FILE是最致命的,给你对文件的读写权。不过UTL_FILE_DIR参数可以进行限制,管理员可以修改这个参数。Oracle运行你加载libraries到一个独立的进程空间使用EXTOROC可执行文件。一些共享libraries和DLL也是可行的。
7 操作系统
• Oracle 有许多tUID文件
• Oratclsh was setUID root
– TCL debugger
– Allowed you to run a script as root
– Change setuid immediately, even if you are not using
Setuid有很多问题,以前oracle捆绑了15setuid文件。最大的问题是TCL debugger,为应用程序设计在他们用于DBSNMP引擎前。这个debugger是steuid的而且由root拥有。所以很容易获得root权限,虽然有patch,不过还是有很多文件可以被利用的,这个问题即使是dbsnmp agent 不使用也会有。
Other SetUID files
• Were many until Oracle8i release 2
– Cmctl, tnslsnr, etc…
• Very important one – oracle
– Main database engine
• Relies on ORACLE_HOME directory
– To load the pwdSID.ora file
– Allows you to load a rogue database
8.1.6已经取消了许多SetUIDbits. Dbsnmp和oracle files然后保留,这两个都需要SetUID才能恰当的工作。推荐使用不同策略,可以是任何人不可以可执行权限除了所有者。Oracle是主要的可执行程序,如果你设置为可执行的话,任何在server有帐户的人可以开启一个实例