跳轉到內容

Metasploit/使用Metasploit

來自華夏公益教科書


使用 Metasploit

[編輯 | 編輯原始碼]

本章涵蓋使用Metasploit 框架的各個方面。

目前,它是一組各種主題的集合。稍後可以對其進行組織以更有意義。

使用 MSF 資料庫

[編輯 | 編輯原始碼]

MSF 允許將掃描/利用結果儲存到資料庫中以進行持久儲存。該框架支援使用相當多的資料庫後端。這些包括

  • Postgres
  • MySQL
  • SQLite (版本 2 和 3)

注意:除了 SQLite(將資料庫儲存為檔案外),您需要在開始使用框架中的資料庫之前啟動 Postgres 或 MySQL 的資料庫伺服器。

注意:在使用框架與 postgres 時,需要編輯 /etc/postgresql/x.x/main/pg_hba.conf。將預設的“ident”方法替換為“trust”,如下所示

注意:在使用 postgres 之前,您必須執行$ gem install postgres。這需要與 postgresql-dev 一起提供的標頭檔案。

注意:為了獲得最佳效果,請使用 msfconsole 從框架與資料庫互動。

資料庫儲存在 MSF 中非常有用。自動利用中最有趣/熱門的主題(請根據 H.D. 的部落格寫一篇關於該主題的優秀文章)與資料庫整合一起使用。MSF 可以使用 nmap 掃描主機並將它們的狀態儲存在資料庫中,然後自動嘗試利用 MSF 中存在針對這些漏洞的漏洞。輔助模組(例如掃描器)也可以利用資料庫來儲存不同的狀態資訊。您甚至可以編寫自己的快速掃描器並讓它將資訊儲存在底層資料庫中。

資料庫模式

[編輯 | 編輯原始碼]

MSF 為其使用建立了一個數據庫。該資料庫包含以下表格

  • hosts
  • creds
  • refs
  • services
  • vulns
  • vulns_refs
  • clients
  • events
  • loots
  • report_templates
  • reports
  • tasks
  • users
  • workspaces

您可以在 RDBMS 中輕鬆檢視此資料庫的結構。

注意:資料夾 framework_base_folder/data/sql/ 包含用於為給定 RDBMS 建立資料庫表的 SQL 檔案。

載入資料庫模組

[編輯 | 編輯原始碼]

在開始使用 MSF 的資料庫之前,應載入相應的資料庫模組。對於 msfconsole,這可以透過使用load db_* 命令來載入。以下是使用 MSF 的 MySQL 示例會話。

root # /etc/rc.d/rc.mysqld start     # start mysql database server
Starting mysqld daemon with databases from /var/lib/mysql
root # msfconsole                    # start MSF console interface

                     888                           888        d8b888
                     888                           888        Y8P888
                     888                           888           888
