fw.py 17.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
from firewall import models
import django.conf


import subprocess
import re
from datetime import datetime, timedelta
from django.db.models import Q


settings = django.conf.settings.FIREWALL_SETTINGS
12 13


14
class Firewall:
15
    IPV6 = False
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    RULES = None
    RULES_NAT = []
    vlans = None
    pub = None
    hosts = None
    fw = None

    def dportsport(self, rule, repl=True):
        retval = ' '
        if rule.proto == 'tcp' or rule.proto == 'udp':
            retval = '-p %s ' % rule.proto
            if rule.sport:
                retval += ' --sport %s ' % rule.sport
            if rule.dport:
                retval += ' --dport %s ' % (rule.nat_dport
31 32
                          if (repl and rule.nat and rule.direction == '1')
                          else rule.dport)
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
        elif rule.proto == 'icmp':
            retval = '-p %s ' % rule.proto
        return retval

    def iptables(self, s):
        """Append rule to filter table."""
        self.RULES.append(s)

    def iptablesnat(self, s):
        """Append rule to NAT table."""
        self.RULES_NAT.append(s)

    def host2vlan(self, host, rule):
        if not rule.foreign_network:
            return

        if self.IPV6 and host.ipv6:
            ipaddr = host.ipv6 + '/112'
        else:
            ipaddr = host.ipv4

        dport_sport = self.dportsport(rule)

        for vlan in rule.foreign_network.vlans.all():
            if rule.accept:
                if rule.direction == '0' and vlan.name == 'PUB':
                    if rule.dport == 25:
                        self.iptables('-A PUB_OUT -s %s %s -p tcp '
61 62
                                      '--dport 25 -j LOG_ACC' %
                                      (ipaddr, rule.extra))
63 64 65 66 67 68 69
                        break
                    action = 'PUB_OUT'
                else:
                    action = 'LOG_ACC'
            else:
                action = 'LOG_DROP'

70 71 72 73
            if rule.direction == '1':  # going TO host
                self.iptables('-A %s_%s -d %s %s %s -g %s' %
                              (vlan, host.vlan, ipaddr, dport_sport,
                               rule.extra, action))
74
            else:
75 76 77
                self.iptables('-A %s_%s -s %s %s %s -g %s' %
                              (host.vlan, vlan, ipaddr, dport_sport,
                               rule.extra, action))
78 79 80 81 82 83 84 85

    def fw2vlan(self, rule):
        if not rule.foreign_network:
            return

        dport_sport = self.dportsport(rule)

        for vlan in rule.foreign_network.vlans.all():
86
            if rule.direction == '1':  # going TO host
87
                self.iptables('-A INPUT -i %s %s %s -g %s' %
88 89
                              (vlan.interface, dport_sport, rule.extra,
                               'LOG_ACC' if rule.accept else 'LOG_DROP'))
90 91
            else:
                self.iptables('-A OUTPUT -o %s %s %s -g %s' %
92 93
                              (vlan.interface, dport_sport, rule.extra,
                               'LOG_ACC' if rule.accept else 'LOG_DROP'))
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    def vlan2vlan(self, l_vlan, rule):
        if not rule.foreign_network:
            return

        dport_sport = self.dportsport(rule)

        for vlan in rule.foreign_network.vlans.all():
            if rule.accept:
                if rule.direction == '0' and vlan.name == 'PUB':
                    action = 'PUB_OUT'
                else:
                    action = 'LOG_ACC'
            else:
                action = 'LOG_DROP'

110 111 112
            if rule.direction == '1':  # going TO host
                self.iptables('-A %s_%s %s %s -g %s' %
                              (vlan, l_vlan, dport_sport, rule.extra, action))
113 114
            else:
                self.iptables('-A %s_%s %s %s -g %s' % (l_vlan, vlan,
115 116
                                                        dport_sport,
                                                        rule.extra, action))
