コード例 #1
0
def main( rule , table, chain ):

    # Enable on boot
    data = ['#!/bin/sh' , \
            '/sbin/iptables-restore < /etc/iptables.rules']
    for value in data:
        cow('/etc/network/if-pre-up.d/iptables', value)
    command = rule.split(' ')
    command.insert(0,'iptables')
    command.insert(1,'-t')
    command.insert(2, table)
    # Apply rule
    print('-----------------------------------')
    print('RULE:')
    print(command)
    print('-----------------------------------')
    for path in execute(command):
        print(path, end='')

    # Making the rules permanent
    iptables_rules = open('/etc/iptables.rules', 'w')
    p = subprocess.Popen(["iptables-save"], stdout=iptables_rules)
    iptables_rules.close()

    # Present updated chain on CLI
    for path in execute([ 'iptables','-n','--line-numbers','-t', table, '-L', chain ]):
        print(path, end='')
    print('-----------------------------------')
コード例 #2
0
def main(interface, hostname, ip, mac):

    checkfile = 'include \"/etc/dhcpcd.d/static.leases.' + interface + '\";'
    cow('/etc/dhcpcd.conf', checkfile)

    static_leases = open('/etc/dhcpcd.d/static.leases.' + interface, 'a')
    data = [ 'host ' + hostname + '{' \
             ,'hardware ethernet ' + mac + ';' \
             , 'fixed-address ' + ip + ';' \
             , '}\n' ]
    static_leases.writelines('\n'.join(data))

    # Restart the dhcp server
    for path in execute(['/etc/init.d/isc-dhcp-server', 'restart']):
        print(path, end='')
コード例 #3
0
ファイル: upload_rules.py プロジェクト: dirac1/SecRouter
def main(filename):

    # Enable on boot
    data = ['#!/bin/sh' , \
            '/sbin/iptables-restore < /etc/iptables.rules']
    for value in data:
        cow('/etc/network/if-pre-up.d/iptables', value)

    # Flusing old rules
    for path in execute(["iptables", '-t', 'filter', '-F']):
        print(path, end="")
    for path in execute(["iptables", '-t', 'nat', '-F']):
        print(path, end="")

    shutil.copy2('/home/secrouter/tmp/' + filename, '/etc/iptables.rules')
    for path in execute(["sh", '/etc/network/if-pre-up.d/iptables']):
        print(path, end="")

    # Retrieving new rules from uploaded file
    #iptables_rules = open('/home/secrouter/tmp/' + filename, 'r')
    #for line in iptables_rules:
    #    print(line)
    #p = subprocess.Popen(["iptables-restore"], stdin=iptables_rules)
    #p = subprocess.Popen(["iptables-restore"])
    #p.stdin.readline(iptables_rules)
    #p.stdin.close()
    # Updating chains
    #iptables_rules.close()

    print('-------------FIREWALL--------------')
    print('-----------------------------------')
    print('NEW FILTER RULES:')
    for path in execute(["iptables", '-v', '-L', '-t', 'filter']):
        print(path, end="")
    print('-----------------------------------')
    print('NEW NAT RULES:')
    for path in execute(["iptables", '-v', '-L', '-t', 'nat']):
        print(path, end="")
    print('-----------------------------------')