88888b.d88b.  .d88b. 888888 8888b. .d8888b 88888b. 888 .d88b. 888888888
888 "888 "88bd8P  Y8b888       "88b88K     888 "88b888d88""88b888888
888  888  88888888888888   .d888888"Y8888b.888  888888888  888888888
888  888  888Y8b.    Y88b. 888  888     X88888 d88P888Y88..88P888Y88b.
888  888  888 "Y8888  "Y888"Y888888 88888P'88888P" 888 "Y88P" 888 "Y888
                                           888
                                           888
                                           888


       =[ msf v3.1-dev
+ -- --=[ 191 exploits - 104 payloads
+ -- --=[ 17 encoders - 5 nops
       =[ 35 aux

msf > load db_mysql                 # Load mysql database plugin
[*] Successfully loaded plugin: db_mysql
msf > help                          # New set of commands are available now
MySQL Database Commands
=======================

    Command        Description
    -------        -----------
    db_connect     Connect to an existing database ( user:pass@host:port/db )
    db_create      Create a brand new database ( user:pass@host:port/db )
    db_destroy     Drop an existing database ( user:pass@host:port/db )
    db_disconnect  Disconnect from the current database instance

建立資料庫

[編輯 | 編輯原始碼]

您第一次要使用 MSF 的資料庫時,需要建立它。建立資料庫後,您只需在將來的會話中連線到它即可使用它。db_create 命令允許您建立一個新資料庫。讓我們建立一個名為metasploit 的資料庫

# Connect and user root with password mydbpass and create a database named metasploit
msf > db_create root:mydbpass@localhost/metasploit
[*] Database creation complete (check for errors)

建立資料庫後,它將自動可用於該會話。對於後續會話,您可以連線到 db。

使用現有資料庫

[編輯 | 編輯原始碼]

如果您之前已建立過資料庫,則在將來的會話中,您只需使用db_connect 命令即可使用它。

root # msfconsole                    # start MSF console interface

                     888                           888        d8b888
                     888                           888        Y8P888
                     888                           888           888
88888b.d88b.  .d88b. 888888 8888b. .d8888b 88888b. 888 .d88b. 888888888
888 "888 "88bd8P  Y8b888       "88b88K     888 "88b888d88""88b888888
888  888  88888888888888   .d888888"Y8888b.888  888888888  888888888
888  888  888Y8b.    Y88b. 888  888     X88888 d88P888Y88..88P888Y88b.
888  888  888 "Y8888  "Y888"Y888888 88888P'88888P" 888 "Y88P" 888 "Y888
                                           888
                                           888
                                           888


       =[ msf v3.1-dev
+ -- --=[ 191 exploits - 104 payloads
+ -- --=[ 17 encoders - 5 nops
       =[ 35 aux

msf > load db_mysql                                  # Load mysql database plugin
[*] Successfully loaded plugin: db_mysql
msf > db_connect root:mydbpass@localhost/metasploit  # Connect to the metasploit db
msf > help                 # Upon connecting to a database, we get another new set of commands
Database Backend Commands
=========================

    Command               Description
    -------               -----------
    db_add_host           Add one or more hosts to the database
    db_add_port           Add a port to host
    db_autopwn            Automatically exploit everything
    db_hosts              List all hosts in the database
    db_import_nessus_nbe  Import a Nessus scan result file (NBE)
    db_import_nmap_xml    Import a Nmap scan results file (-oX)
    db_nmap               Executes nmap and records the output automatically
    db_services           List all services in the database
    db_vulns              List all vulnerabilities in the database
msf > db_hosts
[*] Host: localhost

斷開資料庫連線

[編輯 | 編輯原始碼]

如果您在會話中覺得不再需要使用資料庫,則只需在“MSF>”提示符下發出db_disconnect 命令即可斷開資料庫連線。


刪除資料庫

[編輯 | 編輯原始碼]

當您只想刪除具有所有資料的資料庫(也許您已經備份了資料,現在正在釋放磁碟空間)時,您可以使用db_destroy 命令來完成此操作。

msf > db_destroy root:mydbpass@localhost/metasploit  # Drops the metasploit database
Database "metasploit" dropped

資料庫後端命令

[編輯 | 編輯原始碼]

當 MSF 連線到資料庫時,另一組稱為資料庫後端命令 的命令可用。這些命令允許您對主機執行埠掃描,檢查活動主機、它們正在執行的服務以及這些服務所具有的漏洞。

   Command               Description                                         
   -------               -----------                                         
   db_add_host           Add one or more hosts to the database               
   db_add_note           Add a note to host                                  
   db_add_port           Add a port to host                                  
   db_autopwn            Automatically exploit everything                    
   db_hosts              List all hosts in the database                      
   db_import_nessus_nbe  Import a Nessus scan result file (NBE)              
   db_import_nmap_xml    Import a Nmap scan results file (-oX)               
   db_nmap               Executes nmap and records the output automatically  
   db_notes              List all notes in the database                      
   db_services           List all services in the database                   
   db_vulns              List all vulnerabilities in the database

最重要的一個,也是最常使用的,是db_nmap,它將執行具有指定命令的 nmap 並在資料庫中記錄發現結果。

msf> db_nmap -sS -P0 192.168.1.1
...

要列出掃描中找到的主機...

msf> db_hosts
[*] Time: Wed Mar 05 15:18:48 -0500 2008 Host: 192.168.1.1

要列出主機掃描中發現的可能的漏洞...

msf> db_vulns
...

db_autopwn

[編輯 | 編輯原始碼]

您可以使用另一個數據庫後端命令db_autopwn 對資料庫中的主機執行漏洞利用。 H.D. Moore 在該功能新增到框架時寫了關於它的內容——更多資訊。

msf > db_autopwn
[*] Usage: db_autopwn [options]
       -h         Display this help text
       -t         Show all matching exploit modules
       -x         Select modules based on vulnerability references
       -p         Select modules based on open ports
       -e         Launch exploits against all matched targets
       -s         Only obtain a single shell per target system (NON-FUNCTIONAL)
       -r         Use a reverse connect shell
       -b         Use a bind shell on a random port
       -I [range] Only exploit hosts inside this range
       -X [range] Always exclude hosts inside this range
華夏公益教科書