forked from p34709852/monkey
Merge pull request #114 from guardicore/master
Rebase develop onto master
This commit is contained in:
commit
f37c3aaa2c
|
@ -25,7 +25,7 @@ def _cast_by_example(value, example):
|
||||||
if example_type is str:
|
if example_type is str:
|
||||||
return os.path.expandvars(value).encode("utf8")
|
return os.path.expandvars(value).encode("utf8")
|
||||||
elif example_type is tuple and len(example) != 0:
|
elif example_type is tuple and len(example) != 0:
|
||||||
if value is None or value == tuple(None):
|
if value is None or value == tuple([None]):
|
||||||
return tuple()
|
return tuple()
|
||||||
return tuple([_cast_by_example(x, example[0]) for x in value])
|
return tuple([_cast_by_example(x, example[0]) for x in value])
|
||||||
elif example_type is list and len(example) != 0:
|
elif example_type is list and len(example) != 0:
|
||||||
|
|
|
@ -106,32 +106,38 @@ def check_tcp_ports(ip, ports, timeout=DEFAULT_TIMEOUT, get_banner=False):
|
||||||
"""
|
"""
|
||||||
sockets = [socket.socket(socket.AF_INET, socket.SOCK_STREAM) for _ in range(len(ports))]
|
sockets = [socket.socket(socket.AF_INET, socket.SOCK_STREAM) for _ in range(len(ports))]
|
||||||
[s.setblocking(0) for s in sockets]
|
[s.setblocking(0) for s in sockets]
|
||||||
good_ports = []
|
possible_ports = []
|
||||||
|
connected_ports_sockets = []
|
||||||
try:
|
try:
|
||||||
LOG.debug("Connecting to the following ports %s" % ",".join((str(x) for x in ports)))
|
LOG.debug("Connecting to the following ports %s" % ",".join((str(x) for x in ports)))
|
||||||
for sock, port in zip(sockets, ports):
|
for sock, port in zip(sockets, ports):
|
||||||
err = sock.connect_ex((ip, port))
|
err = sock.connect_ex((ip, port))
|
||||||
if err == 0:
|
if err == 0: # immediate connect
|
||||||
good_ports.append((port, sock))
|
connected_ports_sockets.append((port, sock))
|
||||||
|
possible_ports.append((port, sock))
|
||||||
continue
|
continue
|
||||||
if err == 10035: # WSAEWOULDBLOCK is valid, see https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
|
if err == 10035: # WSAEWOULDBLOCK is valid, see
|
||||||
good_ports.append((port, sock))
|
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
|
||||||
|
possible_ports.append((port, sock))
|
||||||
continue
|
continue
|
||||||
if err == 115: # EINPROGRESS 115 /* Operation now in progress */
|
if err == 115: # EINPROGRESS 115 /* Operation now in progress */
|
||||||
good_ports.append((port, sock))
|
possible_ports.append((port, sock))
|
||||||
continue
|
continue
|
||||||
LOG.warning("Failed to connect to port %s, error code is %d", port, err)
|
LOG.warning("Failed to connect to port %s, error code is %d", port, err)
|
||||||
|
|
||||||
if len(good_ports) != 0:
|
if len(possible_ports) != 0:
|
||||||
time.sleep(timeout)
|
time.sleep(timeout)
|
||||||
# this is possibly connected. meaning after timeout wait, we expect to see a connection up
|
sock_objects = [s[1] for s in possible_ports]
|
||||||
# Possible valid errors codes if we chose to check for actually closed are
|
# first filter
|
||||||
# ECONNREFUSED (111) or WSAECONNREFUSED (10061) or WSAETIMEDOUT(10060)
|
_, writeable_sockets, _ = select.select(sock_objects, sock_objects, sock_objects, 0)
|
||||||
connected_ports_sockets = [s for s in good_ports if
|
for s in writeable_sockets:
|
||||||
s[1].getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) == 0]
|
try: # actual test
|
||||||
|
connected_ports_sockets.append((s.getpeername()[1], s))
|
||||||
|
except socket.error: # bad socket, select didn't filter it properly
|
||||||
|
pass
|
||||||
LOG.debug(
|
LOG.debug(
|
||||||
"On host %s discovered the following ports %s" %
|
"On host %s discovered the following ports %s" %
|
||||||
(str(ip), ",".join([str(x[0]) for x in connected_ports_sockets])))
|
(str(ip), ",".join([str(s[0]) for s in connected_ports_sockets])))
|
||||||
banners = []
|
banners = []
|
||||||
if get_banner:
|
if get_banner:
|
||||||
readable_sockets, _, _ = select.select([s[1] for s in connected_ports_sockets], [], [], 0)
|
readable_sockets, _, _ = select.select([s[1] for s in connected_ports_sockets], [], [], 0)
|
||||||
|
@ -140,7 +146,7 @@ def check_tcp_ports(ip, ports, timeout=DEFAULT_TIMEOUT, get_banner=False):
|
||||||
for port, sock in connected_ports_sockets]
|
for port, sock in connected_ports_sockets]
|
||||||
pass
|
pass
|
||||||
# try to cleanup
|
# try to cleanup
|
||||||
[s[1].close() for s in good_ports]
|
[s[1].close() for s in possible_ports]
|
||||||
return [port for port, sock in connected_ports_sockets], banners
|
return [port for port, sock in connected_ports_sockets], banners
|
||||||
else:
|
else:
|
||||||
return [], []
|
return [], []
|
||||||
|
|
Loading…
Reference in New Issue