117 118 119 120 121 122 123 124 125 126 127 128 129

    def prerun(self):
        self.iptables('*filter')
        self.iptables(':INPUT DROP [88:6448]')
        self.iptables(':FORWARD DROP [0:0]')
        self.iptables(':OUTPUT DROP [50:6936]')

        # initialize logging
        self.iptables('-N LOG_DROP')
        # windows port scan are silently dropped
        self.iptables('-A LOG_DROP -p tcp --dport 445 -j DROP')
        self.iptables('-A LOG_DROP -p udp --dport 137 -j DROP')
        self.iptables('-A LOG_DROP -j LOG --log-level 7 '
130
                      '--log-prefix "[ipt][drop]"')
131 132 133
        self.iptables('-A LOG_DROP -j DROP')
        self.iptables('-N LOG_ACC')
        self.iptables('-A LOG_ACC -j LOG --log-level 7 '
134
                      '--log-prefix "[ipt][isok]"')
135 136 137 138
        self.iptables('-A LOG_ACC -j ACCEPT')

        self.iptables('-N PUB_OUT')

139 140
        self.iptables('-A FORWARD -m set --match-set blacklist src,dst '
                      '-j DROP')
141 142
        self.iptables('-A FORWARD -m state --state INVALID -g LOG_DROP')
        self.iptables('-A FORWARD -m state --state ESTABLISHED,RELATED '
143
                      '-j ACCEPT')
144
        self.iptables('-A FORWARD -p icmp --icmp-type echo-request '
145
                      '-g LOG_ACC')
146 147 148 149 150

        self.iptables('-A INPUT -m set --match-set blacklist src -j DROP')
        self.iptables('-A INPUT -m state --state INVALID -g LOG_DROP')
        self.iptables('-A INPUT -i lo -j ACCEPT')
        self.iptables('-A INPUT -m state --state ESTABLISHED,RELATED '
151
                      '-j ACCEPT')
152 153 154 155

        self.iptables('-A OUTPUT -m state --state INVALID -g LOG_DROP')
        self.iptables('-A OUTPUT -o lo -j ACCEPT')
        self.iptables('-A OUTPUT -m state --state ESTABLISHED,RELATED '
156
                      '-j ACCEPT')
157 158 159

    def postrun(self):
        self.iptables('-A PUB_OUT -s 152.66.243.160/27 -p tcp --dport 25 '
160
                      '-j LOG_ACC')
161
        self.iptables('-A PUB_OUT -s 152.66.243.160/27 -p tcp --dport 445 '
162
                      '-j LOG_ACC')
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        self.iptables('-A PUB_OUT -p tcp --dport 25 -j LOG_DROP')
        self.iptables('-A PUB_OUT -p tcp --dport 445 -j LOG_DROP')
        self.iptables('-A PUB_OUT -p udp --dport 445 -j LOG_DROP')

        self.iptables('-A PUB_OUT -g LOG_ACC')
        self.iptables('-A FORWARD -g LOG_DROP')
        self.iptables('-A INPUT -g LOG_DROP')
        self.iptables('-A OUTPUT -g LOG_DROP')
        self.iptables('COMMIT')

    def ipt_nat(self):
        self.iptablesnat('*nat')
        self.iptablesnat(':PREROUTING ACCEPT [0:0]')
        self.iptablesnat(':INPUT ACCEPT [0:0]')
        self.iptablesnat(':OUTPUT ACCEPT [1:708]')
        self.iptablesnat(':POSTROUTING ACCEPT [1:708]')

        # portforward
        for host in self.hosts.exclude(pub_ipv4=None):
            for rule in host.rules.filter(nat=True, direction='1'):
                dport_sport = self.dportsport(rule, False)
                if host.vlan.snat_ip:
                    self.iptablesnat('-A PREROUTING -d %s %s %s -j DNAT '
186 187 188
                                     '--to-destination %s:%s' %
                                     (host.pub_ipv4, dport_sport, rule.extra,
                                      host.ipv4, rule.nat_dport))
189 190 191 192 193

        # rules for machines with dedicated public IP
        for host in self.hosts.exclude(shared_ip=True):
            if host.pub_ipv4:
                self.iptablesnat('-A PREROUTING -d %s -j DNAT '
194 195
                                 '--to-destination %s' %
                                 (host.pub_ipv4, host.ipv4))
196
                self.iptablesnat('-A POSTROUTING -s %s -j SNAT '
197 198
                                 '--to-source %s' %
                                 (host.ipv4, host.pub_ipv4))
