-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmacfinder.py
executable file
·146 lines (117 loc) · 4.13 KB
/
macfinder.py
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
#!/usr/bin/env python
"""
This program tries to be a Fing-like tool
https://play.google.com/store/apps/details?id=com.overlook.android.fing&hl=en
It scans your Wi-Fi network, and discovers which devices are connected.
Author: Fernando Tricas Garcia ([email protected])
Based on a work of [email protected] that can be found at:
https://github.com/mpescimoro/WiFinder
Licence : GPL v3 or any later version
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import sys
import nmap # import nmap.py
#from nmap import nmap
import pwd, grp
import time
import pickle
import pprint
def loadData():
ipList={}
try:
fIP = open(fileName,"rb")
try:
ipList=pickle.load(fIP)
except:
print("The file does not contain adequate data")
except:
print("No file with stored ips ")
ipList={}
print(ipList)
return ipList
# http://stackoverflow.com/questions/2699907/dropping-root-permissions-in-python
#Throws OSError exception (it will be thrown when the process is not allowed
#to switch its effective UID or GID):
def drop_privileges():
if os.getuid() != 0:
# We're not root so, like, whatever dude
return
# Get the uid/gid from the name
user_name = os.getenv("SUDO_USER")
pwnam = pwd.getpwnam(user_name)
# Remove group privileges
os.setgroups([])
# Try setting the new uid/gid
os.setgid(pwnam.pw_gid)
os.setuid(pwnam.pw_uid)
#Ensure a reasonable umask
old_umask = os.umask(0o22)
def seek(ipList): # defines a function to analize the network
count = 0
nm.scan(hosts='192.168.1.0/24', arguments='-n -sP -PE -T5')
# executes a ping scan
hosts_list = [(nm[x]['addresses']) for x in nm.all_hosts()]
# saves the host list
pprint.pprint(hosts_list)
print("ipList")
pprint.pprint(ipList)
for addresses in hosts_list:
print("addressses", addresses)
count = count + 1
#try:
if 'mac' in addresses and not addresses['mac'] in ipList:
ipList[addresses['mac']] = ("", addresses['ipv4'])
#except:
# pass
pprint.pprint(ipList)
return count # returns the host number
def name(pList):
# defines a function to analize the network
print("nombres")
print(ipList)
for mac in ipList.keys():
name = raw_input(mac+" ("+ipList[mac][0]+","+ipList[mac][1]+") Name? ")
if name != "":
ipList[mac]=(name, ipList[mac][1])
if __name__ == '__main__':
# File used to store data
fileName=os.path.expanduser('~')+'/.ipList.txt'
try:
nm = nmap.PortScanner() # creates an'instance of nmap.PortScanner
ipList = {}
except nmap.PortScannerError:
print('Nmap not found', sys.exc_info()[0])
sys.exit(0)
except:
print("Unexpected error:", sys.exc_info()[0])
sys.exit(0)
count = 1
ipList=loadData()
if (len(sys.argv)>1 and (sys.argv[1] == "-l")):
pprint.pprint(ipList)
sys.exit()
# check if the number of addresses is still the same
while (count <= 10):
print("Pass: ",count, "Found: ", seek(ipList), "Total: ", len(ipList))
time.sleep(1)
count = count + 1
print(os.getresuid())
print("We do not need root privileges anymore ...")
drop_privileges()
print(os.getresuid())
print("========= So .... =======")
pprint.pprint(ipList)
name(ipList)
print(ipList,type(ipList))
fIP = open(fileName,"wb")
pickle.dump(ipList,fIP)