Skip to content
Tutorials Center
My learning

Topics

Lesson 51 of 53 ยท Practice and Review

Practice Exercises with Solutions

Practice ethical hacking and defensive security with scoped questions and coding exercises across the whole course, each with a worked answer.

Reading explains ideas; practice makes them stick. This lesson collects exercises from every chapter of the course: short conceptual questions, scoping and reporting tasks, and coding exercises you can run in the browser. Every exercise has a worked answer. Try each one yourself first, write down your answer, and only then open the solution.

All coding exercises are stand-ins that work on made-up data inside the editor, so nothing here touches a real system. Keep that rule in real life too: test only systems you own or have written permission to test, and stay inside the agreed scope.

How to practice

Attempt first Answer before you read.
Run it Change the code and re-run.
Revisit Repeat weak chapters later.

Struggling for five minutes teaches more than reading for ten.

1 / 2

Part 1: Foundations, scope and law

Exercise 1. A client's authorization letter lists app.example.com and api.example.com. During testing you find admin.example.com, which looks vulnerable. What do you do?

Answer. Do not test it. It is not in the written scope. Note that it exists, stop, and tell your client contact so they can confirm in writing whether it is owned by them and may be added. Scope is a legal boundary as well as a technical one (Law, Scope and Rules of Engagement).

Exercise 2. Name the five elements a rules-of-engagement document should contain.

Answer. Any sensible five from: authorized targets and exclusions, testing window and times, permitted techniques (for example no denial of service or social engineering unless agreed), contact and escalation details, data handling and reporting rules, and a "stop" procedure if something breaks.

Exercise 3. Using STRIDE, classify "an attacker changes the price field in a request before it reaches the server".

Answer. Tampering. It breaks integrity, and the defense is to never trust client-supplied prices and to look them up on the server (Threat Modeling).

Coding exercise A: scope checker

Write a function inScope(target, allowList) that returns true only when the host is exactly on the list or is a subdomain of a listed domain, and that does not accept look-alikes such as app.example.com.evil.test. Then test it.

JS Solution A: scope checker

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The key is "." + domain: matching on the dot stops notexample.com from passing. Comparing the end of the string (not "contains") stops the look-alike app.example.com.evil.test. Real scope checks must also consider IP ranges and redirects.

Part 2: Lab, networking and reconnaissance

Exercise 4. Why use a host-only or isolated virtual network for vulnerable training machines?

Answer. Deliberately vulnerable systems can be taken over by anyone who reaches them. Isolation stops outsiders from using your lab, and stops your own tools from touching real networks (Building a Safe Home Lab).

Exercise 5. In the TCP three-way handshake, which packet does the server send first, and what flags does it carry?

Answer. After the client's SYN, the server replies with SYN-ACK. A completed connection scan finishes the handshake with an ACK; a half-open SYN scan stops after the SYN-ACK (Networking Essentials).

Exercise 6. Give two ways an organization can reduce what passive reconnaissance reveals about it.

Answer. Examples: remove metadata from published documents, avoid listing internal host names in public DNS, limit staff details on public pages, retire forgotten subdomains and old certificates, and keep staff training on what to share online (Passive Reconnaissance).

Part 3: Cryptography, passwords and web security

Exercise 7. Why is a fast hash a poor choice for storing passwords, and what should you use?

Answer. Fast hashes allow billions of guesses per second on stolen hashes. Use a slow, salted password hash such as Argon2id, bcrypt or scrypt (Password Attacks and Defenses).

Exercise 8. Rewrite this vulnerable pseudo-code safely: query = "SELECT * FROM users WHERE name = '" + name + "'".

Answer. Use a parameterized query so input stays data: query = "SELECT * FROM users WHERE name = ?" with name passed as a parameter. Also give the database account only the permissions the app needs (SQL Injection).

Coding exercise B: spot the injectable query

Write one function that builds SQL by joining user input into the string and one that uses a parameter. Run both with a normal name and with an attack string on a toy database, and explain the difference.

Python Solution B: vulnerable vs parameterized

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The vulnerable version returns every row for the attack string because the quote changed the meaning of the query. The parameterized version treats the whole string as a name and finds nothing.

Exercise 9. A site stores a session token in a cookie. Which three cookie attributes help protect it, and what does each do?

Answer. HttpOnly hides it from page scripts (limits theft through XSS), Secure sends it only over HTTPS, and SameSite limits cross-site sending (helps against CSRF) (XSS and CSRF Defense).

Coding exercise C: output encoding

Write escapeHtml that makes untrusted text safe to place in HTML element content, and check it with a hostile input.

JS Solution C: escape untrusted text

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The ampersand must be replaced first, otherwise the other replacements would be double-encoded. Remember that escaping depends on context: this version is for element text and quoted attribute values, not for JavaScript or URL contexts.

Part 4: Network, cloud and systems

Exercise 10. What does ARP spoofing let an attacker on the same local network do, and name one defense?

Answer. It lets them place themselves between a victim and the gateway to read or alter traffic. Defenses include dynamic ARP inspection on switches, network segmentation, and end-to-end encryption such as TLS so intercepted data is useless (Network Attacks and Defenses).

Exercise 11. Why is an s3-style storage bucket set to "public read" dangerous even if the file names are hard to guess?

Answer. Obscurity is not protection: names leak through links, logs and listing, and automated tools probe for open storage constantly. Make storage private by default and grant access with least privilege (Cloud and Container Security).

Coding exercise D: triage by risk

Given findings with a CVSS score, whether the asset is internet-facing and whether the flaw is known to be exploited, sort so the most urgent comes first.

Python Solution D: triage order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Finding B has only a medium score yet comes first because attackers are already using it against reachable systems. Compare with the fuller tracker in Project: Vulnerability Triage Tracker.

Part 5: Detection, response and reporting

Exercise 12. List the phases of incident response in order.

Answer. Preparation, detection and analysis, containment, eradication, recovery, and lessons learned (Incident Response).

Exercise 13. Rewrite this weak finding: "The login is insecure."

Answer. A good finding states what, where, impact, evidence, severity and fix. For example: "The /login endpoint allows unlimited password attempts (tested with 200 requests in one minute, all processed). An attacker can guess passwords and take over accounts. Severity: High. Fix: limit attempts per account and address, add temporary lockout and MFA" (Writing a Penetration Test Report).

Coding exercise E: find a hard-coded secret

Write a scanner that flags lines assigning a long string to a variable whose name suggests a secret.

JS Solution E: simple secret finder

Press Run (or Ctrl + Enter) to see the console output here.

No output.

It flags the two literals and skips the environment-variable lookup, which is the recommended pattern. Real scanners add entropy checks and known token formats, and they will still produce false positives, so a human reviews the results (Secure Coding and DevSecOps).

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. You discover an out-of-scope host that looks vulnerable. What is the correct action?

    Authorization is limited to the written scope. Out-of-scope testing can be illegal even with good intentions.
  2. 2. Why must & be escaped first in an HTML escape function?

    Escaping < creates &lt;. If & were replaced afterward, that new ampersand would become &amp;lt;.
  3. 3. Why can a medium CVSS finding outrank a critical one in triage?

    Context such as internet exposure and known exploitation changes the risk to your organization.

Summary

  • Practice each topic by predicting an answer before reading the solution.
  • Scope, authorization and reporting skills matter as much as technical skills.
  • Defensive coding exercises (parameterized queries, output encoding, secret scanning) turn concepts into habits.
  • Triage uses context, not only a score.
  • Keep every exercise inside a lab or on made-up data.

Next: Interview Questions.