199 200 201 202 203 204

        # default NAT rules for VLANs
        for s_vlan in self.vlans:
            if s_vlan.snat_ip:
                for d_vlan in s_vlan.snat_to.all():
                    self.iptablesnat('-A POSTROUTING -s %s -o %s -j SNAT '
205
                                     '--to-source %s' %
206
                                     (str(s_vlan.network4), d_vlan.interface,
207
                                      s_vlan.snat_ip))
208 209 210

        # hard-wired rules
        self.iptablesnat('-A POSTROUTING -s 10.5.0.0/16 -o vlan0003 -j SNAT '
211 212 213 214 215 216
                         '--to-source 10.3.255.254')  # man elerheto legyen
        self.iptablesnat('-A POSTROUTING -o vlan0008 -j SNAT '
                         '--to-source 10.0.0.247')  # wolf network for printing
        self.iptablesnat('-A POSTROUTING -s 10.3.0.0/16 -p udp --dport 53 '
                         '-o vlan0002 -j SNAT ''--to-source %s' %
                         self.pub.ipv4)  # kulonben nem megy a dns man-ban
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

        self.iptablesnat('COMMIT')

    def ipt_filter(self):
        ipv4_re = re.compile('([0-9]{1,3}\.){3}[0-9]{1,3}')

        # pre-run stuff
        self.prerun()

        # firewall's own rules
        for f in self.fw:
            for rule in f.rules.all():
                self.fw2vlan(rule)

        # zonak kozotti lancokra ugras
        for s_vlan in self.vlans:
            for d_vlan in self.vlans:
                self.iptables('-N %s_%s' % (s_vlan, d_vlan))
                self.iptables('-A FORWARD -i %s -o %s -g %s_%s' %
236 237
                              (s_vlan.interface, d_vlan.interface, s_vlan,
                               d_vlan))
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

        # hosts' rules
        for i_vlan in self.vlans:
            for i_host in i_vlan.host_set.all():
                for group in i_host.groups.all():
                    for rule in group.rules.all():
                        self.host2vlan(i_host, rule)
                for rule in i_host.rules.all():
                    self.host2vlan(i_host, rule)

        # enable communication between VLANs
        for s_vlan in self.vlans:
            for rule in s_vlan.rules.all():
                self.vlan2vlan(s_vlan, rule)

        # zonak kozotti lancokat zarja le
        for s_vlan in self.vlans:
            for d_vlan in self.vlans:
                self.iptables('-A %s_%s -g LOG_DROP' % (s_vlan, d_vlan))

        # post-run stuff
        self.postrun()

        if self.IPV6:
            self.RULES = [x for x in self.RULES if not ipv4_re.search(x)]
            self.RULES = [x.replace('icmp', 'icmpv6') for x in self.RULES]

    def __init__(self, IPV6=False):
266 267
        self.RULES = []
        self.RULES_NAT = []
268 269 270 271 272 273 274 275 276 277 278 279
        self.IPV6 = IPV6
        self.vlans = models.Vlan.objects.all()
        self.hosts = models.Host.objects.all()
        self.pub = models.Vlan.objects.get(name='PUB')
        self.fw = models.Firewall.objects.all()
        self.ipt_filter()
        if not self.IPV6:
            self.ipt_nat()

    def reload(self):
        if self.IPV6:
            process = subprocess.Popen(['/usr/bin/ssh', 'fw2',
280 281 282
                                        '/usr/bin/sudo',
                                        '/sbin/ip6tables-restore', '-c'],
                                       shell=False, stdin=subprocess.PIPE)
283 284 285
            process.communicate('\n'.join(self.RULES) + '\n')
        else:
            process = subprocess.Popen(['/usr/bin/ssh', 'fw2',
286 287 288
                                        '/usr/bin/sudo',
                                        '/sbin/iptables-restore', '-c'],
                                       shell=False, stdin=subprocess.PIPE)
289
            process.communicate('\n'.join(self.RULES) + '\n' +
290
                                '\n'.join(self.RULES_NAT) + '\n')
291 292 293

    def get(self):
        if self.IPV6:
294
            return {'filter': self.RULES, }
295
        else:
296
            return {'filter': self.RULES, 'nat': self.RULES_NAT}
