03. Data Types
Introduction¶
Data types are the foundation of Python. In cybersecurity, they help us store, process, and manipulate information like:
-
IP addresses & ports (strings, integers, tuples)
-
Credentials & payloads (strings, dictionaries, bytes)
-
Network logs & results (lists, sets, dicts)
-
Encryption, packet data, session info (bytes, booleans, None)
Core Data Types in Python (with Cybersecurity Examples)¶
1. Strings (str)¶
- Store text → usernames, payloads, log entries, URLs.
Usage: SQLi payloads, URLs, wordlists, log analysis.
2. Integers (int)¶
- Store whole numbers → ports, counts, retries.
Usage: Port scanning, failed login counters.
3. Floats (float)¶
- Store decimal values → delays, timings.
Usage: Latency in ping sweeps, timing attacks.
4. Booleans (bool)¶
- True/False values → conditions.
Usage: Service status (open/closed), login success/failure.
5. Lists (list)¶
- Ordered, changeable collections → multiple IPs, payloads.
Usage: Target lists, usernames, passwords.
6. Tuples (tuple)¶
- Ordered, immutable collections → fixed data like IP-port pairs.
Usage: Host-port mapping.
7. Sets (set)¶
- Unordered, unique values → no duplicates.
Usage: Unique IPs in logs, deduplication in scans.
8. Dictionaries (dict)¶
- Key-value pairs → configs, credentials, structured results.
Usage: Login credentials, HTTP headers, scan reports.
9. Bytes (bytes)¶
- Store raw binary data → packets, hashes, encrypted content.
Usage:
-
Crafting packets with Scapy
-
Handling hashes (MD5/SHA256)
-
Working with encrypted data
10. NoneType (None)¶
- Represents no value / null.
Usage: No session, no connection, placeholder for empty values.
Data Type Conversions¶
Cybersecurity scripts often need conversions (e.g., from strings to integers).
Cybersecurity Mini Example (Mixed Types)¶
target = {
"ip": "192.168.1.10", # string
"ports": [22, 80, 443], # list of ints
"status": True, # bool
"response_time": 0.21, # float
"banner": b"SSH-2.0-OpenSSH", # bytes
"session": None # None
}
print(target)
Quick Reference Table¶
| Data Type | Example | Cybersecurity Usage |
|---|---|---|
| String | "admin", "http://example.com" |
Payloads, URLs, logs, wordlists |
| Integer | 80, 443 |
Port numbers, counters |
| Float | 0.532 |
Response times, delays |
| Boolean | True, False |
Service status, login checks |
| List | ["192.168.1.1", "192.168.1.2"] |
Target lists, wordlists |
| Tuple | ("192.168.1.10", 22) |
Host-port mapping |
| Set | {"192.168.1.1", "192.168.1.2"} |
Unique IPs, deduplication |
| Dict | {"user":"admin","pass":"1234"} |
Credentials, headers, reports |
| Bytes | b"SSH-2.0-OpenSSH" |
Packets, hashes, encryption |
| None | None |
No session, null value |
Practice Section — Cybersecurity Exercises¶
Strings¶
- Create a variable with a SQL injection payload and print it.
- Extract the domain name from
"http://example.com/login".
Integers¶
- Store port numbers in variables and print them.
- Write a loop that prints ports from
20to25.
Floats¶
- Simulate a ping sweep storing different response times as floats.
- Print if latency is > 1.0 sec = "High Latency", else
"OK".
Booleans¶
- Write a script that checks if a given port is 22. If yes, print
"SSH detected".
Lists¶
- Store a list of IP addresses and iterate over them.
- Write a script that checks if
"192.168.1.5"exists in the list.
Tuples¶
- Store an IP and port pair in a tuple. Print both.
- Try modifying the tuple → notice it’s immutable.
Sets¶
- Store IPs with duplicates in a set. Print the unique IPs.
- Combine two sets of IPs using union().
Dictionaries¶
- Store credentials in a dictionary and print the username.
- Store open ports of an IP in a dictionary →
{"192.168.1.1": [22, 80]}.
Bytes¶
- Convert the string
"Hello"into bytes and print it.
- Create a fake HTTP request in bytes and print it.
NoneType¶
- Create a variable
session = None. - Check if session is
Noneand print"No active session".
Tip: Try combining datatypes. Example:
scan_result = {
"ip": "192.168.1.5",
"ports": [22, 80],
"status": True,
"banner": b"SSH-2.0-OpenSSH",
"latency": 0.34,
"session": None
}
print(scan_result)
Takeaway:
Each Python data type has a direct role in cybersecurity scripting → from handling payloads and credentials to crafting packets and analyzing logs.