import socket

# Change the following host and see what IP it prints!
host = "google.com"
ip = socket.gethostbyname(host)

print(ip)
216.239.38.120
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((ip, 80))
    print("Successfully connected!")
Successfully connected!

Check-In

  1. What is an IP address?
  2. What is a TCP port?
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((ip, 80))

    # Send a GET request to "/"
    s.sendall(b"GET / HTTP/1.1\r\n\r\n")

    # Recieve & print 2048 bytes of data
    data = s.recv(2048)
    print(data.decode())
HTTP/1.1 200 OK
Date: Wed, 26 Apr 2023 20:42:23 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Content-Security-Policy-Report-Only: object-src 'none';base-uri 'self';script-src 'nonce-P8iyfLS7sVOMtw4VH_ETZg' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Set-Cookie: 1P_JAR=2023-04-26-20; expires=Fri, 26-May-2023 20:42:23 GMT; path=/; domain=.google.com; Secure
Set-Cookie: AEC=AUEFqZfIZR7D7_Ir2nIxXQkdrgQ18Zio2oRGrMvTMIr4PGb2PJ9hu1PLcLU; expires=Mon, 23-Oct-2023 20:42:23 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
Set-Cookie: NID=511=RgU6-HAk4FzeCRp2vMqoKEru0hdLT770SuNXFL1iziw3mwOoF5arW3cFVuo7GHAWGuo7PkElFUcTu8IFBA2aq29bSyM-XSX9fTotRtfAvGZSItButC2QxxcCTnJiTeAzoFrXUfatqlXqPZpFSMAv1vCuoFIRuIv5-ddMM8c9vfE; expires=Thu, 26-Oct-2023 20:42:23 GMT; path=/; domain=.google.com; HttpOnly
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked

5c4e
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for." name="description"><meta content="noodp" name="robots"><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script nonce="P8iyfLS7sVOMtw4VH_ETZg">(function(){window.google={kEI:'r4xJZJfnMJLTkPIPtPO2qAs',kEXPI:'0,1359409,6058,207,4804,2316,383,246,5,1129120,1197694,380796,16115,19398,9286,22430,1362,283,12037,2814,14765,4998,13228,3847,38444,889,1983,2891,3926,214,4208,3406,606,29877,30813,15324,432,3,1590,1,16916,2652,4,1528,2304,29062,13063,16640,1457,16786,5824,2533,4094,7596,1,42154,2,140
import requests

# Change the URL to whatever you'd like
response = requests.get("https://google.com")

print("Status code:", response.status_code)
print("Headers:", response.headers)
print("Response text:", response.text[:100])

# Add a line to print the "Content-Type" header of the response
# Try an image URL!
Status code: 200
Headers: {'Date': 'Wed, 26 Apr 2023 20:45:36 GMT', 'Expires': '-1', 'Cache-Control': 'private, max-age=0', 'Content-Type': 'text/html; charset=ISO-8859-1', 'Content-Security-Policy-Report-Only': "object-src 'none';base-uri 'self';script-src 'nonce-xMcRaBRzcjBeBaX8gdqZvg' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp", 'P3P': 'CP="This is not a P3P policy! See g.co/p3phelp for more info."', 'Content-Encoding': 'gzip', 'Server': 'gws', 'X-XSS-Protection': '0', 'X-Frame-Options': 'SAMEORIGIN', 'Set-Cookie': '1P_JAR=2023-04-26-20; expires=Fri, 26-May-2023 20:45:36 GMT; path=/; domain=.google.com; Secure, AEC=AUEFqZc02uup9CYz4qolBUcX_nuILDbjyk0QBctQXqkspLU9iU81RR2BlpI; expires=Mon, 23-Oct-2023 20:45:36 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax, NID=511=tLX8LdFaCQXP7XJy_RGAN-Cn4TJMoXYGHCku8HAwQpNvbtDP99QekUFltpB7hZ72o8jmZcgnQl5Ji3VtJEWZKZRE9tbaNLBFzoOjo_CIjBb2eZvOKG_2cdmE9EyruAGyhO6sM1nEWTwbvkHExkk9xv2hX606V_RQNBJ_laSP8rM; expires=Thu, 26-Oct-2023 20:45:36 GMT; path=/; domain=.google.com; HttpOnly', 'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000', 'Transfer-Encoding': 'chunked'}
Response text: <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content

NGINX

aws = "3.130.255.192"

response = requests.get("http://" + aws)
print(response.text)
<html><head><title>ContentKeeper</title></heQad>
<body bgcolor="#A02E5F" text="White" link="Lime" vlink="Aqua">
<center><br><h1>ContentKeeper<br>Non-Managed Site</h1>
No access is available to NON-Managed Sites.<br><br>
<table border="1" cellspacing="0">
<tr><td>URL</td><td><b>3.130.255.192</b></td></tr>
<tr><td>Username</td><td><b>1905501/pusd</b></td></tr></table>
</body></html>

Configuration

server {
    // Listen on virtual "port 80"
    listen 80;
    listen [::]:80;
    server_name 3.130.255.192;

    location / {
        // Inform server about original client
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;

        // Forward all requests transparently to the server running on our computer
        proxy_pass              http://localhost:9099;
    }
}

Load Balancing

upstream example.com {
    server server1.example.com;
    server server1.example.com;
}

HTTP Headers

server {
    add_header X-Cool-Header "I love APCSP!";

    location /pages {
        add_header X-Cooler-Header "This is my secret header!";
    }
}

Check In

  1. Research 1 HTTP header and describe, in detail, its purpose.
  2. Write a line in a sample NGINX configuration that will add that specific header to the /information location
  3. Explain the purpose of the load balancing performed by NGINX
  4. Modify the following code block to obtain the value of the secret header on /products of the AWS site
aws = "3.130.255.192"

response = requests.get("http://" + aws+ "/products")

print("The secret header is:", "...")

Hacks

  • Complete the above check-in questions and change the hosts (0.1)
  • Complete the above code-segment to retrieve the secret header (0.1)

Bonus (0.05)

Create a diagram showing the layers of abstraction that allow us to use HTTP (IP, TCP, etc.)

CORS Hacks

  1. Explain what CORS is and what it stands for
  2. Describe how you would be able to implement CORS into your own websites
  3. Describe why you would want to implement CORS into your own websites
  4. How could use CORS to benefit yourself in the future?

Total: 0.2 points

KASM Hacks

  1. What is the purpose of "sudo" when running commands in terminal?
  2. What are some commands which allow us to look at how the storage of a machine is set up as?
  3. What do you think are some alternatives to running "curl -O" to get the zip file for KASM?
  4. What kind of commands do you think the "install.sh" command has and why is it necessary to call it?
  5. Explain in at least 3-4 sentences how deploying KASM is related to/requires other topics talked about in the lesson and/or potential ways to add things mentioned in the lesson to this guide.

Total: 0.2 points

AWS/RDS Hacks

See the setup post

  • Create your own database in the EC2 I have created (ec2-database-connect)
    • name it with your first and last name (example: aditya-nawandhar) (0.1)
    • Create a table using the commands on the link provided. (0.1)
    • using commands from the link provided make columns and rows with test data (can be anything) (example: “name” and “class” are the columns with rows being something like “Aditya” and “Junior”). At least 4 test rows (0.1)
    • additional points if the data matches CPT (Bonus: 0.05)

Total: 0.3