-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhostResolver.py
219 lines (200 loc) · 8.56 KB
/
hostResolver.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# imports
######
import queue
from threading import Lock
import shlex, subprocess
from copy import deepcopy
from playsound import playsound
import configuration
from time import sleep
from pathlib import Path
import os
########################
class HostResolverClass(object):
__hostRequestResolutionQueue = queue.Queue()
__hostResolvedList = []
__hostsNotResolved = []
__mutexSolved = Lock()
# Tool:
WHOSIP_EXE = "whosip.exe" # will be completed with absolute path in __init__()
# counters
hostsRequested = 0
hostsResolved = 0
hostsFailed = 0
countersLock = Lock()
#############
def __init__(self):
#################
currAbsPath = Path().absolute()
currAbsPath = str(currAbsPath)
currAbsPath = currAbsPath.replace("\\", "/")
##########################################
runningScript = os.path.basename(__file__)
# when executing IPRadar2.exe we'll get hostResolver.pyc instead of hostResolver.py
if(runningScript=="hostResolver.py"):
currAbsPath = currAbsPath + "/dist/WhosIP/"
else:
currAbsPath = currAbsPath + "/WhosIP/"
##########################################
# print("Current directory: ", currAbsPath)
self.WHOSIP_EXE = currAbsPath+self.WHOSIP_EXE
#####################
def getHostsFailedPast(self):
return len(self.__hostsNotResolved) - 1
######################
# use command-line tool to resolve host
# the tool WhosIP is imilar to IPNetINfo which checks:
# ARIN, RiPE, ARNIC, LACNI, AfriNIC
# TODO: improvement: store all individual fields of whosip in separate fieds in node so we can use them directly and avoid parsing.
# e.g. see checkForHostsResolution() when checking if ower is in configuration.WhiteListOwner
######################
def whosip(self, hostIP):
dict_elem_host = {}
host = " (unknown)" # note: not able to distinguish from default value..?
whosip_response = "" # "WHOSIP_START#"
try:
command = self.WHOSIP_EXE+" "+hostIP
print(command)
p1 = subprocess.Popen(shlex.split(command), shell=True, stdout=subprocess.PIPE)
out, err = p1.communicate()
if p1.returncode == 0:
out = out.splitlines()
for netstatLine in out:
line = str(netstatLine)
if "b\'" in line:
line = line.replace("b\'", "") # workaround to get rid of strange characters.. b' at once..
if "\'" in line:
line = line.replace("\'", "") # workaround to get rid of strange characters.. each ' around texts
if line != "":
# comma is used as field separator, if e.g. name has a comma we then replace it with a semi-colon!
######################################################
# if "," in line:
line = line.replace(",", ";") # workaround to get rid of strange characters.. each ' around texts
print(line)
whosip_response = whosip_response + line
if "Owner Name:" in line:
for ix in range(13, len(line)-1):
a = line[ix]
a = a
if line[ix] != " ":
host = line[ix:len(line)]
print("WhosIP Owner Name = ", host)
break
whosip_response = whosip_response + ", "
else:
print("Error: could not execute whosip.exe correctly to find host information!")
##########
p1.terminate()
p1.kill()
except Exception as e: # avoid catching exceptions like SystemExit, KeyboardInterrupt, etc.
print("whosip(): Exception: ", e)
# build dictionary element
dict_elem_host = {"ip" : hostIP, "host" : hostIP + " " + host, "whosip" : whosip_response}
return dict_elem_host
######################
# de-queued hosts are processed here
######################
def __processHost(self, hostIP):
self.__mutexSolved.acquire()
try:
dict_elem_host = self.whosip(hostIP)
print("resolved host as dict: ", dict_elem_host["ip"])
self.__hostResolvedList.append(dict_elem_host)
self.countersLock.acquire()
self.hostsResolved = self.hostsResolved + 1
self.countersLock.release()
if configuration.SOUND:
playsound('Sounds/smb_jump-small.mp3', block=False)
except Exception as e: # avoid catching exceptions like SystemExit, KeyboardInterrupt, etc.
print("__processHost(): Exception while trying to resolve IP ", hostIP)
print("__processHost(): Exception: ", e)
# just in case we empty the queue, could be corrupted
self.__hostResolvedList = [] # we assume that any previous element has been consumed up to now.
# append unresolved host to list
if hostIP not in self.__hostsNotResolved:
self.__hostsNotResolved.append(hostIP)
self.countersLock.acquire()
self.hostsFailed = self.hostsFailed + 1
self.countersLock.release()
if configuration.SOUND:
playsound('Sounds/smb_mariodie.mp3', block=False)
finally:
self.__mutexSolved.release()
# end of __processHost()
###############
######################
# process hosts in queue
######################
def processingThread(self):
# main loop
########
while True:
if not self.__hostRequestResolutionQueue.empty():
host = self.__hostRequestResolutionQueue.get_nowait()
if host != None:
# resolve host name
self.__processHost(host)
# wait if queue is empty
##############
if self.__hostRequestResolutionQueue.empty():
sleep(configuration.CHECK_PERIOD_IN_SEC)
# end of processingThread()
#######################
######################
# get resolved hosts
######################
def getResolvedHosts(self):
resolvedHostsTemp = []
self.__mutexSolved.acquire()
try:
if self.__hostResolvedList:
resolvedHostsTemp = deepcopy(self.__hostResolvedList)
# emtpy/clear the list with resolved hosts, they were passed already
self.__hostResolvedList = []
else:
resolvedHostsTemp = []
except Exception as e:
print("Exception in getResolvedHosts = ", e)
resolvedHostsTemp = []
finally:
self.__mutexSolved.release()
return resolvedHostsTemp
# end of getResolvedHosts()
################
######################
# put host to resolve
######################
def putHostToResolve(self, host):
# try to resolve ONLY ONCE for now..
# otherwise we are continuously blocking "unresolvable" requests
if host not in self.__hostsNotResolved:
self.__hostRequestResolutionQueue.put(host)
self.countersLock.acquire()
self.hostsRequested = self.hostsRequested + 1
self.countersLock.release()
else:
# sorry, I tried that already..without success
pass
# end of putHostToResolve()
################
#################
def getNumberOfHostsRequested(self):
tempVal = 0 # int is an immutable object so assignment will get a COPY of the value
self.countersLock.acquire()
tempVal = self.hostsRequested
self.countersLock.release()
return tempVal
#################
def getNumberOfHostsSolved(self):
tempVal = 0 # int is an immutable object so assignment will get a COPY of the value
self.countersLock.acquire()
tempVal = self.hostsResolved
self.countersLock.release()
return tempVal
#################
def getNumberOfHostsFailed(self):
tempVal = 0 # int is an immutable object so assignment will get a COPY of the value
self.countersLock.acquire()
tempVal = self.hostsFailed
self.countersLock.release()
return tempVal