Assignment 2.1 Port Scanning 1
Part 1: Creating a TCP connection on Port 80
Do the following:
Open up wireshark with a display filter on tcp port 80

Execute the echo command
bash -c "echo >/dev/tcp/19.0.5.21/80"
This command attempts to open a TCP connection to the specified IP address on port 80.
bash -c
executes the following command string using bash"echo >/dev/tcp/19.0.5.21/90
uses Bashes built-in/dev/tcp
feature to attempt to connect to the IP address on port 80.
Capture the TCP handshake, push and then tear down

Deliverable 1: Provide a screenshot of the wireshark capture above.
Part 2: With Bash - Single Host Single Port
Create a list of targets

printf
Create a short list of popular TCP ports

Recreate the script and add enhancements such as error checking, parameter and option checking, and output enhancement.

Script Breakdown
hostfile=$1
This assigns the first argument
$1
to the variablehostfile
must be defined when executing the script (./portscanner.sh hostfile portfile)
profile=$2
This overwrites the value of
portfile
with$2
must be defined when executing the script (./portscanner.sh hostfile portfile)
echo "host,port"
Prints
host,port
to the terminal
for host in $(cat $hostfile); do
Outer loop
Reads each line from the
hostfile
and assigns the value to the variablehost
$cat $hostfile)
reads the contents of the file specified byhostfile
for port in $(cat $portfile); do
Inner Loop
Reads each line from the
portfile
and assigns the value to the variableport
This loop iterates for every
host
in the outer loop
timeout .1 bash -c "echo >/dev/tcp/$host/$port" 2>/dev/null && echo "$host,$port"
Testing TCP connection
timeout .1
limits the execution time of the following command to 0.1 secondsbash -c "echo >/dev/tcp/$host/$port"
Tries to open a TCP connection to the currenthost
andport
Uses Bash's
/dev/tcp
feature for this purposeIf the connection is successful, the command will succeed
2>/dev/bull
Redirects the error messages ti
/dev/null
and silently discards them
&& echo "$host,$port"
If the connection is successful, it prints the
host
andport
Enchanced Script
I had to execute sudo chmod +x portscanner.sh
for sudo to recognize the file as an executable.
#!/bin/bash
#variables users must define when running the script
hostfile=$1
portfile=$2
outputfile=$3
#checks to see if users gave variable $outputfile a value
#the -z option checks if the variable is empty or undefined
if [[ -z $outputfile ]]; then
outputfile="results.csv"
fi
#makes a header in the output file
echo "host,port,status" > "$outputfile"
#the display message on the screen once the scan is complete.
#Uses the variable $outputfile in the message
echo "host,port,status can be found in "$outputfile""
#reads each line the hostfile/portfile and assigns a value to host and port
for host in $(cat $hostfile); do
for port in $(cat $portfile); do
#commands to make a connection via the specified hosts/ports
if timeout .1 bash -c "echo >/dev/tcp/$host/$port" 2>/dev/null; then
#Status was added to show weather a TCP connection was a success/failure
echo "$host,$port,success" >> "$outputfile"
else
echo "$host,$port,failure" >> "$outputfile"
fi
done
done
#command above attempts to establish a connect with the hosts
#and ports specified
Deliverable 2: Execute your script (demo your enhancements as well), provide a source code listing (also upload this to your technical journal)

Deliverable 3: So, you notice we target the file /dev/tcp/thehostip/thetcpport. Can you find this file in kali? Break out our friend Google and see if you can find out what is going on. Briefly explain what you discover.
/dev/tcp/"the host ip/ "the tcp port"
/dev/tcp
is device file within the /dev
directory that allows you to directly connect to a remote TCP endpoint without the use of netcat.
The command allows you to establish a connection with a remote server over a network. It does this by opening a network socket and then allowing you to read/write to it, similar to how you would be with a normal file.
One of the most common uses for this command is to check if a port is open in a remote host. It can be useful on machines that nc, curl, or wget
don't work to check for network connections.
Network socket
A virtual connection endpoint within a computer network that allows applications on different devices to send and receive data with each other, acting as a two-way channel for network communication. A socket is identified by a port number and IP address.

Sources:
(great resource for more options that work with this command) https://medium.com/@stefanos.kalandaridis/bash-ing-your-network-f7069ab7c5f4
NMAP
Begin a wireshark session and run a default scan against 10.0.5.31
Deliverable 4. Provide a screenshot showing your nmap output

ou will notice right away that the default nmap scan begins by an ICMP echo request, a SYN to 80 and 443 and an ICMP timestamp request. It will then SYN scans 1000 popular ports.

Find a port reported to be open and observe the TCP Flags being invoked by client and server.
Deliverable 5. find another open port, create the appropriate display filter and submit a screenshot similar to the example (but with another port).
Troubleshooting
I found that using the tcp.flags.xxx
filter in wireshark wasn't filtering the packets in the way I wanted because it was still showing packets with other flags.
I found that using the hexadecimal value of the flag worked better.
tcp.flags == 0x10 && ip.addr == 10.0.5.31

NMAP Single Port
Restart a new wireshark capture and clear display filters. Run the following command
sudo nmap 10.0.5.31 -p 3389
(-p defines a port)Look for traffic to and from port 3389
tcp.port == 3389
Run the same command without sudo

Deliverable 6. Describe the difference in the two wireshark captures
When nmap was run without sudo, the host was able to complete the TCP handshake with 10.0.5.31, as we can see by the ACK flag in packet 14. In the wireshark capture with sudo, 10.0.5.31 responds to us but doesn't make the full connection.
Limiting NMAP's host discovery with -Pn
Deliverable 7. Add the -Pn flag and provide a wireshark display. You should have a total of 3 packets and evidence of a simple SYN scan similar to the one below.

Last updated