コード例 #4
0
ファイル: dhcp_server.py プロジェクト: dirac1/SecRouter
def main(interface, network, prefix, gateway, Pool_Range_Start,
         Pool_Range_Stop, DNS_Server_1, DNS_Server_2, lease_time, Add_ARP):

    # transitive variable
    Pool_Range = []
    Pool_Range.append(Pool_Range_Start)
    Pool_Range.append(Pool_Range_Stop)
    DNS_Server = []
    DNS_Server.append(DNS_Server_1)
    DNS_Server.append(DNS_Server_2)
    networkprefix = network + '/' + prefix

    # -------------------------- ip calculations -----------------------------
    netmask = str(ipaddress.ip_network(networkprefix).netmask)

    broadcast = str(ipaddress.ip_network(networkprefix).broadcast_address)

    #------------------ writing on /etc/default/isc-dhcp-server -------------------------
    cow('/etc/default/isc-dhcp-server', 'INTERFACESv4=\"' + interface + '\"')

    # -------------------------- writing on /etc/dhcpcp.conf-----------------------------
    cow('/etc/dhcpcd.conf',
        'include \"/etc/dhcpcd.d/' + interface + '.conf\"' + ';')

    # ------------- checking and overwriting /etc/dhcpcd.d/ínterface.conf ----------
    dhcp_dir = os.listdir('/etc/dhcpcd.d/')
    for files in dhcp_dir:
        if files == interface + '.conf':  # check if the file exist in the directory and erase it
            print('/etc/dhcpcp.d/' + interface + '.conf exists')
            os.remove('/etc/dhcpcd.d/' + interface + '.conf')
        if files == interface + '.conf.disabled':  # check if the file exist in the directory and erase it
            print('/etc/dhcpcp.d/' + interface + '.conf.disabled exists')
            os.remove('/etc/dhcpcd.d/' + interface + '.conf.disabled')

    static_lease_dir = os.listdir('/etc/dhcpcd.d/')
    for files in static_lease_dir:
        if files == 'static.leases.' + interface:
            os.remove('/etc/dhcpcd.d/static.leases.' + interface)

    # ------------- writing the configuration file -------------
    dhcpd = open('/etc/dhcpcd.d/' + interface + '.conf', 'a')
    conf = [ 'subnet ' + network + ' netmask ' + netmask + ' ' + '{', \
             #'interface ' + interface + ';', \
             'authoritative;', \
             'range ' + Pool_Range[0] + ' ' + Pool_Range[1] + ';', \
             'option routers ' +  gateway + ';', \
             'option subnet-mask ' + netmask + ';', \
             'option broadcast-address ' +  broadcast + ';', \
             'option domain-name-servers ' + DNS_Server[0] + ',' + DNS_Server[1] + ';', \
             'max-lease-time ' + str(lease_time) + ';', \
             '} ' ]

    if Add_ARP == False:
        conf.pop(2)
        conf.insert(2, '#authoritative;')

    dhcpd.writelines('\n'.join(conf))
    dhcpd.close()

    # Restart the dhcp server to apply the changes
    for path in execute(["systemctl", "restart", "isc-dhcp-server"]):
        print(path, end='')
コード例 #5
0
def main(enable,conf_type,int_bridge,stp_mode,bridge_name,network,prefix,ip,gw):

    if network=='':
        print('manual/dhcp type')
    else:
        # -------------------------- ip calculations -----------------------------
        networkprefix = network+'/'+prefix
        netmask = str(ipaddress.ip_network(networkprefix).netmask)
        broadcast = str(ipaddress.ip_network(networkprefix).broadcast_address)

    file_dir = '/etc/network/bridge.d/'
    file_int = '/etc/network/bridge.d/'+bridge_name

    # ----------- removing white lines -------------
    rwl('/etc/network/bridge.d/',bridge_name)

    # ------------------------- configuration --------------------------------
    if enable=='1': # Enable the bridge 

        if conf_type=='1': # conf_type == manual 
           # --------------------------------------- 
            data = [ 'auto ' + bridge_name, \
                     'iface ' + bridge_name + ' inet manual', \
                     'pre-up ifconfig $IFACE up', \
                     'post-down ifconfig $IFACE down', \
                    'bridge_ports '+ int_bridge, \
                    'bridge_stp ' + stp_mode ]
            open(file_int, 'a').close()
            for value in data:
                cow(file_int,value)
        if conf_type=='2': # conf_type == static 
           # --------------------------------------- 
            data = [ 'auto ' + bridge_name, \
                     'iface ' + bridge_name + ' inet static', \
                     'address ' + ip, \
                     'netmask ' + netmask, \
                     'gateway ' + gw, \
                     'bridge_ports '+ int_bridge, \
                     'bridge_stp ' + stp_mode ]
            open(file_int, 'a').close()
            for value in data:
                cow(file_int,value)

        if conf_type=='3': # conf_type == dhcp 
           # --------------------------------------- 
            data = [ 'auto ' + bridge_name, \
                     'iface ' + bridge_name + ' inet dhcp', \
                     'bridge_ports '+ int_bridge, \
                     'bridge_stp ' + stp_mode ]
            open(file_int, 'a').close()
            for value in data:
                cow(file_int,value)

        for path in execute(['ip','link','add','name',bridge_name, 'type', 'bridge']):
            print(path,end=' ')
        for path in execute(['ip','link','set',int_bridge, 'master',bridge_name]):
            print(path,end=' ')
        for path in execute(['ifquery','-a']):
            print(path, end='')
        for path in execute(['ip','link','set','dev',bridge_name,'down']):
            print(path, end='')
        for path in execute(['ip','link','set','dev',bridge_name,'up']):
            print(path, end='')

    else: # Disable the bridge 

        if conf_type=='1': # conf_type == manual 
           # --------------------------------------- 
            data = [ 'auto ' + bridge_name, \
                     'iface ' + bridge_name + ' inet manual', \
                     'pre-up ifconfig $IFACE up', \
                     'post-down ifconfig $IFACE down', \
                     'bridge_ports '+ int_bridge, \
                     'bridge_stp ' + stp_mode ]
            open(file_int, 'a').close()
            for value in data:
                cor(file_int,value,'')
        if conf_type=='2': # conf_type == static 
           # --------------------------------------- 
            data = [ 'auto ' + bridge_name, \
                     'iface ' + bridge_name + ' inet static', \
                     'address ' + ip, \
                     'netmask ' + netmask, \
                     'gateway ' + gw, \
                     'bridge_ports '+ int_bridge, \
                     'bridge_stp ' + stp_mode ]
            open(file_int, 'a').close()
            for value in data:
                cor(file_int,value,'')

        if conf_type=='3': # conf_type == dhcp 
           # --------------------------------------- 
            data = [ 'auto ' + bridge_name, \
                     'iface ' + bridge_name + ' inet dhcp', \
                     'bridge_ports '+ int_bridge, \
                     'bridge_stp ' + stp_mode ]
            open(file_int, 'a').close()
            for value in data:
                cor(file_int,value,'')

        for path in execute(['ip','link','delete',bridge_name,'type','bridge']):
            print(path,end=' ')
        for path in execute(['ifquery','-a']):
            print(path, end='')
        for interface in int_bridge.split(' '):
            for path in execute(['ip','link','set','dev',interface,'down']):
                print(path, end='')
            for path in execute(['ip','link','set','dev',interface,'up']):
                print(path, end='')
