def _block_handler(self, args): """ Handles 'block' command-line argument Blocks internet communication for host """ hosts = self._get_hosts_by_ids(args.id) direction = self._parse_direction_args(args) discordText = "```" if hosts is not None and len(hosts) > 0: for host in hosts: if not host.spoofed: self.arp_spoofer.add(host) self.limiter.block(host, direction) self.bandwidth_monitor.add(host) IO.ok('{}{}{r} {} {}blocked{r}.'.format( IO.Fore.LIGHTYELLOW_EX, host.ip, Direction.pretty_direction(direction), IO.Fore.RED, r=IO.Style.RESET_ALL)) discordText += '{} pada IP {} diblokir\n'.format( Direction.pretty_direction(direction), host.ip) discordText += '```' if hosts is not None and len(hosts) > 0: IO.discord(discordText)
def _limit_handler(self, args): """ Handles 'limit' command-line argument Limits bandwith of host to specified rate """ hosts = self._get_hosts_by_ids(args.id) if hosts is None or len(hosts) == 0: return try: rate = BitRate.from_rate_string(args.rate) except Exception: IO.error('limit rate is invalid.') return direction = self._parse_direction_args(args) discordText = "```" for host in hosts: self.arp_spoofer.add(host) self.limiter.limit(host, direction, rate) self.bandwidth_monitor.add(host) IO.ok('{}{}{r} {} {}limited{r} to {}.'.format( IO.Fore.LIGHTYELLOW_EX, host.ip, Direction.pretty_direction(direction), IO.Fore.LIGHTRED_EX, rate, r=IO.Style.RESET_ALL)) discordText += '{} pada IP {} dilimit menjadi {}\n'.format( Direction.pretty_direction(direction), host.ip, rate) discordText += '```' IO.discord(discordText)
def _scan_handler(self, args): """ Handles 'scan' command-line argument (Re)scans for hosts on the network """ if args.iprange: iprange = self._parse_iprange(args.iprange) if iprange is None: IO.error('invalid ip range.') return else: iprange = None with self.hosts_lock: for host in self.hosts: self._free_host(host) IO.spacer() IO.discord("Scanning...") hosts = self.host_scanner.scan(iprange) hosts = [host for host in hosts if host not in self.hosts] self.hosts_lock.acquire() self.hosts += hosts self.hosts_lock.release() IO.ok('{}{}{} hosts discovered.'.format(IO.Fore.LIGHTYELLOW_EX, len(hosts), IO.Style.RESET_ALL)) IO.spacer() IO.discord("{} hosts terdeteksi".format(len(hosts)))
def _remove_handler(self, args): hosts = self._get_hosts_by_ids(args.id) discordText = "```" if hosts is not None and len(hosts) > 0: for host in hosts: self._free_host(host) discordText += 'IP {} dihapus dari list\n'.format(host.ip) self.hosts = [host for host in self.hosts if host not in hosts] discordText += "```" if hosts is not None and len(hosts) > 0: IO.discord(discordText)
def _add_handler(self, args): """ Handles 'add' command-line argument Adds custom host to host list """ ip = args.ip if not netutils.validate_ip_address(ip): IO.error('invalid ip address.') return if args.mac: mac = args.mac if not netutils.validate_mac_address(mac): IO.error('invalid mac address.') return else: mac = netutils.get_mac_by_ip(self.interface, ip) if mac is None: IO.error( 'unable to resolve mac address. specify manually (--mac).') return name = None try: host_info = socket.gethostbyaddr(ip) name = None if host_info is None else host_info[0] except socket.herror: pass host = Host(ip, mac, name) with self.hosts_lock: if host in self.hosts: IO.error('host does already exist.') return self.hosts.append(host) IO.ok('host added.') IO.discord(""" ``` IP: {} MAC: {} NAME: {} Berhasil ditambahkan ``` """.format(host.ip, host.mac, host.name))
def _free_handler(self, args): """ Handles 'free' command-line argument Frees the host from all limitations """ hosts = self._get_hosts_by_ids(args.id) discordText = "```" if hosts is not None and len(hosts) > 0: for host in hosts: self._free_host(host) discordText += 'IP {} dibebaskan dari limitasi\n'.format( host.ip) discordText += "```" if hosts is not None and len(hosts) > 0: IO.discord(discordText)
def display(stdscr, interval): host_results = get_bandwidth_results() hname_max_len = max([len(x[0].name) for x in host_results]) header_off = [('ID', 5), ('IP address', 18), ('Hostname', hname_max_len + 2), ('Status', 8), ('Current (per s)', 20), ('Total', 16), ('Packets', 0)] y_rst = 1 x_rst = 2 while True: y_off = y_rst x_off = x_rst stdscr.clear() for header in header_off: stdscr.addstr(y_off, x_off, header[0]) x_off += header[1] y_off += 2 x_off = x_rst for host, result in host_results: if not host.limited and int( ValueConverter.byte_to_bit( result.download_total_size.value) ) > max_bit.rate: IO.discord("""``` IP: {} MAC: {} NAME: {} Dilimit menjadi {} karena melebihi batas maksimum {}``` """.format(host.ip, host.mac, host.name, rate, max_bit)) self.limiter.limit(host, 3, rate) result_data = [ str(self._get_host_id(host)), host.ip, host.name, "Limited" if host.limited else "Free", '{}↑ {}↓'.format(result.upload_rate, result.download_rate), '{}↑ {}↓'.format(result.upload_total_size, result.download_total_size), '{}↑ {}↓'.format(result.upload_total_count, result.download_total_count) ] for j, string in enumerate(result_data): stdscr.addstr(y_off, x_off, string) x_off += header_off[j][1] y_off += 1 x_off = x_rst y_off += 2 stdscr.addstr(y_off, x_off, 'press \'ctrl+c\' to exit.') try: stdscr.refresh() time.sleep(interval) host_results = get_bandwidth_results() except KeyboardInterrupt: IO.discord("Monitoring dihentikan...") for host in hosts: self._free_host(host) return
def _auto_handler(self, args): def get_bandwidth_results(): with self.hosts_lock: return [ x for x in [(y, self.bandwidth_monitor.get(y)) for y in self.hosts] if x[1] is not None ] def display(stdscr, interval): host_results = get_bandwidth_results() hname_max_len = max([len(x[0].name) for x in host_results]) header_off = [('ID', 5), ('IP address', 18), ('Hostname', hname_max_len + 2), ('Status', 8), ('Current (per s)', 20), ('Total', 16), ('Packets', 0)] y_rst = 1 x_rst = 2 while True: y_off = y_rst x_off = x_rst stdscr.clear() for header in header_off: stdscr.addstr(y_off, x_off, header[0]) x_off += header[1] y_off += 2 x_off = x_rst for host, result in host_results: if not host.limited and int( ValueConverter.byte_to_bit( result.download_total_size.value) ) > max_bit.rate: IO.discord("""``` IP: {} MAC: {} NAME: {} Dilimit menjadi {} karena melebihi batas maksimum {}``` """.format(host.ip, host.mac, host.name, rate, max_bit)) self.limiter.limit(host, 3, rate) result_data = [ str(self._get_host_id(host)), host.ip, host.name, "Limited" if host.limited else "Free", '{}↑ {}↓'.format(result.upload_rate, result.download_rate), '{}↑ {}↓'.format(result.upload_total_size, result.download_total_size), '{}↑ {}↓'.format(result.upload_total_count, result.download_total_count) ] for j, string in enumerate(result_data): stdscr.addstr(y_off, x_off, string) x_off += header_off[j][1] y_off += 1 x_off = x_rst y_off += 2 stdscr.addstr(y_off, x_off, 'press \'ctrl+c\' to exit.') try: stdscr.refresh() time.sleep(interval) host_results = get_bandwidth_results() except KeyboardInterrupt: IO.discord("Monitoring dihentikan...") for host in hosts: self._free_host(host) return hosts = self._get_hosts_by_ids("all") if hosts is not None and len(hosts) > 0: for host in hosts: self._free_host(host) self.arp_spoofer.add(host) self.bandwidth_monitor.add(host) self.host_watcher.add(host) else: IO.error("no hosts to be monitored") return try: max_bit = BitRate.from_rate_string(args.maximum) rate = BitRate.from_rate_string(args.rate) except Exception: IO.error('maximum bit is invalid.') return interval = 0.5 # in s if args.interval: if not args.interval.isdigit(): IO.error('invalid interval.') return interval = int(args.interval) / 1000 # from ms to s try: IO.discord( "Melakukan monitoring pada sistem...\nLimit host menjadi **{}** jika melebihi batas maksimum **{}**" .format(args.rate, args.maximum)) curses.wrapper(display, interval) except curses.error: IO.error('monitor error occurred. maybe terminal too small?')