297 298 299 300 301 302

    def show(self):
        if self.IPV6:
            return '\n'.join(self.RULES) + '\n'
        else:
            return ('\n'.join(self.RULES) + '\n' +
303 304
                    '\n'.join(self.RULES_NAT) + '\n')

305 306

def ipset():
307 308 309 310
    week = datetime.now() - timedelta(days=2)
    filter_ban = (Q(type='tempban', modified_at__gte=week) |
                  Q(type='permban')).values('ipv4', 'reason')
    return models.Blacklist.objects.filter(filter_ban)
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326


def ipv6_to_octal(ipv6):
    while len(ipv6.split(':')) < 8:
        ipv6 = ipv6.replace('::', ':::')
    octets = []
    for part in ipv6.split(':'):
        if not part:
            octets.extend([0, 0])
        else:
            # Pad hex part to 4 digits.
            part = '%04x' % int(part, 16)
            octets.append(int(part[:2], 16))
            octets.append(int(part[2:], 16))
    return '\\' + '\\'.join(['%03o' % x for x in octets])

327

328 329 330 331
def ipv4_to_arpa(ipv4, cname=False):
    m2 = re.search(r'^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$', ipv4)
    if cname:
        return ('%s.dns1.%s.%s.%s.in-addr.arpa' %
332
                (m2.group(4), m2.group(3), m2.group(2), m2.group(1)))
333 334
    else:
        return ('%s.%s.%s.%s.in-addr.arpa' %
335 336
                (m2.group(4), m2.group(3), m2.group(2), m2.group(1)))

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

def ipv6_to_arpa(ipv6):
    while len(ipv6.split(':')) < 8:
        ipv6 = ipv6.replace('::', ':::')
    octets = []
    for part in ipv6.split(':'):
        if not part:
            octets.extend([0, 0, 0, 0])
        else:
            # Pad hex part to 4 digits.
            part = '%04x' % int(part, 16)
            octets.insert(0, int(part[0], 16))
            octets.insert(0, int(part[1], 16))
            octets.insert(0, int(part[2], 16))
            octets.insert(0, int(part[3], 16))
    return '.'.join(['%1x' % x for x in octets]) + '.ip6.arpa'


# =fqdn:ip:ttl          A, PTR
# &fqdn:ip:x:ttl        NS
# ZfqdnSOA
# +fqdn:ip:ttl          A
# ^                     PTR
# C                     CNAME
# :                     generic

def dns():
    vlans = models.Vlan.objects.all()
365
    # regex = re.compile(r'^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$')
366 367 368
    DNS = []

    for i_vlan in vlans:
369
        # m = regex.search(i_vlan.net4)
370 371 372 373 374 375 376
        rev = i_vlan.reverse_domain

        for i_host in i_vlan.host_set.all():
            ipv4 = (i_host.pub_ipv4 if i_host.pub_ipv4 and
                    not i_host.shared_ip else i_host.ipv4)
            i = ipv4.split('.', 4)
            reverse = (i_host.reverse if i_host.reverse and
377
                       len(i_host.reverse) else i_host.get_fqdn())
378 379 380 381

            # ipv4
            if i_host.ipv4:
                DNS.append("^%s:%s:%s" % (
382 383
                    (rev % {'a': int(i[0]), 'b': int(i[1]), 'c': int(i[2]),
                            'd': int(i[3])}),
384 385 386 387 388
                    reverse, models.settings['dns_ttl']))

            # ipv6
            if i_host.ipv6:
                DNS.append("^%s:%s:%s" % (ipv6_to_arpa(i_host.ipv6),
389
                                          reverse, models.settings['dns_ttl']))
390 391

    for domain in models.Domain.objects.all():
392 393 394
        DNS.append("Z%s:%s:support.ik.bme.hu::::::%s" %
                   (domain.name, settings['dns_hostname'],
                    models.settings['dns_ttl']))
395 396 397 398 399 400

    for r in models.Record.objects.all():
        d = r.get_data()
        if d['type'] == 'A':
            DNS.append("+%s:%s:%s" % (d['name'], d['address'], d['ttl']))
        elif d['type'] == 'AAAA':