コード例 #6
0
def main(enable,conf_type,vlan_raw_device,vlan_id,mtu,network,prefix,ip,gw):
    file_dir = '/etc/network/vlan.d/'
    file_int = '/etc/network/vlan.d/'+'vlan'+vlan_id

    if network=='':
        print('manual/dhcp type')
    else:
        # -------------------------- ip calculations -----------------------------
        networkprefix = network+'/'+prefix
        netmask = str(ipaddress.ip_network(networkprefix).netmask)
        broadcast = str(ipaddress.ip_network(networkprefix).broadcast_address)

    # ----------- removing white lines -------------
    rwl('/etc/network/vlan.d/','vlan'+vlan_id)

    # ------------------------- configuration --------------------------------
    if enable=='1': # Enable the vlan 

        if conf_type=='1': # conf_type == manual 
           # --------------------------------------- 
            data = [ 'auto ' + vlan_raw_device+'.'+vlan_id, \
                     'iface ' + vlan_raw_device+'.'+vlan_id + ' inet manual', \
                     'pre-up ifconfig $IFACE up', \
                     'post-down ifconfig $IFACE down', \
                     'vlan-raw-device '+ vlan_raw_device, ]
            open(file_int, 'a').close()
            for value in data:
                cow(file_int,value)

        if conf_type=='2': # conf_type == static 
           # --------------------------------------- 
            data = [ 'auto ' + vlan_raw_device+'.'+vlan_id, \
                     'iface ' + vlan_raw_device+'.'+vlan_id + ' inet static', \
                     'address ' + ip, \
                     'netmask ' + netmask, \
                     'gateway ' + gw ]
            open(file_int, 'a').close()
            for value in data:
                cow(file_int,value)

        if conf_type=='3': # conf_type == dhcp 
           # --------------------------------------- 
            data = [ 'auto ' + vlan_raw_device+'.'+vlan_id, \
                     'iface ' + vlan_raw_device+'.'+vlan_id + ' inet dhcp' ]
            open(file_int, 'a').close()
            for value in data:
                cow(file_int,value)

        for path in execute(['ip','link','add','link',vlan_raw_device,'name',vlan_raw_device+'.'+vlan_id , 'type', 'vlan','id',vlan_id]):
            print(path,end=' ')
        for path in execute(['ifquery','-a']):
            print(path, end='')
        for path in execute(['ip','link','set','dev',vlan_raw_device+'.'+vlan_id,'down']):
            print(path, end='')
        for path in execute(['ip','link','set','dev',vlan_raw_device+'.'+vlan_id,'up']):
            print(path, end='')

    else: # Disable the vlan 

        if conf_type=='1': # conf_type == manual 
           # --------------------------------------- 
            data = [ 'auto ' + vlan_raw_device+'.'+vlan_id, \
                     'iface ' + vlan_raw_device+'.'+vlan_id + ' inet manual', \
                     'pre-up ifconfig $IFACE up', \
                     'post-down ifconfig $IFACE down', \
                     'vlan-raw-device '+ vlan_raw_device, ]
            open(file_int, 'a').close()
            for value in data:
                cor(file_int,value,'')

        if conf_type=='2': # conf_type == static 
           # --------------------------------------- 
            data = [ 'auto ' + vlan_raw_device+'.'+vlan_id, \
                     'iface ' + vlan_raw_device+'.'+vlan_id + ' inet static', \
                     'address ' + ip, \
                     'netmask ' + netmask, \
                     'gateway ' + gw ]
            open(file_int, 'a').close()
            for value in data:
                cor(file_int,value,'')

        if conf_type=='3': # conf_type == dhcp 
           # --------------------------------------- 
            data = [ 'auto ' + vlan_raw_device+'.'+vlan_id, \
                     'iface ' + vlan_raw_device+'.'+vlan_id + ' inet dhcp' ]
            open(file_int, 'a').close()
            for value in data:
                cor(file_int,value,'')

        for path in execute(['ip','link','delete',vlan_raw_device+'.'+vlan_id]):
            print(path,end=' ')
        for path in execute(['ifquery','-a']):
            print(path, end='')
        for path in execute(['ip','link','set','dev',vlan_raw_device,'down']):
            print(path, end='')
        for path in execute(['ip','link','set','dev',vlan_raw_device,'up']):
            print(path, end='')
