---
# System prepended metadata

title: 'COSC60: Computer Networks Lab #1'

---

# COSC60: Computer Networks Lab#1
## Author: Johan Cruz Hernandez
## Exercise #1
### Question 1a
The domain packetbender.com is registered through GoDaddy.com, LLC. It's using name servers from DomainControl.com, which is typically associated with GoDaddy. To get this information i ran the "traceroute packetbender.com" command.

The IP address 71.19.146.5 is managed by prgmr.com, Inc. The Autonomous System Numbers (ASNs) providing connectivity include AS47066, which is directly associated with prgmr.com, Inc., AS2914, which is owned by NTT America, Inc., and AS6939, which is Hurricane Electric, LLC. To get this information I ran the command whois 71.19.146.5 where 71.19.146.5 is the ip address of packetbender.com.

#### Terminal Output:
![Screen Shot 2024-04-22 at 7.52.00 PM](https://hackmd.io/_uploads/rkON_dEWA.png)

![Screen Shot 2024-04-22 at 8.00.00 PM](https://hackmd.io/_uploads/SygfcuE-R.png)

![Screen Shot 2024-04-22 at 8.02.12 PM](https://hackmd.io/_uploads/r1H5q_E-A.png)

![Screen Shot 2024-04-22 at 8.03.26 PM](https://hackmd.io/_uploads/SkSyiOEWA.png)

![Screen Shot 2024-04-22 at 8.04.16 PM](https://hackmd.io/_uploads/ByIzouVZC.png)

### Question 1b
17 network hops separate my machine from packetbender. Used the tool traceroute to figure out how many network hops it takes to get from my computer to packetbender.com. I am assuming that the *** are prvate networks that are inaccessible to me.

#### Terminal Output
![Screen Shot 2024-04-22 at 7.43.55 PM](https://hackmd.io/_uploads/ByZ8U_E-R.png)

![Screen Shot 2024-04-22 at 7.47.41 PM](https://hackmd.io/_uploads/H1k4vdVWC.png)

### Question 1c
The token I recieved was TOKEN=9BtmELOC. The procedure I followed is outlines below:
- Open terminal 1 and run the command sudo tcpdump -i any icmp -A -vv. Terminal 1 is set to listen to requests.
- Open terminal 2 and run the following:
    - echo f004hqk | md5 | cut -c1-8   
    - echo ID=b8ee4252 | xxd -p
    - for i in {30..60}; do ping -c 1 -m $i -v -p 49443d62386565343235320a packetbender.com; done
#### Terminal output
![Screen Shot 2024-04-22 at 3.43.49 PM](https://hackmd.io/_uploads/H1GZAEVW0.png)

## Exercise #2
- Echo Reply (Type 0 Code 0)
    - ping packetbender.com
    - ![Screen Shot 2024-04-22 at 8.38.39 PM](https://hackmd.io/_uploads/ByRM7K4bA.png)
- Time Exceeded Message (Type 11 Code 0):
    - command: traceroute -m 3 packetbender.com
        response: Time-to-live exceeded (Time to live exceeded in transit)
    - ![Screen Shot 2024-04-22 at 8.17.40 PM](https://hackmd.io/_uploads/HkG4RdNZ0.png)

- Echo Ping Request (Type 8 Code 0)
    - command: ping 10.254.254.254
        response: localhost > localhost: ICMP localhost udp port 32376 unreachable, length 36
    - ![Screen Shot 2024-04-22 at 8.21.46 PM](https://hackmd.io/_uploads/SyOXkY4ZC.png)

- Destination Unreachable (Type 3 Code 3)
    - command: ping -c 1 -v -p 49443d62386565343235320a packetbender.com
    - ![Screen Shot 2024-04-22 at 8.20.49 PM](https://hackmd.io/_uploads/S1xxyK4ZR.png)

- Summary of Message Types:
    (0) Echo Reply
    (3) Destination Unreachable
    (4) Source Quench
    (5) Redirect
    (8) Echo
    (11) Time Exceeded
    (12) Parameter Problem
    (13) Timestamp
    (14) Timestamp Reply
    (15) Information Request
    (16) Information Reply

## Exercise #3
- Receive token from TCP protocol: **echo -ne "\x09\x00\xfff004hqk" | nc -p 2103 thepond.cs.dartmouth.edu 603**
    - TCP token:  3a27d767
- Receive token from UDP protocol: **echo -ne "\x09\x00\xfff004hqk" | nc -u -p 2696 thepond.cs.dartmouth.edu 603**
    - UDP token: 0c0eea5f
#### Terminal output
![Screen Shot 2024-04-22 at 3.53.42 PM](https://hackmd.io/_uploads/HkNUgHNZA.png)
#### Procedure
#### (1) ssh into the pond
    - ssh -p 106 johan@thepond.cs.dartmouth.edu
#### (2) Find the priviledged port set up to listen to UDP and TCP protocol.
    - netstat -tuln | grep ":603 "
![Screen Shot 2024-04-22 at 4.04.57 PM](https://hackmd.io/_uploads/BJIl7HEZA.png)
#### (3) Interact with bot. Send message via UDP and TCP protocols. Figure out how the bot wants the message:
    - UDP: echo "Hello" | nc -u thepond.cs.dartmouth.edu 603
    - TCP: echo "Hello" | nc thepond.cs.dartmouth.edu 603
#### Terminal Output
![Screen Shot 2024-04-22 at 6.28.10 PM](https://hackmd.io/_uploads/rktYEPNZ0.png)

![Screen Shot 2024-04-22 at 6.29.04 PM](https://hackmd.io/_uploads/r10h4wEWR.png)
#### (4) Bot expected a message of 10 bytes. Send a message of 10 bytes to see bot's reponse.
    - UDP: echo -n "Hello     " | nc -u thepond.cs.dartmouth.edu 603
    - TCP: echo -n "Hello     " | nc thepond.cs.dartmouth.edu 603

![Screen Shot 2024-04-22 at 6.31.37 PM](https://hackmd.io/_uploads/SySUrPEb0.png)

![Screen Shot 2024-04-22 at 6.31.52 PM](https://hackmd.io/_uploads/H1IDBwE-0.png)
#### (5) Figure out what the first magic byte is by echoing every possible byte.
    - run the bash script: ./magicbyte.sh
    - UDP: echo -ne "\x09234567890" | nc -u thepond.cs.dartmouth.edu 603
    - TCP: echo -ne "\x09234567890" | nc thepond.cs.dartmouth.edu 603

#### Terminal Output
Output of magicbyte.sh
![Screen Shot 2024-04-22 at 6.36.12 PM](https://hackmd.io/_uploads/Bk_PUP4WR.png)

Output of writing via UDP protocol
![Screen Shot 2024-04-22 at 6.50.48 PM](https://hackmd.io/_uploads/r1EAYDV-A.png)

Output of writing via TCP protocol
![Screen Shot 2024-04-22 at 6.51.06 PM](https://hackmd.io/_uploads/SkwJcDEbR.png)


#### Bash Script
To compile and run
```
chmod +x magicbyte.sh
./magicbyte.sh
```
```
#!/bin/bash

HOST="thepond.cs.dartmouth.edu"
PORT=603
MESSAGE="123456789"
ERROR_MESSAGE="Error: Message does not start with magic byte"

MAGIC_BYTE=""  # Initialize MAGIC_BYTE variable

for i in {0..255}; do
    START_BYTE=$(printf "\\x%02x" $i)
    RESULT=$(echo -n -e "\x$START_BYTE$MESSAGE" | nc -u -w 1 $HOST $PORT)
    if [ "$RESULT" == "$ERROR_MESSAGE" ]; then
        continue
    else
        echo "Found magic byte: $START_BYTE"
        MAGIC_BYTE=$START_BYTE
        break
    fi
done

if [ -z "$MAGIC_BYTE" ]; then
    echo "Magic byte not found."
else
    echo "Magic byte found: $MAGIC_BYTE"
    echo "Sending message with magic byte:"
    echo -n -e "\x$MAGIC_BYTE$MESSAGE" | nc -u -w 1 $HOST $PORT
fi
```
#### (6) Figure out what the second magic byte is by echoing every possible byte.
    - run bash script: ./mb2.sh
    - UDP: echo -ne "\x09\x0023456789" | nc -u thepond.cs.dartmouth.edu 603
    - TCP: echo -ne "\x09\x0023456789" | nc thepond.cs.dartmouth.edu 603

#### Terminal Output
Output of mb2.sh
![Screen Shot 2024-04-22 at 6.54.32 PM](https://hackmd.io/_uploads/S1IncDVWR.png)

Output of echo via UDP Protocol
![Screen Shot 2024-04-22 at 7.00.22 PM](https://hackmd.io/_uploads/ryPz3PEWA.png)

Output of echo via TCP Protocol
![Screen Shot 2024-04-22 at 7.01.00 PM](https://hackmd.io/_uploads/B1o4nwEZC.png)

#### Bash Script
To compile and run
```
chmod +x mb2.sh
./mb2.sh
```
```
#!/bin/bash

HOST="thepond.cs.dartmouth.edu"
PORT=603
MESSAGE="12345678" # 8 additional characters to make total length 10 with two bytes
FIRST_MAGIC_BYTE="\x09"
ERROR_MESSAGE="Error: Message does not start with magic byte"

# Iterate over all possible second bytes
for i in {0..255}; do
    SECOND_BYTE=$(printf "\\x%02x" $i)
    FULL_MESSAGE=$FIRST_MAGIC_BYTE$SECOND_BYTE$MESSAGE
    
    # Send the full message to the server and capture the result
    RESULT=$(echo -ne "$FULL_MESSAGE" | nc -u -w 1 $HOST $PORT)
    
    # Check if the response is not the known error message
    if [[ "$RESULT" != "$ERROR_MESSAGE" ]]; then
        echo "Found second magic byte: $(printf "%02x" $i)"
        echo "Magic bytes found: 09 $(printf "%02x" $i)"
        echo "Sending message with magic bytes:"
        echo -ne "$FULL_MESSAGE" | nc -u -w 1 $HOST $PORT
        exit 0
    fi
done

echo "Second magic byte not found."
```
#### (7) Figure out what the third magic byte is by echoing every possible byte.
    - run bash script: 
    - UDP: echo -ne "\x09\x00\xff1234567" | nc -u thepond.cs.dartmouth.edu 603
    - TCP: echo -ne "\x09\x00\xff1234567" | nc thepond.cs.dartmouth.edu 603

#### Terminal Output
Output of echo via UDP Protocol
![Screen Shot 2024-04-22 at 7.04.29 PM](https://hackmd.io/_uploads/BJoZpvV-C.png)

Output of echo via TCP Protocol
![Screen Shot 2024-04-22 at 7.04.52 PM](https://hackmd.io/_uploads/Hkz7TvN-R.png)

#### (8) Insert NetID into the payload with the three magic bytes.
    - UDP: echo -ne "\x09\x00\xfff004hqk" | nc -u thepond.cs.dartmouth.edu 603
    - TCP: echo -ne "\x09\x00\xfff004hqk" | nc thepond.cs.dartmouth.edu 603
#### Terminal Output
Output of echo via UDP Protocol
![Screen Shot 2024-04-22 at 7.07.18 PM](https://hackmd.io/_uploads/HkmhpPNWR.png)

Output of echo via TCP Protocol
![Screen Shot 2024-04-22 at 7.07.00 PM](https://hackmd.io/_uploads/SJZjpPEWC.png)

#### (9) Find correct soure port by sending to all possible source ports.
    - UDP: Source port 2696
        - echo -ne "\x09\x00\xfff004hqk" | nc -u -p 2696 thepond.cs.dartmouth.edu 603
        - TOKEN= 0c0eea5f
    - TCP: Source port 2103
        - echo -ne "\x09\x00\xfff004hqk" | nc -p 2103 thepond.cs.dartmouth.edu 603
        - TOKEN= 3a27d767
#### Terminal Output
Output of UDP Echo
![Screen Shot 2024-04-22 at 7.10.36 PM](https://hackmd.io/_uploads/SyF_RPN-0.png)

Output of TCP Echo
![Screen Shot 2024-04-22 at 7.12.53 PM](https://hackmd.io/_uploads/rJ-ZyuVWR.png)

Output of Bash Script for UDP sp.sh
![Screen Shot 2024-04-22 at 7.30.28 PM](https://hackmd.io/_uploads/H1X7Xu4W0.png)

Output of Bash Script for TCP sp_tcp.sh
![Screen Shot 2024-04-22 at 7.32.51 PM](https://hackmd.io/_uploads/ByV2m_VbA.png)


#### Bash Scripts
UDP Protocol: sp.sh
To compile and run
```
chmod +x sp.sh
./sp.sh
```
```
#!/bin/bash

HOST="thepond.cs.dartmouth.edu"
PORT=603
MESSAGE="\x09\x00\xff\xf004hqk" # Adjusted message with the correct magic bytes
RANGE_START=2000
RANGE_END=2999

for (( SRC_PORT=RANGE_START; SRC_PORT<=RANGE_END; SRC_PORT++ )); do
    # Sending the message from the specified source port
    RESULT=$(echo -ne "$MESSAGE" | nc -u -p $SRC_PORT -w 1 $HOST $PORT)
    
    # Check if the response is what we expect or if we need to keep trying
    if [[ "$RESULT" != "Error: Incorrect source port." ]]; then
        echo "Success from port $SRC_PORT: $RESULT"
        break
    else
        echo "Tried port $SRC_PORT, but got: $RESULT"
    fi
done

```

TCP Protocol: sp_tcp.sh
To compile and run
```
chmod +x sp_tcp.sh
./sp_tcp.sh
```
```
#!/bin/bash

HOST="thepond.cs.dartmouth.edu"
PORT=603
MESSAGE="\x09\x00\xff\xf004hqk" # Adjusted message with the correct magic bytes
RANGE_START=2000
RANGE_END=2999

for (( SRC_PORT=RANGE_START; SRC_PORT<=RANGE_END; SRC_PORT++ )); do
    # Attempt to send the message from the specified source port using TCP
    RESULT=$(echo -ne "$MESSAGE" | nc -p $SRC_PORT -w 1 $HOST $PORT 2>&1) # Redirect stderr to stdout to capture errors

    # Check if the result indicates a successful transmission or an error
    if [[ "$RESULT" == *"Success"* ]]; then
        echo "Success from port $SRC_PORT: $RESULT"
        break
    elif [[ "$RESULT" == *"bind failed: Address already in use"* ]]; then
        echo "Port $SRC_PORT in use, trying next."
        continue
    else
        echo "Tried port $SRC_PORT, but got: $RESULT"
    fi
done

```