Repository navigation
Expand file tree
/
Copy pathIPDiscovery.py
More file actions
153 lines (128 loc) · 5.38 KB
/
Copy pathIPDiscovery.py
File metadata and controls
153 lines (128 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
############################################################################
# LOCAl MAP Created By Matin Afzal
# https://github.com/MatinAfzal
# contact.matin@yahoo.com
############################################################################
############################################################################
# DISCLAIMER: This tool is made only to increase the knowledge
# and security of the internal network, The responsibility of
# any unethical and illegal use of this tool is on user!
############################################################################
###################################-HELP-###################################
# C: means class
# F: means function
# FF: means a function that should only be called by another function
# EJ: means extra work that the module does (Extra job)
############################################################################
############################################################################
# C: This module contains the IPDiscovery class
# Discovery class function and their tasks:
# F: getLocalIP() : Finds the local address of the host machine.
# F: getPossibleHosts() : Through the local address of the host
# and the specified IP range, it prepares a complete list of possible
# devices in the network.
# F: getMAC(): It receives the IP address of a live device in the network
# and returns its MAC address.
# F: getMacManufacturer(): It receives the MAC address of a device and
# performs a MAC Lookup to find the specifications of the network card
# manufacturer.
############################################################################
import socket
import ipaddress
from mac_vendor_lookup import MacLookup
from scapy.layers.l2 import ARP, Ether
from scapy.all import *
# Module identity information
__author__ = "Matin Afzal (contact.matin@yahoo.com)"
__version__ = "0.0.5"
__last_modification__ = "2023/03/27"
class IPDiscovery():
"""
A class to find IP address information
"""
def __init__(self, prefix='16', IP=''):
"""
Definition of IP address information variables & Attributes
-host_os : host device operating system to assign the appropriate switches
-prefix : prefix to specify the range of possible machines in the IP address range /8 /16 /24
-IP : IP address inside the network with default value = ''
"""
# IP address inside the network
self.IP = IP
# getLocalIP() Variables
self.default_local_IP = "127.0.0.1"
self.local_IP = ''
self.client_timeout = 0
# getPossibleHosts() Variables
self.prefix = prefix
self.range_ip = []
self.possible_hosts = []
# getMAC() Variables
self.mac = ''
self.mac_manufacturer = ''
def getLocalIP(self):
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.settimeout(self.client_timeout)
try:
# dosent even have to reachable
client.connect(("10.254.254.254", 1))
self.local_IP = client.getsockname()[0]
except Exception:
self.local_IP = self.default_local_IP
finally:
client.close()
def getPossibleHosts(self, host_range=False, **kwargs):
"""
Build a complete list of possible devices
-range : Specifies the range selection mode for possible hosts
-**kwargs : -start_range and -end_range
"""
self.range_ip = self.local_IP.split(".")
if self.prefix == '8':
self.range_ip = ".".join(self.range_ip[0:1])
self.range_ip += ".0.0.0"
elif self.prefix == '16':
self.range_ip = ".".join(self.range_ip[0:2])
self.range_ip += ".0.0"
elif self.prefix == '24':
self.range_ip = ".".join(self.range_ip[0:3])
self.range_ip += ".0"
for ip in ipaddress.IPv4Network('{}/{}'.format(self.range_ip, self.prefix)):
self.possible_hosts.append(str(ip))
if host_range:
start_range = kwargs["start_range"]
end_range = kwargs["end_range"]
self.possible_hosts = self.possible_hosts[start_range:end_range]
def getMAC(self):
"""
This function tries to find the MAC address of the device through IP
"""
conf.verb = 0 # hide all verbose of scapy
try:
arp = ARP(pdst=self.IP)
ether = Ether(dst="ff:ff:ff:ff:ff:ff")
packet = ether/arp
result = srp(packet, timeout=3, verbose=0)[0]
clients = []
for sent, received in result:
clients.append({"ip": received.psrc, "mac": received.hwsrc})
except:
self.mac = "Not Found"
else:
for client in clients:
self.mac = client["mac"]
def getMacManufacturer(self):
"""
This function tries to identify the manufacturer information of the device through mac address
"""
mac = MacLookup()
try:
mac.update_vendors()
except:
pass
try:
self.mac_manufacturer = mac.lookup(self.mac)
except:
self.mac_manufacturer = "Not Found"
else:
pass