コード例 #7
0
ファイル: int_ipconf.py プロジェクト: dirac1/SecRouter
def main(enable, conf_type, interface, network, prefix, ip, gw):
    file_dir = '/etc/network/interfaces.d/'
    file_int = '/etc/network/interfaces.d/' + interface

    if network == '':
        print('manual/dhcp type')
    else:
        # -------------------------- ip calculations -----------------------------
        networkprefix = network + '/' + prefix
        netmask = str(ipaddress.ip_network(networkprefix).netmask)
        broadcast = str(ipaddress.ip_network(networkprefix).broadcast_address)

    # ----------- removing white lines -------------
    rwl('/etc/network/interfaces.d/', interface)

    # ------------------------- configuration --------------------------------
    if enable == '1':  # Enable the interface

        if conf_type == '1':  # conf_type == manual
            # ---------------------------------------
            data = [ 'auto ' + interface, \
                     'iface ' + interface + ' inet manual', \
                     'pre-up ifconfig $IFACE up', \
                     'post-down ifconfig $IFACE down' ]
            open(file_int, 'a').close()
            for value in data:
                cow(file_int, value)

        if conf_type == '2':  # conf_type == static
            # ---------------------------------------
            data = [ 'auto ' + interface, \
                     'iface ' + interface + ' inet static', \
                     'address ' + ip, \
                     'netmask ' + netmask, \
                     'gateway ' + gw ]
            open(file_int, 'a').close()
            for value in data:
                cow(file_int, value)

        if conf_type == '3':  # conf_type == dhcp
            # ---------------------------------------
            data = [ 'auto ' + interface, \
                     'allow-hotplug ' + interface, \
                     'iface ' + interface + ' inet dhcp', ]
            open(file_int, 'a').close()
            for value in data:
                cow(file_int, value)

        for path in execute(['ifquery', '-a']):
            print(path, end='')
        #for path in execute(['systemctl','restart','networking']):
        #    print(path, end='')
        for path in execute(['ip', 'link', 'set', 'dev', interface, 'down']):
            print(path, end='')
        for path in execute(['ip', 'link', 'set', 'dev', interface, 'up']):
            print(path, end='')
    else:  # Disable the interface

        if conf_type == '1':  # conf_type == manual
            # ---------------------------------------
            data = [ 'auto ' + interface, \
                     'iface ' + interface + ' inet manual', \
                     'pre-up ifconfig $IFACE up', \
                     'post-down ifconfig $IFACE down' ]
            open(file_int, 'a').close()
            for value in data:
                cor(file_int, value, '')

        if conf_type == '2':  # conf_type == static
            # ---------------------------------------
            data = [ 'auto ' + interface, \
                     'iface ' + interface + ' inet static', \
                     'address ' + ip, \
                     'netmask ' + netmask, \
                     'gateway ' + gw ]
            open(file_int, 'a').close()
            for value in data:
                cor(file_int, value, '')

        if conf_type == '3':  # conf_type == dhcp
            # ---------------------------------------
            data = [ 'auto ' + interface, \
                     'allow-hotplug ' + interface, \
                     'iface ' + interface + ' inet dhcp', ]
            open(file_int, 'a').close()
            for value in data:
                cor(file_int, value, '')
        for path in execute(['ifquery', '-a']):
            print(path, end='')
