forked from p34709852/monkey
Merge branch 'develop' of https://github.com/guardicore/monkey into develop
This commit is contained in:
commit
5240b4c0f0
11
.travis.yml
11
.travis.yml
|
@ -4,14 +4,11 @@ cache: pip
|
||||||
python:
|
python:
|
||||||
- 2.7
|
- 2.7
|
||||||
- 3.6
|
- 3.6
|
||||||
#- nightly
|
|
||||||
#- pypy
|
|
||||||
#- pypy3
|
|
||||||
matrix:
|
matrix:
|
||||||
allow_failures:
|
include:
|
||||||
- python: nightly
|
- python: 3.7
|
||||||
- python: pypy
|
dist: xenial # required for Python 3.7 (travis-ci/travis-ci#9069)
|
||||||
- python: pypy3
|
sudo: required # required for Python 3.7 (travis-ci/travis-ci#9069)
|
||||||
install:
|
install:
|
||||||
#- pip install -r requirements.txt
|
#- pip install -r requirements.txt
|
||||||
- pip install flake8 # pytest # add another testing frameworks later
|
- pip install flake8 # pytest # add another testing frameworks later
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
import logging
|
import logging
|
||||||
|
import sys
|
||||||
|
import subprocess
|
||||||
import select
|
import select
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from six import text_type
|
||||||
|
import ipaddress
|
||||||
|
|
||||||
DEFAULT_TIMEOUT = 10
|
DEFAULT_TIMEOUT = 10
|
||||||
BANNER_READ = 1024
|
BANNER_READ = 1024
|
||||||
|
|
||||||
|
@ -167,3 +172,60 @@ def check_tcp_ports(ip, ports, timeout=DEFAULT_TIMEOUT, get_banner=False):
|
||||||
|
|
||||||
def tcp_port_to_service(port):
|
def tcp_port_to_service(port):
|
||||||
return 'tcp-' + str(port)
|
return 'tcp-' + str(port)
|
||||||
|
|
||||||
|
|
||||||
|
def traceroute(target_ip, ttl):
|
||||||
|
"""
|
||||||
|
Traceroute for a specific IP.
|
||||||
|
:param target_ip: Destination
|
||||||
|
:param ttl: Max TTL
|
||||||
|
:return: Sequence of IPs in the way
|
||||||
|
"""
|
||||||
|
if sys.platform == "win32":
|
||||||
|
try:
|
||||||
|
# we'll just use tracert because that's always there
|
||||||
|
cli = ["tracert",
|
||||||
|
"-d",
|
||||||
|
"-w", "250",
|
||||||
|
"-h", str(ttl),
|
||||||
|
target_ip]
|
||||||
|
proc_obj = subprocess.Popen(cli, stdout=subprocess.PIPE)
|
||||||
|
stdout, stderr = proc_obj.communicate()
|
||||||
|
ip_lines = stdout.split('\r\n')[3:-3]
|
||||||
|
trace_list = []
|
||||||
|
for line in ip_lines:
|
||||||
|
tokens = line.split()
|
||||||
|
last_token = tokens[-1]
|
||||||
|
try:
|
||||||
|
ip_addr = ipaddress.ip_address(text_type(last_token))
|
||||||
|
except ValueError:
|
||||||
|
ip_addr = ""
|
||||||
|
trace_list.append(ip_addr)
|
||||||
|
return trace_list
|
||||||
|
except:
|
||||||
|
return []
|
||||||
|
else: # linux based hopefully
|
||||||
|
# implementation note: We're currently going to just use ping.
|
||||||
|
# reason is, implementing a non root requiring user is complicated (see traceroute(8) code)
|
||||||
|
# while this is just ugly
|
||||||
|
# we can't use traceroute because it's not always installed
|
||||||
|
current_ttl = 1
|
||||||
|
trace_list = []
|
||||||
|
while current_ttl <= ttl:
|
||||||
|
try:
|
||||||
|
cli = ["ping",
|
||||||
|
"-c", "1",
|
||||||
|
"-w", "1",
|
||||||
|
"-t", str(current_ttl),
|
||||||
|
target_ip]
|
||||||
|
proc_obj = subprocess.Popen(cli, stdout=subprocess.PIPE)
|
||||||
|
stdout, stderr = proc_obj.communicate()
|
||||||
|
ip_line = stdout.split('\n')
|
||||||
|
ip_line = ip_line[1]
|
||||||
|
ip = ip_line.split()[1]
|
||||||
|
trace_list.append(ipaddress.ip_address(text_type(ip)))
|
||||||
|
except (IndexError, ValueError):
|
||||||
|
# assume we failed parsing output
|
||||||
|
trace_list.append("")
|
||||||
|
current_ttl += 1
|
||||||
|
return trace_list
|
||||||
|
|
Loading…
Reference in New Issue