401 402
            DNS.append(":%s:28:%s:%s" %
                       (d['name'], ipv6_to_octal(d['address']), d['ttl']))
403 404 405 406 407 408 409
        elif d['type'] == 'NS':
            DNS.append("&%s::%s:%s" % (d['name'], d['address'], d['ttl']))
        elif d['type'] == 'CNAME':
            DNS.append("C%s:%s:%s" % (d['name'], d['address'], d['ttl']))
        elif d['type'] == 'MX':
            mx = d['address'].split(':', 2)
            DNS.append("@%(fqdn)s::%(mx)s:%(dist)s:%(ttl)s" %
410 411
                       {'fqdn': d['name'], 'mx': mx[1], 'dist': mx[0],
                        'ttl': d['ttl']})
412 413 414 415 416
        elif d['type'] == 'PTR':
            DNS.append("^%s:%s:%s" % (d['name'], d['address'], d['ttl']))

    return DNS
    process = subprocess.Popen(['/usr/bin/ssh', 'tinydns@%s' %
417 418 419
                                settings['dns_hostname']],
                               shell=False, stdin=subprocess.PIPE)
    process.communicate("\n".join(DNS) + "\n")
420 421 422 423 424 425 426 427 428 429 430 431
    # print "\n".join(DNS)+"\n"


def prefix_to_mask(prefix):
    t = [0, 0, 0, 0]
    for i in range(0, 4):
        if prefix > i * 8 + 7:
            t[i] = 255
        elif i * 8 < prefix and prefix <= (i + 1) * 8:
            t[i] = 256 - (2 ** ((i + 1) * 8 - prefix))
    return ".".join([str(i) for i in t])

432

433 434 435
def dhcp():
    vlans = models.Vlan.objects.all()
    regex = re.compile(r'^([0-9]+)\.([0-9]+)\.[0-9]+\.[0-9]+\s+'
436
                       r'([0-9]+)\.([0-9]+)\.[0-9]+\.[0-9]+$')
437 438 439 440 441 442 443 444
    DHCP = []

# /tools/dhcp3/dhcpd.conf.generated

    for i_vlan in vlans:
        if(i_vlan.dhcp_pool):
            m = regex.search(i_vlan.dhcp_pool)
            if(m or i_vlan.dhcp_pool == "manual"):
445
                DHCP.append('''
446 447 448 449 450 451 452 453 454 455 456
    # %(name)s - %(interface)s
    subnet %(net)s netmask %(netmask)s {
      %(extra)s;
      option domain-name "%(domain)s";
      option routers %(router)s;
      option domain-name-servers %(dnsserver)s;
      option ntp-servers %(ntp)s;
      next-server %(tftp)s;
      authoritative;
      filename \"pxelinux.0\";
      allow bootp; allow booting;
457
    }''' % {
458 459 460 461 462 463 464
                    'net': i_vlan.net4,
                    'netmask': prefix_to_mask(i_vlan.prefix4),
                    'domain': i_vlan.domain,
                    'router': i_vlan.ipv4,
                    'ntp': i_vlan.ipv4,
                    'dnsserver': settings['rdns_ip'],
                    'extra': ("range %s" % i_vlan.dhcp_pool
465
                              if m else "deny unknown-clients"),
466 467 468 469 470 471
                    'interface': i_vlan.interface,
                    'name': i_vlan.name,
                    'tftp': i_vlan.ipv4
                })

                for i_host in i_vlan.host_set.all():
472
                    DHCP.append('''
473 474 475 476 477 478 479 480 481 482 483
                    host %(hostname)s {
                      hardware ethernet %(mac)s;
                      fixed-address %(ipv4)s;
                    }''' % {
                        'hostname': i_host.hostname,
                        'mac': i_host.mac,
                        'ipv4': i_host.ipv4,
                    })

    return DHCP
    process = subprocess.Popen(['/usr/bin/ssh', 'fw2',
484 485 486
                                'cat > /tools/dhcp3/dhcpd.conf.generated;'
                                'sudo /etc/init.d/isc-dhcp-server restart'],
                               shell=False, stdin=subprocess.PIPE)
487
#   print "\n".join(DHCP)+"\n"
488
    process.communicate("\n".join(DHCP) + "\n")