コード例 #8
0
def main(enable, conf_type, interface, dst_network, prefix, gw):
    print(" ### Starting configuration ### ")
    # -------------------------- ip calculations -----------------------------
    networkprefix = dst_network + '/' + prefix
    netmask = str(ipaddress.ip_network(networkprefix).netmask)
    broadcast = str(ipaddress.ip_network(networkprefix).broadcast_address)

    # ip route add [dst_network]+/+[prefix] via [gw] dev [interface]

    # ----------- remove white lines -------------
    rwl('/etc/network/interfaces.d/', interface)
    # ------------------------- configuration --------------------------------
    if enable == '1':  # Enable the route
        if conf_type == '1':  # conf_type == phy
            # ---------------------------------------
            file_int = '/etc/network/interfaces.d/' + interface
            # data = [ 'up route add -net '+ dst_network +' netmask ' + netmask +' gw ' + gw, \
            #          'down route del -net ' +  dst_network +' netmask '+ netmask + ' gw ' + gw ]
            data = [ 'post-up ip route add '+ dst_network+'/'+prefix+ ' via ' + gw +' dev ' + interface, \
                     'pre-up ip route add '+ dst_network+'/'+prefix+ ' via ' + gw +' dev ' + interface ]
            open(file_int, 'a').close()
            for value in data:
                cow(file_int, value)

        if conf_type == '2':  # conf_type == vlan
            # ---------------------------------------
            file_int = '/etc/network/vlan.d/' + interface
            # data = [ 'up route add -net '+ dst_network +' netmask ' + netmask +' gw ' + gw, \
            #          'down route del -net ' +  dst_network +' netmask '+ netmask + ' gw ' + gw ]
            data = [ 'post-up ip route add '+ dst_network+'/'+prefix+ ' via ' + gw +' dev ' + interface, \
                     'pre-up ip route add '+ dst_network+'/'+prefix+ ' via ' + gw +' dev ' + interface ]
            open(file_int, 'a').close()
            for value in data:
                cow(file_int, value)

        if conf_type == '3':  # conf_type == bridge
            # ---------------------------------------
            file_int = '/etc/network/bridge.d/' + interface
            # data = [ 'up route add -net '+ dst_network +' netmask ' + netmask +' gw ' + gw, \
            #          'down route del -net ' +  dst_network +' netmask '+ netmask + ' gw ' + gw ]
            data = [ 'post-up ip route add '+ dst_network+'/'+prefix+ ' via ' + gw +' dev ' + interface, \
                     'pre-up ip route add '+ dst_network+'/'+prefix+ ' via ' + gw +' dev ' + interface ]
            open(file_int, 'a').close()
            for value in data:
                cow(file_int, value)

        print(" ### Actual configuration ### ")
        for path in execute([
                'ip', 'route', 'add', dst_network + '/' + prefix, 'via', gw,
                'dev', interface
        ]):
            print(path, end='')
        for path in execute(['ifquery', '-a']):
            print(path, end='')

    else:  # Disable the route

        if conf_type == '1':  # conf_type == phy
            # ---------------------------------------
            file_int = '/etc/network/interfaces.d/' + interface
            data = [ 'post-up ip route add '+ dst_network+'/'+prefix+ ' via ' + gw +' dev ' + interface, \
                     'pre-up ip route add '+ dst_network+'/'+prefix+ ' via ' + gw +' dev ' + interface ]
            open(file_int, 'a').close()
            for value in data:
                cor(file_int, value, '')

        if conf_type == '2':  # conf_type == vlan
            # ---------------------------------------
            file_int = '/etc/network/vlan.d/' + interface
            data = [ 'post-up ip route add '+ dst_network+'/'+prefix+ ' via ' + gw +' dev ' + interface, \
                     'pre-up ip route add '+ dst_network+'/'+prefix+ ' via ' + gw +' dev ' + interface ]
            open(file_int, 'a').close()
            for value in data:
                cor(file_int, value, '')

        if conf_type == '3':  # conf_type == bridge
            # ---------------------------------------
            file_int = '/etc/network/bridge.d/' + interface
            data = [ 'post-up ip route add '+ dst_network+'/'+prefix+ ' via ' + gw +' dev ' + interface, \
                     'pre-up ip route add '+ dst_network+'/'+prefix+ ' via ' + gw +' dev ' + interface ]
            open(file_int, 'a').close()
            for value in data:
                cor(file_int, value, '')

        print(" ### Actual configuration ### ")
        for path in execute(
            ['ip', 'route', 'delete', dst_network + '/' + prefix]):
            print(path, end='')
        for path in execute(['ifquery', '-a']):
            print(path, end='')