import socket

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

print(ip)
104.18.17.236
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?
    • An IP (Internet Protocol) address is a numerical label assigned to every device connected to a computer network that uses the Internet Protocol for communication. It serves as a unique identifier for the device and allows it to communicate with other devices on the network.
  2. What is a TCP port?
    • In computer networking, a TCP (Transmission Control Protocol) port is a virtual communication endpoint that enables two devices to establish a connection and exchange data over the internet or a network.
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 400 Bad Request
Server: cloudflare
Date: Mon, 01 May 2023 23:24:27 GMT
Content-Type: text/html
Content-Length: 155
Connection: close
CF-RAY: -

<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>cloudflare</center>
</body>
</html>

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])
print("Content-Type:", response.headers)

# Add a line to print the "Content-Type" header of the response
# Try an image URL!
Status code: 200
Headers: {'Date': 'Mon, 01 May 2023 23:25:41 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-sdjLIksrqvc0n2tPynQo2w' '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-05-01-23; expires=Wed, 31-May-2023 23:25:41 GMT; path=/; domain=.google.com; Secure, AEC=AUEFqZdRJPsYavtkqZTPWUzDzUGf5z6krYS7TiH-MzuAoUiJ_dRuAuT1Rw; expires=Sat, 28-Oct-2023 23:25:41 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax, NID=511=UgbiJSNYjreqCbo-kQ7usTaZjwq2jxFyqE6uJX0A8ZZGZl4cMDzl3M0PSpqtZSANS2EX2DIQeyAclM6x8NBYoyY_J7oZIXtIAgliQMx2p7kaJg_u00oNnK_yH-OSjPgnyblgyWyEPNblJjYu_XJLOHkxuWxPFgR8B09tzrs55bU; expires=Tue, 31-Oct-2023 23:25:41 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
Content-Type: {'Date': 'Mon, 01 May 2023 23:25:41 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-sdjLIksrqvc0n2tPynQo2w' '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-05-01-23; expires=Wed, 31-May-2023 23:25:41 GMT; path=/; domain=.google.com; Secure, AEC=AUEFqZdRJPsYavtkqZTPWUzDzUGf5z6krYS7TiH-MzuAoUiJ_dRuAuT1Rw; expires=Sat, 28-Oct-2023 23:25:41 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax, NID=511=UgbiJSNYjreqCbo-kQ7usTaZjwq2jxFyqE6uJX0A8ZZGZl4cMDzl3M0PSpqtZSANS2EX2DIQeyAclM6x8NBYoyY_J7oZIXtIAgliQMx2p7kaJg_u00oNnK_yH-OSjPgnyblgyWyEPNblJjYu_XJLOHkxuWxPFgR8B09tzrs55bU; expires=Tue, 31-Oct-2023 23:25:41 GMT; path=/; domain=.google.com; HttpOnly', 'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000', 'Transfer-Encoding': 'chunked'}
aws = "3.130.255.192"

response = requests.get("http://" + aws)
print(response.text)
<!doctype html>
<html>
<head>
<title>Cool site</title>
<meta name="description" content="cool site for apcsp">
</head>
<body>
Hello, this is my cool site. Check out my products:
<a href="/products">Products!!</a>
</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. One HTTP header that plays a critical role in web communication is the "User-Agent" header. The User-Agent header is an HTTP request header field that identifies the client software, including the web browser or application, that sends the HTTP request to the server.

The User-Agent header helps the web server to understand the type of device or software the client is using to request the resource, including its operating system, version, and capabilities. This information enables the server to optimize the content delivery for that specific device, such as serving a mobile-friendly version of a website to a mobile browser.

  1. Write a line in a sample NGINX configuration that will add that specific header to the /information location from flask import Flask from flask_nginx import Nginx

app = Flask(name) nginx = Nginx(app)

@app.route('/information') def get_information(): headers = {'User-Agent': 'My Custom User Agent'} return 'Information page', 200, headers

if name == 'main': app.run()

  1. Explain the purpose of the load balancing performed by NGINX The main purpose of load balancing performed by NGINX is to distribute incoming requests from clients across multiple backend servers to ensure that no single server becomes overloaded or a single point of failure. NGINX can balance the traffic among multiple servers based on various algorithms, including round-robin, least connections, IP hash, and more, to optimize resource utilization and reduce response times.
  2. Modify the following code block to obtain the value of the secret header on /products of the AWS site import requests

aws = "3.130.255.192"

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

secret_header = response.headers.get("X-Secret-Header")

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

aws = "3.130.255.192"

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

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

aws = "3.130.255.192"

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

secret_header = response.headers.get("X-Secret-Header")

print("The secret header is:", secret_header)
The secret header is: None

CORS Hacks

  1. Explain what CORS is and what it stands for CORS (Cross-Origin Resource Sharing) is a security feature implemented in web browsers that restricts web pages from making requests to a different domain than the one that served the original content. CORS is a web standard that defines a set of HTTP headers and a protocol for enabling safe cross-origin requests between different domains.
  2. Describe how you would be able to implement CORS into your own websites To implement CORS into your own website, you need to configure your web server to send appropriate CORS headers in the response to requests from other domains.
  3. Describe why you would want to implement CORS into your own websites Cross-domain access: CORS allows you to enable cross-domain access to your website's resources, which can be useful if you want to share data or resources with other websites or if you want to build a web application that needs to access resources from different domains.
  4. How could use CORS to benefit yourself in the future? Cross-domain data sharing: CORS can be used to share data between different websites, which can be useful in scenarios such as social media integration or collaborative applications.

Total: 0.2 points

KASM Hacks

  1. What is the purpose of "sudo" when running commands in terminal? The purpose of "sudo" when running commands in the terminal is to execute the command with elevated privileges, usually as the "root" user or a user with administrative privileges. In Unix-based operating systems like Linux and macOS, the "root" user is the user with the highest level of system access, and it can perform any operation on the system.
  2. What are some commands which allow us to look at how the storage of a machine is set up as? df -h: This command shows the disk space usage on all mounted file systems in a human-readable format. It shows the total size, used space, free space, and percentage of used space for each file system. lsblk: This command lists all available block devices, such as hard drives, solid-state drives, and USB drives, and their corresponding mount points. It shows the size, type, and file system of each block device.
  3. What do you think are some alternatives to running "curl -O" to get the zip file for KASM? Use wget: Instead of using curl, you can use the wget command to download the KASM zip file. The wget command is similar to curl and can be used to download files from the web. For example, you can use the command "wget https://download.kasmweb.com/kasm_workspaces.zip" to download the KASM zip file.
  4. What kind of commands do you think the "install.sh" command has and why is it necessary to call it? Dependency checks: The script may check for the required dependencies and software packages needed for the software installation. Download and extraction: The script may download and extract the software files from a remote server or archive file.
  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. Deploying KASM requires knowledge of several topics covered in the lesson, such as networking, server management, and security. Specifically, deploying KASM involves configuring a server to host the KASM application, setting up the appropriate network infrastructure to allow access to the application, and securing the server and application to prevent unauthorized access.

To add more detail to this guide, it could be useful to include sections on configuring a server for KASM deployment, setting up a network infrastructure for KASM access, and securing the KASM application and server. Additionally, information on how to troubleshoot common issues that may arise during the deployment process could be included. Finally, examples of how KASM can be used in real-world scenarios could be added to provide context and demonstrate the practical applications of KASM. Total: 0.2 points