科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道数据库安全基础入门知识简介(3)

数据库安全基础入门知识简介(3)

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

现在,为了使得网站能够提供各种各样动态的内容,数据库已经成为所有基于 WEB 应用程序最重要的组件。由于一些十分敏感或者保密的信息可能会存储在这样的数据库中,因此,您需要非常慎重的考虑如何保护它们。

作者:wenzhai 来源:ccidnet 2007年9月11日

关键字: 安全基础 数据库 SQL Server SQL Server 各版本

  • 评论
  • 分享微博
  • 分享邮件

例子 15-7. 列出文章,以及一些密码(任何数据库服务器)

$query  = "SELECT id, name, inserted, size FROM products
                  WHERE size = '$size'
                  ORDER BY $order LIMIT $limit, $offset;";
$result = odbc_exec($conn, $query);   
The static part of the query can be combined with another SELECT statement which 
reveals all passwords: 
'
union select '1', concat(uname||'-'||passwd) as name, '1971-01-01', '0' from usertable;
--

If this query (playing with the ' and --) were assigned to one of the variables used in $query, the query beast awakened.

SQL UPDATEs are also subject to attacking your database. These queries are also hreatened by chopping and appending an entirely new query to it. But the attacker might fiddle with the SET clause. In this case some schema information must be possessed to manipulate the query successfully. This can be acquired by examing the form variable names, or just simply brute forcing. There are not so many naming convention for fields storing passwords or usernames.

例子 15-8. 利用重设密码来获取更多的权限(任何数据库服务器)

$query = "UPDATE usertable SET pwd='$pwd' WHERE uid='$uid';"; 
But a malicious user sumbits the value ' or uid like'%admin%'; -- to $uid to change the 
admin's password, or simply sets $pwd to "hehehe', admin='yes', trusted=100 " (with a 
trailing space) to gain more privileges. Then, the query will be twisted: 
// $uid == ' or uid like'%admin%'; --
$query = "UPDATE usertable SET pwd='...' WHERE uid='' or uid like '%admin%'; --";
// $pwd == "hehehe', admin='yes', trusted=100 "
$query = "UPDATE usertable SET pwd='hehehe', admin='yes', trusted=100 WHERE ...;"  

A frightening example how operating system level commands can be accessed on some 
database hosts. 例子 15-9. 攻击数据库主机的操作系统 (MSSQL Server)

$query  = "SELECT * FROM products WHERE id LIKE '%$prod%'";
$result = mssql_query($query);  
 
If attacker submits the value a%' exec master..xp_cmdshell 'net user test testpass 
/ADD' -- to $prod, then the $query will be: 


$query  = "SELECT * FROM products
                    WHERE id LIKE '%a%'
                    exec master..xp_cmdshell 'net user test testpass /ADD'--";
$result = mssql_query($query);

MSSQL Server executes the SQL statements in the batch including a command to add a new user to the local accounts database. If this application were running as sa and the MSSQLSERVER service is running with sufficient privileges, the attacker would now have an account with which to access this machine.

注: Some of the examples above is tied to a specific database server. This does not mean that a similar attack is impossible against other products. Your database server may be so vulnerable in other manner.

预防的技巧

You may plead that the attacker must possess a piece of information about the database schema in most examples. You are right, but you never know when and how it can be taken out, and if it happens, your database may be exposed. If you are using an open source, or publicly available database handling package, which may belong to a content management ystem or forum, the intruders easily produce a copy of a piece of your code. It may be also a security risk if it is a poorly designed one.

These attacks are mainly based on exploiting the code not being written with security in mind. Never trust on any kind of input, especially which comes from the client side, even though it comes from a select box, a hidden input field or a cookie. The first example shows that such a blameless query can cause disasters.

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章