coercer.network.utils

 1#!/usr/bin/env python3
 2# -*- coding: utf-8 -*-
 3# File name          : utils.py
 4# Author             : Podalirius (@podalirius_)
 5# Date created       : 17 Sep 2022
 6
 7import socket
 8import fcntl
 9import struct
10
11
12def get_ip_address_of_interface(ifname):
13    """
14    Function get_ip_address_of_interface(ifname)
15    """
16    if type(ifname) == str:
17        ifname = bytes(ifname, "utf-8")
18    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
19    SIOCGIFADDR = 0x8915
20    try:
21        ifname = struct.pack('256s', ifname[:15])
22        a = fcntl.ioctl(s.fileno(), SIOCGIFADDR, ifname)[20:24]
23        return socket.inet_ntoa(a)
24    except OSError as e:
25        return None
26
27
28def get_ip_address_to_target_remote_host(host, port):
29    """
30    Function get_ip_address_to_target_remote_host(host, port)
31    """
32    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
33    try:
34        s.connect((host, port))
35        return s.getsockname()[0]
36    except Exception as e:
37        return None
38
39
40def can_listen_on_port(listen_ip, port):
41    """
42    Function can_listen_on_port(listen_ip, port)
43    """
44    try:
45        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46        s.settimeout(0.05)
47        s.bind((listen_ip, port))
48        s.listen(5)
49        s.close()
50        return True
51    except OSError as e:
52        return False
53
54
55def get_ip_addr_to_listen_on(target, options):
56    """
57    Function get_ip_addr_to_listen_on(target, options)
58    """
59    # Getting IP address to listen on
60    listening_ip = None
61    if options.ip_address is not None:
62        listening_ip = options.ip_address
63    elif options.interface is not None:
64        listening_ip = get_ip_address_of_interface(options.interface)
65        if listening_ip is None:
66            print("[!] Could not get IP address of interface '%s'" % options.interface)
67    else:
68        # Getting ip address of interface that can access remote target
69        possible_ports, k = [445, 139, 88], 0
70        while listening_ip is None and k < len(possible_ports):
71            listening_ip = get_ip_address_to_target_remote_host(target, possible_ports[k])
72            k += 1
73        if listening_ip is None:
74            print("[!] Could not detect interface with a route to target machine '%s'" % target)
75    return listening_ip
76
77
78def get_next_http_listener_port(current_value, listen_ip, options):
79    """
80    Function get_next_http_listener_port(current_value, listen_ip, options)
81    """
82    port_window = (options.max_http_port - options.min_http_port)
83
84    if current_value > options.max_http_port:
85        current_value = options.max_http_port
86
87    if current_value < options.min_http_port:
88        current_value = options.min_http_port
89
90    current_value = options.min_http_port + ((current_value + 1) % port_window)
91    while not can_listen_on_port(listen_ip, current_value):
92        current_value = options.min_http_port + ((current_value + 1) % port_window)
93
94    return current_value
def get_ip_address_of_interface(ifname):
13def get_ip_address_of_interface(ifname):
14    """
15    Function get_ip_address_of_interface(ifname)
16    """
17    if type(ifname) == str:
18        ifname = bytes(ifname, "utf-8")
19    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
20    SIOCGIFADDR = 0x8915
21    try:
22        ifname = struct.pack('256s', ifname[:15])
23        a = fcntl.ioctl(s.fileno(), SIOCGIFADDR, ifname)[20:24]
24        return socket.inet_ntoa(a)
25    except OSError as e:
26        return None

Function get_ip_address_of_interface(ifname)

def get_ip_address_to_target_remote_host(host, port):
29def get_ip_address_to_target_remote_host(host, port):
30    """
31    Function get_ip_address_to_target_remote_host(host, port)
32    """
33    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
34    try:
35        s.connect((host, port))
36        return s.getsockname()[0]
37    except Exception as e:
38        return None

Function get_ip_address_to_target_remote_host(host, port)

def can_listen_on_port(listen_ip, port):
41def can_listen_on_port(listen_ip, port):
42    """
43    Function can_listen_on_port(listen_ip, port)
44    """
45    try:
46        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
47        s.settimeout(0.05)
48        s.bind((listen_ip, port))
49        s.listen(5)
50        s.close()
51        return True
52    except OSError as e:
53        return False

Function can_listen_on_port(listen_ip, port)

def get_ip_addr_to_listen_on(target, options):
56def get_ip_addr_to_listen_on(target, options):
57    """
58    Function get_ip_addr_to_listen_on(target, options)
59    """
60    # Getting IP address to listen on
61    listening_ip = None
62    if options.ip_address is not None:
63        listening_ip = options.ip_address
64    elif options.interface is not None:
65        listening_ip = get_ip_address_of_interface(options.interface)
66        if listening_ip is None:
67            print("[!] Could not get IP address of interface '%s'" % options.interface)
68    else:
69        # Getting ip address of interface that can access remote target
70        possible_ports, k = [445, 139, 88], 0
71        while listening_ip is None and k < len(possible_ports):
72            listening_ip = get_ip_address_to_target_remote_host(target, possible_ports[k])
73            k += 1
74        if listening_ip is None:
75            print("[!] Could not detect interface with a route to target machine '%s'" % target)
76    return listening_ip

Function get_ip_addr_to_listen_on(target, options)

def get_next_http_listener_port(current_value, listen_ip, options):
79def get_next_http_listener_port(current_value, listen_ip, options):
80    """
81    Function get_next_http_listener_port(current_value, listen_ip, options)
82    """
83    port_window = (options.max_http_port - options.min_http_port)
84
85    if current_value > options.max_http_port:
86        current_value = options.max_http_port
87
88    if current_value < options.min_http_port:
89        current_value = options.min_http_port
90
91    current_value = options.min_http_port + ((current_value + 1) % port_window)
92    while not can_listen_on_port(listen_ip, current_value):
93        current_value = options.min_http_port + ((current_value + 1) % port_window)
94
95    return current_value

Function get_next_http_listener_port(current_value, listen_ip, options)