File size: 6,271 Bytes
2f3c093 | 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 | import logging
import subprocess
import random
import time
import uuid
import ipaddress
from typing import Dict, Any
import platform
class IdentityManager:
def __init__(self, logger: logging.Logger):
self.logger = logger
self.original_mac = None
self.original_ip = None
self.interface = self._get_default_interface()
def _get_default_interface(self) -> str:
"""Detects the default network interface."""
system = platform.system()
if system == "Linux":
try:
result = subprocess.run(["route", "-n"], capture_output=True, text=True, check=True)
for line in result.stdout.splitlines():
if "UG" in line and "0.0.0.0" in line:
return line.split()[-1]
except Exception as e:
self.logger.error(f"Error detecting default interface: {e}")
return "eth0" # Default to eth0 if detection fails
elif system == "Darwin":
try:
result = subprocess.run(["route", "-n", "get", "default"], capture_output=True, text=True, check=True)
for line in result.stdout.splitlines():
if "interface:" in line:
return line.split("interface:")[1].strip()
except Exception as e:
self.logger.error(f"Error detecting default interface: {e}")
return "en0" # Default to en0 if detection fails
else:
self.logger.warning(f"Unsupported OS: {system}. Defaulting to eth0.")
return "eth0"
return "eth0"
def get_current_mac(self, interface: str = None) -> str:
interface = interface or self.interface
try:
result = subprocess.run(["ifconfig", interface], capture_output=True, text=True, check=True)
for line in result.stdout.splitlines():
if "ether" in line or "lladdr" in line:
parts = line.split("ether" if "ether" in line else "lladdr")
if len(parts) > 1:
return parts[1].strip().split(" ")[0]
return None
except Exception as e:
self.logger.error(f"Error getting MAC address: {e}")
return None
def get_current_ip(self, interface: str = None) -> str:
interface = interface or self.interface
try:
result = subprocess.run(["ifconfig", interface], capture_output=True, text=True, check=True)
for line in result.stdout.splitlines():
if "inet " in line:
return line.split("inet ")[1].split(" ")[0]
return None
except Exception as e:
self.logger.error(f"Error getting IP address: {e}")
return None
def change_mac_address(self, new_mac: str, interface: str = None) -> bool:
interface = interface or self.interface
try:
self.original_mac = self.get_current_mac(interface)
subprocess.run(["ifconfig", interface, "down"], check=True)
subprocess.run(["ifconfig", interface, "hw", "ether", new_mac], check=True)
subprocess.run(["ifconfig", interface, "up"], check=True)
self.logger.info(f"MAC address changed to {new_mac} on {interface}")
return True
except Exception as e:
self.logger.error(f"Error changing MAC address: {e}")
return False
def restore_mac_address(self, interface: str = None) -> bool:
interface = interface or self.interface
if not self.original_mac:
self.logger.warning("Original MAC address not saved. Cannot restore.")
return False
try:
subprocess.run(["ifconfig", interface, "down"], check=True)
subprocess.run(["ifconfig", interface, "hw", "ether", self.original_mac], check=True)
subprocess.run(["ifconfig", interface, "up"], check=True)
self.logger.info(f"MAC address restored to {self.original_mac} on {interface}")
return True
except Exception as e:
self.logger.error(f"Error restoring MAC address: {e}")
return False
def change_ip_address(self, new_ip: str, interface: str = None) -> bool:
interface = interface or self.interface
try:
self.original_ip = self.get_current_ip(interface)
subprocess.run(["ifconfig", interface, new_ip], check=True)
self.logger.info(f"IP address changed to {new_ip} on {interface}")
return True
except Exception as e:
self.logger.error(f"Error changing IP address: {e}")
return False
def restore_ip_address(self, interface: str = None) -> bool:
interface = interface or self.interface
if not self.original_ip:
self.logger.warning("Original IP address not saved. Cannot restore.")
return False
try:
subprocess.run(["ifconfig", interface, self.original_ip], check=True)
self.logger.info(f"IP address restored to {self.original_ip} on {interface}")
return True
except Exception as e:
self.logger.error(f"Error restoring IP address: {e}")
return False
def start_tor_session(self):
self.original_mac = self.get_current_mac()
self.original_ip = self.get_current_ip()
new_mac = self._generate_random_mac()
new_ip = self._generate_random_ip()
self.change_mac_address(new_mac)
self.change_ip_address(new_ip)
self.logger.info("TOR session started with new identity.")
def stop_tor_session(self):
self.restore_mac_address()
self.restore_ip_address()
self.logger.info("TOR session stopped and original identity restored.")
def _generate_random_mac(self) -> str:
mac = [0x00, 0x16, 0x3e,
random.randint(0x00, 0x7f),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff)]
return ':'.join(map(lambda x: "%02x" % x, mac))
def _generate_random_ip(self) -> str:
ip = ipaddress.IPv4Address(random.randint(0x0B000000, 0xDF000000))
return str(ip)
|