ajout port scanner
This commit is contained in:
93
cheat_cheat/modules.md
Normal file
93
cheat_cheat/modules.md
Normal file
@@ -0,0 +1,93 @@
|
||||
# 🐍 Core Python (your foundation)
|
||||
|
||||
These are built-in. You’ll use them everywhere.
|
||||
|
||||
* `socket` → low-level networking (port scanners, connections)
|
||||
* `subprocess` → run system commands (e.g. `ping`, `nmap`)
|
||||
* `os` → interact with the operating system
|
||||
* `sys` → handle arguments and runtime behavior
|
||||
* `threading` / `concurrent.futures` → speed up scans
|
||||
* `time` → delays, timing attacks (basic)
|
||||
* `re` → regex (log analysis, pattern detection)
|
||||
* `json` → handle API and structured data
|
||||
* `argparse` → clean CLI tools (very useful)
|
||||
|
||||
---
|
||||
|
||||
# 🌐 Networking & Web
|
||||
|
||||
Where most beginner cybersecurity action happens.
|
||||
|
||||
* `requests` → send HTTP requests (GET, POST, etc.)
|
||||
* `urllib` → lower-level web handling
|
||||
* `http.client` → raw HTTP interactions
|
||||
|
||||
👉 You’ll use these for:
|
||||
|
||||
* Web scanners
|
||||
* API testing
|
||||
* Automation scripts
|
||||
|
||||
---
|
||||
|
||||
# 🍲 Parsing & Scraping
|
||||
|
||||
Turning messy data into something useful.
|
||||
|
||||
* `BeautifulSoup` (bs4) → parse HTML
|
||||
* `lxml` → faster HTML/XML parsing
|
||||
|
||||
👉 Useful for:
|
||||
|
||||
* Extracting forms and inputs
|
||||
* Web reconnaissance
|
||||
|
||||
---
|
||||
|
||||
# 🔍 Security & Scanning
|
||||
|
||||
More “cyber-flavored” tools.
|
||||
|
||||
* `scapy` → packet crafting/sniffing (very powerful)
|
||||
* `nmap` (via `python-nmap`) → automate scans
|
||||
* `shodan` → search exposed devices (API)
|
||||
|
||||
👉 These unlock:
|
||||
|
||||
* Network analysis
|
||||
* Recon automation
|
||||
|
||||
---
|
||||
|
||||
# 🔐 Cryptography & Hashing
|
||||
|
||||
For passwords, encryption, and security logic.
|
||||
|
||||
* `hashlib` → hashing (MD5, SHA256, etc.)
|
||||
* `hmac` → message authentication
|
||||
* `cryptography` → encryption/decryption (advanced)
|
||||
|
||||
👉 Use cases:
|
||||
|
||||
* Password crackers
|
||||
* Hash verification
|
||||
|
||||
---
|
||||
|
||||
# 📂 File & Log Analysis
|
||||
|
||||
Blue team territory.
|
||||
|
||||
* `pandas` → analyze large datasets/logs
|
||||
* `csv` → parse log files
|
||||
* `collections` → count patterns (e.g. IP frequency)
|
||||
|
||||
---
|
||||
|
||||
# ⚙️ CLI & Tooling (make your scripts look pro)
|
||||
|
||||
* `argparse` → command-line arguments
|
||||
* `colorama` → colored terminal output
|
||||
* `rich` → beautiful CLI dashboards
|
||||
|
||||
---
|
||||
153
cheat_cheat/socket.md
Normal file
153
cheat_cheat/socket.md
Normal file
@@ -0,0 +1,153 @@
|
||||
The `socket` module is where Python stops being polite and starts speaking raw network 🧩📡
|
||||
Here’s a **clean, practical cheat sheet** you’ll actually use when building cyber tools.
|
||||
|
||||
---
|
||||
|
||||
# 🐍 `socket` Cheat Sheet (Cybersecurity Edition)
|
||||
|
||||
## 🔌 Create a Socket
|
||||
|
||||
```python
|
||||
import socket
|
||||
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
```
|
||||
|
||||
* `AF_INET` → IPv4
|
||||
* `SOCK_STREAM` → TCP
|
||||
* (use `SOCK_DGRAM` for UDP)
|
||||
|
||||
---
|
||||
|
||||
## 🌐 Connect to a Target (Client)
|
||||
|
||||
```python
|
||||
s.connect(("127.0.0.1", 80))
|
||||
```
|
||||
|
||||
👉 Used in:
|
||||
|
||||
* Port scanners
|
||||
* Banner grabbing
|
||||
|
||||
---
|
||||
|
||||
## 📤 Send Data
|
||||
|
||||
```python
|
||||
s.send(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
|
||||
```
|
||||
|
||||
* Must be **bytes** (`b""`)
|
||||
|
||||
---
|
||||
|
||||
## 📥 Receive Data
|
||||
|
||||
```python
|
||||
response = s.recv(1024)
|
||||
print(response.decode())
|
||||
```
|
||||
|
||||
* `1024` = buffer size
|
||||
|
||||
---
|
||||
|
||||
## ❌ Close Connection
|
||||
|
||||
```python
|
||||
s.close()
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# 🔍 Port Scanner Example (core pattern)
|
||||
|
||||
```python
|
||||
import socket
|
||||
|
||||
target = "127.0.0.1"
|
||||
|
||||
for port in range(1, 1025):
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(2)
|
||||
|
||||
result = s.connect_ex((target, port))
|
||||
|
||||
if result == 0:
|
||||
print(f"Port {port} is open")
|
||||
|
||||
s.close()
|
||||
```
|
||||
|
||||
🔥 Key trick:
|
||||
|
||||
* `connect_ex()` returns `0` if open (no crash, cleaner)
|
||||
|
||||
---
|
||||
|
||||
# ⏱️ Timeout (avoid freezing forever)
|
||||
|
||||
```python
|
||||
s.settimeout(1)
|
||||
```
|
||||
|
||||
Without this, your scanner becomes a fossil 🦴
|
||||
|
||||
---
|
||||
|
||||
# 🖥️ Simple Server (Listener)
|
||||
|
||||
```python
|
||||
import socket
|
||||
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.bind(("0.0.0.0", 4444))
|
||||
s.listen(1)
|
||||
|
||||
conn, addr = s.accept()
|
||||
print(f"Connection from {addr}")
|
||||
|
||||
data = conn.recv(1024)
|
||||
print(data.decode())
|
||||
|
||||
conn.close()
|
||||
```
|
||||
|
||||
👉 Used in:
|
||||
|
||||
* Backdoor simulations
|
||||
* Reverse shells (ethical labs only)
|
||||
|
||||
---
|
||||
|
||||
# 📡 UDP Socket (connectionless)
|
||||
|
||||
```python
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
s.sendto(b"Hello", ("127.0.0.1", 53))
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# 🧠 Useful Methods Quick List
|
||||
|
||||
* `socket()` → create socket
|
||||
* `connect()` → connect to server
|
||||
* `connect_ex()` → safer connect
|
||||
* `send()` / `sendall()` → send data
|
||||
* `recv()` → receive data
|
||||
* `bind()` → attach to IP/port
|
||||
* `listen()` → wait for connections
|
||||
* `accept()` → accept connection
|
||||
* `close()` → close socket
|
||||
|
||||
---
|
||||
|
||||
# ⚠️ Common Mistakes
|
||||
|
||||
* Forgetting `.close()` → zombie sockets
|
||||
* Not using timeout → slow scans
|
||||
* Sending string instead of bytes → 💥 error
|
||||
* Scanning too fast → gets blocked
|
||||
Reference in New Issue
Block a user