Sleep

The thread is waiting for the client to send a new statement to it.

https://dev.mysql.com/doc/refman/8.0/en/thread-commands.html

https://www.saotn.org/mysql-sleep-attacks/

Table of Contents

How to put a MySQL server to sleep()

MySQL sleep() command injection attacks: how not validating your PHP user input can lead to Denial of Service (DoS) attacks against websites and back-end database servers. Simply by putting “AND sleep(3)” in the address bar… Happy SQL injection!

Investigating PHP/MySQL sleep() attacks

The other day I noticed several hung queries (SELECT statements) on one of the MySQL database servers under my control. All hung queries had in common they were running for a very long time. And, mysqladmin processlist -v showed a sleep() command in the query.

Given the casing of the MySQL sleep command (“SLeeP”), this was obviously done by an sql injection tool of some kind. I could simply kill the MySQL queries and threads and be done with it, but I wanted to be sure this MySQL sleep() attack couldn’t happen again.

After killing the MySQL threads I took a quick look at the website. Given I had both the executed query and website HTTP log files available for my investigation, I quickly located the vulnerable PHP script.

The vulnerable line of PHP code was:

$id = ( isset( $_GET["id"] ) ? $_GET["id"] : 0 );

Where $id was directly used in a MySQL query.

Did you notice the lack of input validation? As long as $_GET["id"] is provided in the URL, it’s not set to 0. Through my browsers address bar I can add %20AND%20sleep(3) to the URL, to make this query hung, on the server for quite some time.

The MySQL sleep(3) command is executed for every record the query finds. The website’s PHP code doesn’t use prepared statements, I checked.

Because it’s not my PHP code or website, I informed the creator about this vulnerability in his code, and the lack of input validation. Until it’s fixed, I’ll keep a close watch on this website and database. It may not surprise you that I find more and more of these SQL injection and MySQL sleep() attacks lately.

Securing the vulnerable PHP code

In the piece of PHP code above, $id is easily made more robust and secure by adding (int):

(int)$id = ( isset( $_GET["id"] ) ? $_GET["id"] : 0 );

This will make PHP to cast the input $_GET["id"] value into an integer, this is called Type Juggling or Type Casting. The name of the desired type is written in parentheses before the variable which is to be cast. Of course the customer using this code needs to update the PHP functions to use MySQLi or PDO, e.g. migrate from mysql_connect to mysqli_connect.

And further is the use of Prepared Statements and/or Stored Procedures important.

Kill multiple MySQL threads at once

Protip: If you need to kill multiple connection threads in MySQL, you can use the following command to generated a comma separated list of connection id’s and kill them with mysqladmin:

