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

Function get_ip_address_of_interface(ifname)

def get_ip_address_to_target_remote_host(host, port):
38def get_ip_address_to_target_remote_host(host, port):
39    """
40    Function get_ip_address_to_target_remote_host(host, port)
41    """
42    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
43    try:
44        s.connect((host, port))
45        return s.getsockname()[0]
46    except Exception as e:
47        return None

Function get_ip_address_to_target_remote_host(host, port)

def can_listen_on_port(listen_ip, port):
50def can_listen_on_port(listen_ip, port):
51    """
52    Function can_listen_on_port(listen_ip, port)
53    """
54    try:
55        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
56        s.settimeout(0.05)
57        s.bind((listen_ip, port))
58        s.listen(5)
59        s.close()
60        return True
61    except OSError as e:
62        return False

Function can_listen_on_port(listen_ip, port)

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

Function get_ip_addr_to_listen_on(target, options)

def get_next_http_listener_port(current_value, listen_ip, options):
 88def get_next_http_listener_port(current_value, listen_ip, options):
 89    """
 90    Function get_next_http_listener_port(current_value, listen_ip, options)
 91    """
 92    port_window = (options.max_http_port - options.min_http_port)
 93
 94    if current_value > options.max_http_port:
 95        current_value = options.max_http_port
 96
 97    if current_value < options.min_http_port:
 98        current_value = options.min_http_port
 99
100    current_value = options.min_http_port + ((current_value + 1) % port_window)
101    while not can_listen_on_port(listen_ip, current_value):
102        current_value = options.min_http_port + ((current_value + 1) % port_window)
103
104    return current_value

Function get_next_http_listener_port(current_value, listen_ip, options)