mysqladmin processlist | grep database_name | cut -d '|' -f 2 | xargs | tr ' ' ','
mysqladmin kill [your comma separated list of Id's: 1, 2, 3, 6, 77]

This uses mysqladmin and some bash commands to built a comma separated list of connection ID’s, that you can copy and paste into mysqladmin kill.

Or you can kill all threads using mysql’s command interface and a query:

MariaDB [(none)]> select concat('KILL ',id,';') from information_schema.processlist where user='user';
+------------------------+
| concat('KILL ',id,';') |
+------------------------+
| KILL 3763; |
+------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> select concat('KILL ',id,';') from information_schema.processlist where user='user' into outfile '/tmp/a.txt';
Query OK, 1 rows affected (0.00 sec)
MariaDB [(none)]> source /tmp/a.txt;
Query OK, 0 rows affected (0.00 sec) -- kill queries running longer than 600 seconds:
-- select group_concat(concat('KILL ',id,';') separator ' ') from information_schema.processlist where Time > 600 order by Time ASC;

(thank you mysqlperformanceblog.com for this query)

MySQL sleep() injection attacks, the conclusion

This post showed you the importance of validating user supplied input. The lack of input validation (Dutch article) not only makes your website vulnerable to SQL injection attacks (Dutch article) or Cross Site Scripting (XSS – Dutch article), but may also make your web server and/or MySQL database server unresponsive due to these MySQLsleep() command injections.

This’ll disrupt the service, not only for your website, but for all users on the same web server and MySQL database server. Especially when a new MySQL vulnerability is found that crashes the MySQL service, like MySQL DoS in the Procedure Analyse Function – CVE-2015-4870.

You wouldn’t want to be the one who crashed aan entire database server just because you didn’t validate user supplied input, now would you?

select * from post where test like '%nomatch ' OR sleep(300) AND '1%'

SQL Injection with MySQL SLEEP() | Official Pythian® Blog https://blog.pythian.com/mysql-injection-sleep/

Sleep 等待连接攻击的更多相关文章

  1. 【MySQL】 清除等待连接

    由于MySQL突然新增了很多连接,超出了my.cnf所设置的最大连接数,MySQL服务无法访问,这里通过Shell脚本来删掉Sleep连接 方式1 清除连接进程 #!/bin/bash #------ ...

  2. xdebug调试一直等待连接

    调试php时一般会启动浏览器,地址栏里一般是 index.php?XDEBUG_SESSION_START=xxx xxx表示调试的ide_key. 开了代理没有关,结果调试时一直无法连上,折腾了好久 ...

  3. nginx+tomcat抵御慢速连接攻击

    一.安装nginx apt-get install nginx 安装中途可能会要求填写许可,输入‘y’就好了 如果安装提示“E: Unable to locate package nginx”,那么输 ...

  4. zmq作为守护进程?等待连接

    服务端是作为守护进程在运行的,客户端connect成功,但write时直接退出了,我在想肯能服务端socket在write时已经失效了,不然为什么会出现write时进程退出呢?现在的问题是,我要怎么才 ...

  5. TCP连接三次握手协议,释放连接四次挥手,以及使用 awl伪造mac地址进行多线程syn洪泛攻击。

    这个TCP连接就是一次追女生-谈恋爱-分手,追求比分手简单,但是分手比追求复杂.哥,谈了半年的女朋友,在就快要成功了的时候分了,原因是因为有人在后面该老子背后搞SYN洪泛攻击,最后女朋友丢失了.学会T ...

  6. TCP协议三次握手连接四次握手断开和DOS攻击

    转载:http://blog.csdn.net/fw0124/article/details/7452695 TCP连接的状态图 TCP建立连接的三次握手过程,以及关闭连接的四次握手过程 贴一个tel ...

  7. [技术博客]采用Qthread实现多线程连接等待

    采用Qthread实现多线程连接等待 ​ 本组的安卓自动化测试软件中,在测试开始前需要进行连接设备的操作,如下图左侧的按钮 ​ ​ 后端MonkeyRunner相关操作的程序中提供了connect() ...

  8. TCP的三次握手(建立连接)和四次挥手(关闭连接)

    参照: http://course.ccniit.com/CSTD/Linux/reference/files/018.PDF http://hi.baidu.com/raycomer/item/94 ...

  9. netstat监控大量ESTABLISHED连接与Time_Wait连接问题

    问题描述: 在不考虑系统负载.CPU.内存等情况下,netstat监控大量ESTABLISHED连接与Time_Wait连接. # netstat -n | awk '/^tcp/ {++y[$NF] ...

随机推荐

  1. SNF快速开发平台--规则引擎介绍和使用文档

    设计目标: a) 规则引擎语法能够满足分单,计费,WMS策略的配置要求.语法是一致和统一的 b) 能够在不修改规则引擎模块的情况下,加入任意一个新的规则:实现上述需求之外的规则配置需求 c) 运算速度 ...

  2. 使用 .toLocaleString() 轻松实现多国语言价格数字格式化

    用代码对数字进行格式化,显然不是逢三位加逗号这么简单.比如印度在数字分位符号上的处理,就堪称业界奇葩: 印度的数字读法用“拉克”(十万)和“克若尔”(千万),数字标法用不对称的数位分离,即小数点左侧首 ...

  3. iOS开发:一个无限滚动自动播放图片的Demo(Swift语言编码)

    很久以前就想写这么一个无限滚动的Demo了,最近学习了下Swift,手中没有可以用来练手的Demo,所以才将它实现了. Github地址(由于使用了UIView+AutoLayout第三方进行布局,所 ...

  4. post请求数据量过大,提交失败

    HttpRuntimeSection.MaxRequestLength 属性,请求的最大大小(以千字节为单位). 默认大小为 4096 KB (4 MB) <system.web> < ...

  5. 【iCore4 双核心板_ARM】例程十六:USB_HID实验——双向数据传输

    实验方法: 1.USB_HID协议免驱动,此例程不需要驱. 2.将跳线冒跳至USB_OTG,通过Micro USB 线将iCore4 USB-OTG接口与电脑相连. 3.打开上位机软件usb_hid. ...

  6. Thrift源码学习二——Server层

    Thrift 提供了如图五种模式:TSimpleServer.TNonblockingServer.THsHaServer.TThreadPoolServer.TThreadSelectorServe ...

  7. 删除最少字符生成Palindrome

    看到一个哥们G家电面试题,求删除最少字符生成Palindrome.大神们都在讨论dp,我完全想不到. 放一个没优化的DFS. 乐神说是O(n3)复杂度, 有机会的话要减少使用substring,并且要 ...

  8. AllPay(欧付宝)支付接口集成

    AllPay,http://www.allpay.com.tw/,欧付宝是台湾知名的第三方支付公司,拥有丰富的支付模式(支持和支付宝.财付通),只需要一次对接,各种支付模式均可使用. 接口编写SDK: ...

  9. 初试 Kubernetes 集群中使用 Traefik 反向代理

    初试 Kubernetes 集群中使用 Traefik 反向代理 2017年11月17日 09:47:20 哎_小羊_168 阅读数:12308    版权声明:本文为博主原创文章,未经博主允许不得转 ...

  10. Java如何检查日期格式是否正确?

    在Java编程中,如何检查日期格式是否正确? 以下示例演示如何使用String类的matches()方法检查日期格式是否正确. package com.yiibai; public class Che ...