Advanced search


In einer Etage ist per Medienkonverter TX/SX ein LAN versorgt, da der zugehörige Router im Rechenzentrum steht. Im Rechenzentrum soll nun ein neuere Server aufgestellt werden. Nichts leichter als das, denn der Medienkonverter im Rechenzentrum hat einen eingebauten 4-Port Hub, an dem neben den Router noch der Server gesteckt wird.

Anfangs funktioniert alles bestens, aber nach einer guten halben Stunde beschwert sich der Kunde, daß an einem Arbeitsplatz DHCP nicht mehr ginge, an einem anderen Platz geht dagegen alles problemlos. IPv6 über Autoconfig teilt aber an beiden Plätzen die Adressen zu.

Es stellte sich heraus, daß der 4-Port Hub des Konverters nach einer Weile aufhört Broadcast Pakete korrekt weiterzuleiten. Er transportiert Broadcasts nur noch von den Kupferports zum Glasport aber nicht mehr vom Glas zum Kupfer. Wenn man den Server abzieht, funktioniert alles wieder prächtig. Unicast ist nicht betroffen, das tut in alle gewünschten Richtungen.

Das beobachtete Phänomen ist nun leicht erklärt:

  • Der Router hat seine Multicasts für Autoconfig, die wie Broadcasts verteilt werden, in Richtung des Glasinterfaces durchbekommen. Also bekamen die Rechner IPv6 Adressen. Allerdings konnten sie damit nicht arbeiten, weil Neighbor Discovery (Multicast!) die MAC-Adresse des Routers nicht ermitteln konnte.
  • Der Rechner, der über DHCP keine Adresse bekam, war per "ipconfig /release" gesäubert und sendete also Broadcasts, die nicht ankamen.
  • Der Rechner, bei dem DHCP funktionierte, war bereits eine Lease zugeteilt und die Erneuerung der Lease erfolgt per Unicast, die problemlos ankommen.

Fazit: Diese Medienkonverter sind dafür gedacht, Clients anzuschließen, nicht auf der Server- oder Routerseite zu stehen.

Am 4. Oktober findet in Berlin eine öffentliche Anhörung zum Thema Netzneutralität statt, auf wir als Sachverständige (seitens der SPD Fraktion) geladen sind. Das Diskussionthema ist spannend, denn es geht im Endeffekt um die Frage, wofür man als Provider eigentlich bei einem Internetzugang kassiert.

Da das Thema Netzneutralität aktuell heiß diskutiert wird, findet gleich am darauffolgenden Samstag eine offene Diskussionveranstaltung in Berlin statt. Der Schwerpunkt liegt hier mehr auf der Frage, wofür man als Verbraucher bei einem Internetzugang eigentlich bezahlt.

An beiden Veranstaltungen kann man kostenfrei teilnehmen, von der Anhörung gibt es einen Live-Stream unter www.bundestag.de oder später die Aufzeichung.

Wer mit IPv6 Adressen zu tun hat, muß halt auch sich mit anderen über die benutzten Adressen unterhalten. Leider gibt es dazu aktuell keine gemeinsame Sprachreglung für die Adressbestandteile. Dies führt leicht zu Mißverständnissen und im schlimmsten Fall zu Fehlkonfigurationen.

IPv6 Adresse
2001 4bd8 0 666 248 54ff fe12 ee3f
RIPE IKS Kunde VLAN MAC Adresse der Netzkarte

Es eine Sammlung von Namensvorschlägen, die zu einem einheitlichen, verbindlichen Namen für die Teile der IP Adresse führen soll. Aktuell läuft eine Umfrage zur Beliebheit der Namensvorschläge. Es kristallisieren sich zwei Namen heraus:

  • Hextet als Bezeichner für 16 Bits
  • Quibble als Bezeicher für 4 Nibbles (Gruppe von 4 Bits)
ipv6-address-part-names

Nun bin ich schuld(tm). Ich habe fremde Systeme angegriffen. Dabei habe ich einfach nur meine Arbeit getan.

Stellt man einem DNS Server eine Anfrage, so beantwortet er diese. Das ist sein Job. Und DNS ist ein Dienst, der manchmal unter sehr hoher Last steht. Die Serversoftware ist also darauf optimiert, möglichst viele Anfragen beantworten zu können. DNS Anfragen und Antworten werden per UDP ausgetauscht, einem statuslosen Protokoll. Auf ein einzelenes Anfragpaket hin gibt es ein einzelnes Antwortpaket und dann ist für beide Seiten die Sache vergessen. Das geht rasend schnell.

Allerdings kann niemand prüfen, ob die Anfragen auch vom richtigen System kamen. Das einkommende Paket wird schlicht beantwortet und die Antwort geht an die (vorgebliche) Absender-IP-Adresse. Und die kann man fälschen (spoofen). Damit gehen die Antworten an eine anderes System.

Normalerweise ist eine solche Adressfälschung ohne größere Probleme, weil der Angreifer genauso viele Pakete aussenden muß, wie auch ankommen. Problematisch wird es, wenn die Antwort deutlich größer ist als die Anfrage. Dann gewinnt der Angreifer Bandbreite. Mit DNSSEC sieht das Zahlenverhältnis ungefähr so aus: Aus einer 43 Byte großen Anfrage enstand eine 2796 Byte große Antwort. Eine Versechzigfachung!

IP 217.17.207.133.53378 > 217.17.192.66.53:  51078+ [1au] ANY? donnerhacke.de. (43)
IP 217.17.192.66.53 > 217.17.207.133.53378:  51078* 14/0/11 NS avalon.iks-jena.de.,
 NS broceliande.iks-jena.de., MX mailv4.donnerhacke.de. 6, MX mailv6.donnerhacke.de. 4,
 SOA, Type46, Type46, Type46, Type47, Type46, Type46, Type46, Type48[|domain] (2796)

Der Effekt tritt auch bei anderen Diensten auf und ist wohlbekannt. Allerdings gibt es bisher keine gute Lösung. Jeder Ansatz zerstört das DNS irgendwie. Trotzdem mußte jetzt schnell eine Lösung her, denn es liegt eine Beschwerde aus Schweden vor, der Server hier würde die Schulen dort als Teil eine dDoS-Netzwerkes angreifen. Die paar Mbps waren mir gar nicht aufgefallen.

Zuerst habe ich mir die gerade aktuelle Version von BIND gezogen, und ganz normal in Betrieb genommen. Es gibt selbstverständlich erstmal keine Änderung am Verhalten. Des kommen weiterhin Tausende von "ANY"-Anfragen über UDP an die gehosten Zonen.

Glücklicherweise ist der Sourcecode an dieser Stelle für einen Quick-Hack ganz übersichtlich:

--- bin/named/query.c.ORIG      2012-09-10 18:34:44.000000000 +0200
+++ bin/named/query.c   2012-09-10 17:43:23.000000000 +0200
@@ -7326,6 +7326,10 @@
        if (dns_rdatatype_ismeta(qtype)) {
                switch (qtype) {
                case dns_rdatatype_any:
+                       if ((client->attributes & NS_CLIENTATTR_TCP) == 0) {
+                               query_error(client, DNS_R_REFUSED, __LINE__);
+                               return;
+                       }
                        break; /* Let query_find handle it. */
                case dns_rdatatype_ixfr:
                case dns_rdatatype_axfr:

Wenn also jemand nach "ANY" fragt und die Anfrage per UDP reinkommt, dann wird die Anfrage schlicht abgelehnt. Damit ist die Antwort kleiner als die Anfrage und der Angriff lohnt sich nicht mehr. Bis das aber die Angreifer merken, kann es dauern. Es stört nur etwas, daß überhaupt Antwortpakete rausgehen. Zum kommentarlosen Wegwerfen kann ich mich aktuell nicht überwinden.

Andererseits ist schon bekannt, welche Systeme damit Probleme haben werden. Die Software qmail stellt standardmäßig "ANY" Anfragen, um die Mailsserver zu bestimmen. Das wird nun nicht mehr klappen.

Eine korrekte Lösung kann nur sein, ein Dampening einzuführen. Anhand von Strafpunkten für jede Art von Anfrage und Antwort kann dann einfach das Antworten komplett eingestellt werden. Da Strafpunkte mit der Zeit verfallen, wird es dann automatisch irgendwann wieder gehen. Das wird eine spannende Aufgabe.

IPv6 bietet mit der dynamischen Adresszuteilung per SLAAC einen eleganten Weg, feste und temporäre IP Adressen an Hostsysteme zuzuweisen. Das hat Vor- und Nachteile. Gerade bei Umbauten im Netzwerk müssen spontan völlig unbeteiligte Systeme angefaßt werden. Aber wie minimiert man diese Aufwände?

Zunächst einmal verringert SLAAC, also StateLess Address AutoConfig, den Verwaltungsaufwand für den Netzwerker erheblich: Der Router verteilt ein Prefix, aus dem sich die Hosts im versorgten Netzwerk automatisch bedienen, solange das Prefix gültig ist. Soweit so einfach. Das ganze skaliert auch bestens, weil man mehrere Prefixe pro Router oder auch mehrere Router reinstellen kann und alles gleichzeitig funktioniert. Die Hosts lernen dann halt mehrere IP Adressen mit den dazugehörigen Routern. SLAAC ist damit der Mechanismus, mit dem man leicht und im laufenden Betrieb von einem Provider zum anderen wechseln kann, indem man in einer Übergangszeit beide gleichzeitig anschließt: Renumbering is easy.

Hosts leiten mit SLAAC mehrere IP Adressen aus dem Prefix ab: Zum einen eine statische IP, die i.d.R. aus der MAC Adresse oder einer anderen eindeutigen und dauerhaften Nummer gebildet wird, sowie einen Satz zufällig generierter IPs, die nur zeitweise genutzt werden (Privacy extensions).

Interessant für den Dauerbetrieb sind die festen IPs, die auf anderen Systemen hinterlegt werden: Im DNS und in Konfigurationdateien, Zugriffsbeschränkungen, Firewalls, Datenbanken etc. pp. Ändern sich diese Adressen ungeplant, ist der manuelle und zeitliche Aufwand bis zum reibungslosen Betrieb erheblich.

Linux und Window 2003 leiten die feste IPs von der MAC der jeweiligen Netzwerkkarte nach dem EUI-64 Schema ab. Ist zum Tausch von Technik gezwungen (z.B. durch Hardwareausfall), so bekommen diese Server neue IPs. Zur parallelen Inbetriebnahme eines neues Servers ist das ungemein nützlich: Die Technik wird einfach angesteckt und angeschaltet, schon ist das Gerät unter einer vorhersehbaren IP in einem beliebigen Netzsegment direkt erreichbar.

Erfolgt aber der Funktionsübergang von der alten zur neuen Hardware, ergibt sich ein Problem: Überall wo die alte Maschine mit ihrer IP bekannt war, muß die neue IP verbreitet werden. Eine Änderung im DNS scheint einfach, jedoch gibt es immer noch zuviel Software, die das Ergebnis einer DNS Auflösung nicht regelmäßig (TTL!) überprüfen. Firewalls wie iptables, Netzwerksoftware wie Sendmail und NTP, Datenbanken wie PostgreSQL müssen neu gestartet werden, um die Änderungen mitzubekommen – von den manuell eingetragenen IPs in verschiedenen Geräten und Scripten ganz zu schweigen. Wohl dem, der seine Konfigurationen zentral in Textform abgelegt hat: zgrep -Ri "alteip" /archiv/configs/ liefert zumindest Anhaltspunkte für die manuell anzufassenden Systeme. Aber auch zentrales Deployment und Konfigurationsmanagement generiert nach der Änderung der IP einen heftigen Schluckauf über die vielen betroffenen Systeme.

Um gegen Hardwaretausch besser gefeit zu sein, generiert Window 2008 die feste IP beim Einstellen in ein neues Netzwerk zufällig und speichert diese ab. Nun kann man am Server die Netzkarten auswechseln, ohne daß auf anderen Systemen etwas angefaßt werden muß: Der Hardwaretausch bleibt ein lokales Phänomen. Window 2008 macht die Netzwerkumgebung an der Adresse des versorgenden Routers fest: Ändert sich die default Route nicht, so behält Window auch für andere Prefixe den gleichen Suffix bei: Renumbering ist easy. Ändert sich aber die default Route, ändert Windows die festen IPs. Es genügt also, den Router zu tauschen, um spontanes Chaos für alle versorgten Windows Netze zu generieren. Zwar korrigieren sich die DNS Einträge von allein (wenn man das nicht verbietet), aber die anderen betroffenen Systeme sind ebenfalls überrascht.

Die Anforderungen an "feste IPv6 Adressen" sind also vielfältig und widersprüchlich:

  • Hardwaretausch soll nur lokale Aktionen erfordern.
  • Neue Hardware soll ohne großen Verwaltungsaufwand versorgt werden.
  • Neue Netze sollen ohne großen Verwaltungsaufwand einrichtbar sein.

Bei uns hat sich folgendes Schema als praktisch herausgestellt:

  • Router bekommen feste link-lokal Adressen: fe80::<gerät>:<interface>, wobei die Gerätenummer zentral in 0.8.e.f.ip6.arpa. als Wildcard verwaltet wird.
  • Da die Routingtabellen praktisch nur Link-Local Adressen enthalten (z.B. bei SLAAC und OSPF), entfällt die Notwendigkeit von offizellen IPv6 Adressen auf Transfernetzen und die Existenz der Reverse-DNS-Zone erweist sich als äußerst nützlich.
  • Bei den meisten Routern und Firewalls wird der Link-Local Teil auch für die offiziellen Adressen verwendet, womit ein konsistentes Erscheinungsbild beibehalten wird.
  • Für Server ist die Verwaltung in einer zentralen Geräteliste umständlich und unnötig. Da praktisch alle Systeme dual-stack laufen, existiert bereits eine IPv4 Adresse (öffentlich oder privat). Diese übernehmen wir in den lokalen Teil der IPv6 Adresse. Aus 192.168.240.12 wird 2001:db8:x:y:192:168:240:12. Die EUI-64 Beschränkungen für die höchstwertigen Bits existieren nicht, wenn der kennzeichnende "ff:fe" Teil fehlt.
  • Ein Windows System kann nur voll dynamisch oder voll statisch konfiguriert werden. Die IPv6 Adresse ergibt sich also aus der IPv4 und dem gültigen Netzprefix, während das default Gateway die schon bekannte Form fe80::<gerät>:<interface> hat.
  • Linux Systeme gestatten eine Mischkonfiguration. In /proc/sys/net/ipv6/conf/ kann man mit "echo 0 > autoconfig" die Adressgenerierung abschalten, während die Routen gelernt werden. Mit "echo 0 > accept_ra_defrtr" kann man das Lernen der default Route untersagen, aber die Adressbildung zulassen.

Beispielkonfig eines Linux Systems:

( cd /proc/sys/net/ipv6/conf
  # all future interfaces and all current interfaces (incl. the predefined ones)
  for i in default all eth*; do
    echo 0 > $i/accept_ra
    echo 0 > $i/forwarding
  done
)
... loading modules, creating bonding, vlans, etc. ...
( cd /proc/sys/net/ipv6/conf
  # automatic address in the managment network, but no routing
  echo 0 > mgmt/accept_ra_defrtr
  echo 1 > mgmt/accept_ra
  # fixed address in the public network, learn routers
  echo 0 > public/autoconf
  echo 1 > public/accept_ra
)
# ip -6 addr automatic dev mgmt
ip -6 addr add 2001:db8:42:23:203:0:113:131/64 dev public
# ip -6 route default via automatic dev public
ip -4 addr add 203.0.113.131/24 dev public

ip link set mgmt up
ip link set public up 
ip -4 route add default via 203.0.113.1

Zuerst natürlich die Defaults umstellen und dann die Interfaces gleichartig konfigurieren, anderenfalls handelt man sich eine schwer zu debuggende Racecondition ein. Unglücklicherweise überschreibt "all" nicht zwingend die Einstellung existierender Interfaces, deswegen werden diese extra benannt. Die Einrichtung neuer Interface im laufenden Betrieb generiert dank dieser Defaults keine Routingstörungen mehr.

Nachdem dann alle Interfaces existieren, werden nur die Ausgewählten mit IPv6 versorgt werden. Externe vorgegebene Angaben sind dabei nur auskommentiert angegeben. In dieser Konfiguration kann der Host in ein beliebiges Managment-Netz gestellt werden und hat dort immer eine durch die Hardware vorhersagbare Adresse aus dem jeweiligen Management-Netz. Die MAC Adresse der Hardware kann man im Notfall am Switchport ablesen, um sich einen Management-Zugriff zu verschaffen.

Die Routingtabelle schaut dann so aus:

2001:db8:42:23::/64 dev public  proto kernel  metric 256
2001:db8:42:f000::/64 dev mgmt  proto kernel  metric 256  expires 2592292sec
fe80::/64 dev public  proto kernel  metric 256
fe80::/64 dev mgmt  proto kernel  metric 256
ff00::/8 dev public  metric 256
ff00::/8 dev mgmt  metric 256
default via fe80::52:3167 dev public  proto kernel  metric 1024  expires 1647sec
unreachable default dev lo  proto kernel  metric -1  error -101 metric 10 255

Beispielkonfig eines Cisco Routers:

interface Loopback0
 ip address 192.0.2.56 255.255.255.255
 ipv6 address 2001:db8:42::56:0/128
!
interface FastEthernet1/0
 description Management IPv6 only
 ipv6 address FE80::56:1000 link-local
 ipv6 address 2001:db8:42:f000::56:1000/64
!
interface GigabitEthernet2/0.75
 description Core
 encapsulation dot1Q 75
 ip address 198.51.100.28 255.255.255.248
 ipv6 address FE80::56:2075 link-local
 ipv6 unnumbered Loopback0
 ipv6 ospf 1 area 0
!
router ospf 1
  network 198.51.100.24 255.255.255.248 area 0
!
ipv6 router ospf 1
!

Interessant ist am Cisco Beispiel der Verzicht auf Transfernetze im Core und die deutliche Vereinfachung der OSPF Konfiguration gegenüber der legacy Version.

Ein Kunde beschwert sich darüber, daß an einer Dose im Raum die Webseiten zwei Wochen älter wären als an der anderen Dose. Beide Dosen sind identisch beschaltet und liegen im gleichen VLAN. Was geht da vor?

Der erste Verdacht, jemand habe sich an den Patchungen auf der Etage zu schaffen gemacht, ist unzutreffend.

An den betroffenen Systemen springt mir ein nslookup mit Server 192.168.0.1 ins Auge. Diese IP gibt es in diesem Kundennetz nicht. Jedenfalls nicht von mir. Auf die Frage nach Fremdtechnik zeigt man mir einen selbst beschafften WLAN Router, den man an eine der beiden Dosen anschlossen hat. Warum auch nicht?

World_IPv6_launch_logo_128

Schon ahnend, was geschehen ist, bitte ich die Konfig des WLAN Routers so zu verändern, daß er die Finger von IP und DHCP läßt. Anschließend möge man das Kabel von der Wanddose an den Router doch in die blauen LAN Dosem stecken, nicht in die gelbe WAN Dose. Damit ist es eine WLAN Bridge, die sich nicht in Dinge einmischt, von denen sie nichts versteht (vor allem eben IPv6).

Die Webseite selbst zeigt tatsächlich je nach Anschluß (am LAN-Port des WLAN-Routers oder an der Wanddose) unterschiedliche Webseiteninhalte. Da kann man Rechner und Browser wechseln wie man will. Selbst Cache Löschen und Rebooten bringen keinerlei Effekt.

Ein DNS Lookup liefert zwei IPs: IPv4 und IPv6. Die Nagelprobe zeigt, was los ist:

$ telnet -4 www.***.de 80
Connected to ***.servers.jiffybox.net.
GET / HTTP/1.0
Host: www.***.de

 

HTTP/1.1 200 OK
Date: Fri, 14 Sep 2012 11:29:52 GMT
Server: Mongrel 1.1.5
Status: 200 OK
ETag: "8e...ff"
X-Runtime: 250ms
Content-Type: text/html; charset=utf-8
Content-Length: 924
Cache-Control: private, max-age=0, must-revalidate
Set-Cookie: _newapp_session=B...0; path=/; HttpOnly
Vary: Accept-Encoding
Connection: close
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>

Und die andere IP:

$ telnet -6 www.***.de 80
Connected to ***.webpack.hosteurope.de.
GET / HTTP/1.0
Host: www.***.de
 
HTTP/1.1 200 OK
Date: Fri, 14 Sep 2012 11:23:03 GMT
Server: Apache
Last-Modified: Mon, 15 Jun 2009 07:31:26 GMT
ETag: "8f...80"
Accept-Ranges: bytes
Content-Length: 3711
Connection: close
Content-Type: text/html
 
<html>
  <head>

Da keiner der Beteiligen von IPv6 wußte, liegt der Verdacht nahe, daß im Zuge des World IPv6 Launch Days der AAAA Record hinzugekommen ist. Als man dann die Webpräsens auf einen anderen Server verschob, hat man nur den A Record geändert.

Somebody is misusing my servers for DDoS as described in DNSSEC Amplification Attack. Neither the protocol nor the implemetation offers any kind of protection. Both try hard to provide the highest possible (attack) performance. I'll try to defend my systems anyway.

DNS amplification attacks are a well known problem. The attacker sends UDP queries with spoofed adresses to the DNS server. And the server responds with much larger responses to the vicitim. Many hundered Mbps may be generated with ease.

Known solutions

Server operators are helpless. They have only a few options:

  • They can stop offering DNS services. That means, that they do not longer provide their own zones.
  • They can limit the bandwith of the servers. The vast majority of the traffic will still be attack traffic. All packets are dropped with a similar probabiltiy. So this approach will mainly drop the productive packets, which are not resent as often as the attack ones.
  • They can rate limit the queries per client. Unfortunly this generates only a constant data stream of attack packets. DDoS works well with limited data rates per server, if you misuse enough servers. On the other hand the implementation required a lot of ressources.
  • They can respond to the most commom attack queries (ANY over UDP) with REFUSED or TRUNCATED. Those responses are smaller than the queries. So the bandwith benefit for the attacker is negative. Sadly the attacker is not interested in an efficient amplification. The remaining bandwith is still large enough to establish a problem.

All those solutions did not convince me.

Practical experience

In order to understand the problem better, please have a look at real world data. As first approach the interface of the server is switched to 100Mbps and becomes quickly saturated by eight Mbps of attack bandwith. The amplification effect is impressive. Oviously this is not a usable solution.

dnssec-attack-bandwith

Next try reducing the size of the responses using REFUSED. Bandwith drops instantaneously after switching the software. However the vicitims keep complaining. In this mode this server is able to saturate a typical 2Mbps business line. On the other hand queries other than ANY can be observed, which exploit similar amplification effects. This is the path to the next arms race.

dnssec-attack-refused

I'd like to have a self-learning system, which silently drops "bad" queries. It should provide auto-recovery, in order to be run without permanent supervision or manual adjustment. And it should not harm ordinary users.

Dampening—the theory

I remembered BGP Route Dampening, which seems to match my requirements. Wrong defaults discredited this proposal. I was warned and promised to check all my assumptions with real wolrd data.

The basic idea is to collect penalty points per IP (or network) depending on the type of the query and the size of the response. Those penalty points should decay over time. Applying a hysteresis to the penalty will start dampening at a high level and stop at a muich lower value. During dampening no processing (besides calculating penalty points) should occur. In this state all packets should be dropped silently, even if packets are malformed.

Ciscos implementation tastes badly, because they require a regular scheduled full table scan to time out the values. I'd like to have an algorithm with constant or at least logarithmic timeing. Decaying table entries should be handled by the way.

The average case should be fast when accessing the data. And the typical case is dominated by attacks. Therefore the table entries should be searched in the order of probabiltiy.

My prefered data stucture is a heap. Using linear storage, the heap does not require explicit meta information. All the structure information is derivated from the numeric properties of the array indicies. The high penalty values are stored in the front of the heap, so linear searching the heap arrary will work fine in the avarage case.

Instead of aging each of the heap entries regularly, I try to recalculate only the current and the first element. In order to delay the calculation, each entry requires a last access time field. The current penalty is calculated using the old penalty value and the time difference. Recalculation is only needed after serveral seconds, not every time.

Stable software is designed around deterministic ressouce usage. That's why the heap should have a fixed size. To keep it small, all entries which drop below a certain limit should be removed as soon as possible. In the case of a full heap new entries can simply overwrite the last old one.

In order to age all entries, it should be sufficient to decay the first entry with the largest penalty. Over the time the decay will press it downtree. In theory this will cause the whole tree to be processed.

Software safety urges me to consider integer overflows: Penalty points bumps againt an upper bound.

Dampening—the reality

Compile time only configuration let me start quickly with conservative defaults:

  • 1 point per query
  • Activate dampening at 40000 points
  • Deactivate dampening at 1000 points
  • Dropping from table at 100 points

The very first try was a desaster: Software crashed in the first few seconds without any useful output. The reason was as simple as embarrassing: I forgot to allocate the heap. While fixit it, I wrapped most functions with asserts.

And the next try was really promising.

dnssec-attack-dampening1

The old software (reponding with REFUSED) runs up to 02:00. While chaning the software, the output data rate drops naturally. The drastic increase is caused by the learning time, where all packets are processed. 19 seconds later, the first attacker was catched. Those are 40000 queries!

02:10:30.212 general: notice: running
02:10:49.136 client: warning: 99.71.68.170 enters dampening.
02:11:07.477 client: warning: 108.248.25.44 enters dampening.
02:11:27.321 client: warning: 173.218.224.224 enters dampening.
02:11:58.131 client: warning: 217.23.3.214 enters dampening.

But a few minutes later the server crashed again:

02:15:51.732 critical: dampening.c:282: fatal error:
02:15:51.732 critical: RUNTIME_CHECK(b > 0 && b <= dampening_last) failed
02:15:51.732 critical: exiting (due to fatal error in library)

This bug is located in the heap manipulation functions. I rewrote "heap_down" (thanks to Knuth, TAoCP) and removed 80% of the code.

Next try was delayed up to 13:30 and became a complete flop: Traffic increases without limit. The reason is, that the attackers IPs fight about the last entry in the heap. They override each other, so none of them gained enough points to be punished.

13:22:31.985 client: info: 61.247.0.2 added to dampening with 1 (replacing 212.49.98.14 with 31)

The simple solution is to not override the last entry, if it has a higher penalty. I log an error message, if the last entry has a large penalty values. In this case the table is fully booked with attacker IPs.

Those changes bring the system up. After a learning phase the output rate drops to two Mbps since 14:30. But about an hour later, most attacks come through again.

15:29:59.321 client: error: Dampening table to small! Entry 213.186.33.20 with 12175 at the end.

WTF? I do not see that many attackers in the parallel sniffing. I observe some occasional spikes with some new addresses. This bug hides somewhere else. My assumption, the table will be aged step by step, seems to be wrong. How? The most prominent entries do not stop attacking when I stop sending responses. They collect penalty points faster that the decay can remove.

Only logging the situation is a really bad idea. This GB of logs was unnecessary. If the table is full and the server stops with a fatal error, all I can to is to restart the server. That's why it's enough to log it once and clear the tables to allow a new learning phase.

In order to enable real aging, I recalculate three entries: The first, the last, and a random one. There is no need for perfect randomness, it is sufficient to hit the table at different points. Shortly after 17:00 the change is ready for deployment. But about an hour later the system crashes again!

17:55:51.045 general: critical: dampening.c:283: fatal error:
17:55:51.045 general: critical: RUNTIME_CHECK(b > 0 && b <= dampening_last) failed
17:55:51.045 general: critical: exiting (due to fatal error in library

There is no bug at this point. The system can't crash here. What happened?

The only possible explanation is, that the heap was modified while being processed. But bind runs as a single process, does it? No, it does not. Bind is multithreaded. Introducing a simple static counter at the API calls reveals the awful truth:

general: critical: RUNTIME_CHECK(duplicatecheck == 0) failed

After rewriting the whole API code to insert a central LOCK about the heap code, the system is working for the first time!

dnssec-attack-dampening2

I dramatically shortened the learning period by applying a new penalty table:

  • 10 points for an unknown client
  • 100 point for an ANY query, otherwise 1 point per query
  • If a query is repeated with the same ID, 100 additional points per repeat count are applied. So the first retry gains 100 points, the second gains 200, and further on. This escalates quickly.
  • Depending on the response size further penalties add to the entry. Responses up to 500 bytes gain one to five, up to 2500 byte 10 to 50, and larger packets 100 or 200 points.

Using this configuration attacks are detected within the first 40 packets. After two hours of running the situation consolidates to:

  5 minute  input rate  381000 bits/sec,   23 packets/sec
  5 minute output rate 7275000 bits/sec, 9479 packets/sec 

Out of 10000 queries only 30 are permitted and processed.

The Patch

This patch exists only for historical purpose. Please refer to a more usable version.

Update
diff -rbTuN bind-9.9.1-P3/bin/named/client.c bind-9.9.1-P3-dampening/bin/named/client.c
--- bind-9.9.1-P3/bin/named/client.c    2012-08-24 06:43:09.000000000 +0200
+++ bind-9.9.1-P3-dampening/bin/named/client.c  2012-09-23 22:05:38.000000000 +0200
@@ -55,6 +55,8 @@
        #include <named/server.h>
        #include <named/update.h>

+       #include "dampening.h"
+
        /***
         *** Client
         ***/
@@ -877,6 +879,8 @@
                        goto done;
                }

+               dampening_by_size(client, mr->length);
+
                result = client_allocsendbuf(client, &buffer, NULL, mr->length,
                                             sendbuf, &data);
                if (result != ISC_R_SUCCESS)
diff -rbTuN bind-9.9.1-P3/bin/named/dampening.c bind-9.9.1-P3-dampening/bin/named/dampening.c
--- bind-9.9.1-P3/bin/named/dampening.c 1970-01-01 01:00:00.000000000 +0100
+++ bind-9.9.1-P3-dampening/bin/named/dampening.c       2012-09-23 22:34:37.000000000 +0200
@@ -0,0 +1,415 @@
+       /*
+        * Dampening
+        * =========
+        *
+        * Purpose of dampening is to sort out misbehaving clients in order to
+        * serve the legitemate clients and protect the innocent vitims of a
+        * DNS(SEC) amplification attack caused by spoofed UDP queries.
+        *
+        * Overview
+        * ~~~~~~~~
+        *
+        * Each querier IP gets a additive penalty depening on the type of the
+        * query, the size of the answer, and other parameters.
+        *
+        * If the penalty reaches a limit, the state of the IP switches to a
+        * "dampening". In this state the server drops every query from this IP
+        * without further processing (besides adding penalty points).
+        *
+        * The penalty is decreased expotentially over time. So if no further points
+        * are added fast enough, the total penalty will drop below a secondary limit.
+        * Then the state of the IP is changed back to "normal".
+        *
+        * Implementation
+        * ~~~~~~~~~~~~~~
+        *
+        * DNS servers to not track their clients. So adding state to querier IPs
+        * requires a new storage model. Because access to this storage is needed
+        * by processing each query, the storage must be in memory only. In order
+        * to prevent ressouce exhaustion, the used memory is fixed.
+        *
+        * The assumed access pattern is that high penalty clients will be cause
+        * most of the queries. The used data structure is therefore a heap.
+        *
+        * The following operations are implemented:
+        *
+        *  a) Searching an IP by travering the heap array from front (high values)
+        *     to the end (beware of a partially filled heap).
+        *
+        *  b) Adding a new IP (if not found) by overwriting the very last entry
+        *     of the heap, if the heap is full. This will cause one of the low
+        *     penality entries to be lost.
+        *
+        *  c) If a searched IP was found, the last updated timestamp is used to
+        *     recalculate the penality value (expotential decrease plus new
+        *     points), update the state, and rebalanced the heap for this modified
+        *     entry only. If the penality value falls below a certain limit, the
+        *     entry is removed from the heap.
+        *
+        *  d) The top, last, and a random node are recalculated and rebalanced
+        *     after each update or insertion, if the nodes were not updated
+        *     recently.
+        *
+        * Using this approach, the operations on the heap will be O(log n) on
+        * each access. There is no need for a regular maintainence activity.
+        *
+        * TODO
+        * ~~~~
+        *
+        *  - All hardcoded parameters need to be (re)configurable.
+        *  - Allow ACL dependant penalty parameters.
+        *  - Check long time stability.
+        *  - Check, if the assumptions are really true.
+        *
+        */
+
+       #include "dampening.h"
+       #include <isc/util.h>
+       #include <stdlib.h>
+       #include <math.h>
+       #include <named/globals.h>
+       #include <named/log.h>
+
+       #include <isc/netaddr.h>
+       #include <isc/stdtime.h>
+
+       typedef unsigned int dampening_penalty_t;
+       typedef size_t dampening_position_t;
+       #define DAMPENING_NOTHING 0
+
+       struct {
+          struct {
+             dampening_penalty_t top, damp, norm, drop;
+          } limit;
+          size_t heapsize;
+          int updatedelay;
+          int halflife;
+       } dampening_configuration = {
+            { 60000, 40000, 1000, 100 },      /* limits */
+            5000,                             /* entries in the heap */
+            5,                                /* delay expire by seconds */
+            600                               /* half-live of penalty in seconds */
+       };
+
+       typedef struct dampening_entry {
+          isc_netaddr_t netaddr;
+          isc_stdtime_t last_updated;
+          dns_messageid_t last_id;
+          size_t last_id_count;
+          unsigned int penalty;
+          dampening_state_t state;
+       } dampening_entry_t;
+
+       static dampening_entry_t * dampening_heap = NULL;
+       static dampening_position_t dampening_last = 0;
+       static isc_mutex_t dampening_lock;
+
+       /*
+        * Helper functions
+        */
+       void dampening_init(void);
+       dampening_position_t dampening_search(const isc_netaddr_t * netaddr);
+       void dampening_add(const isc_netaddr_t * netaddr, dampening_penalty_t points, isc_stdtime_t now);
+       void dampening_update(dampening_position_t entry, dampening_penalty_t points, isc_stdtime_t now);
+       void dampening_update1(dampening_position_t entry, dampening_penalty_t points, isc_stdtime_t now);
+       dampening_state_t dampening_get_state(dampening_position_t entry);
+       void dampening_expire(isc_stdtime_t now);
+       void dampening_heap_up(dampening_position_t entry);
+       void dampening_heap_down(dampening_position_t entry);
+       void dampening_heap_swap(dampening_position_t a, dampening_position_t b);
+       int dampening_below_top(dampening_position_t entry);
+
+       /*
+        * Main API function
+        */
+       dampening_state_t dampening_query(ns_client_t * client) {
+          isc_stdtime_t now;
+          isc_netaddr_t addr;
+          dampening_state_t state;
+          dampening_position_t entry;
+
+          RUNTIME_CHECK(client != NULL);
+          LOCK(&dampening_lock);
+
+          if(dampening_heap == NULL)
+            dampening_init();                 /* Autoinit */
+
+          isc_stdtime_get(&now);
+          isc_netaddr_fromsockaddr(&addr, &client->peeraddr);
+
+          entry = dampening_search(&addr);
+          if(entry == DAMPENING_NOTHING) {
+             dampening_add(&addr, 10, now);
+             state = DAMPENING_NORMAL;
+          } else {
+             state = dampening_get_state(entry);
+          }
+
+          UNLOCK(&dampening_lock);
+          return state;
+       }
+
+       void dampening_by_qtype(ns_client_t * client, dns_rdatatype_t qtype) {
+          isc_stdtime_t now;
+          isc_netaddr_t addr;
+          dampening_position_t entry;
+          dampening_penalty_t points;
+
+          RUNTIME_CHECK(client != NULL);
+          INSIST(dampening_heap != NULL);
+
+          LOCK(&dampening_lock);
+          isc_stdtime_get(&now);
+          isc_netaddr_fromsockaddr(&addr, &client->peeraddr);
+
+          entry = dampening_search(&addr);
+          if(entry != DAMPENING_NOTHING) {
+             switch(qtype) {
+              case dns_rdatatype_any: points = 100; break;
+              default               : points =   1; break;
+             }
+
+             if(dampening_heap[entry].last_id == client->message->id) {
+                points += (dampening_heap[entry].last_id_count++)*100;
+             } else {
+                dampening_heap[entry].last_id = client->message->id;
+                dampening_heap[entry].last_id_count = 1;
+             }
+
+             dampening_update(entry, points, now);
+          }
+
+          UNLOCK(&dampening_lock);
+       }
+
+       void dampening_by_size(ns_client_t * client, size_t length) {
+          isc_stdtime_t now;
+          isc_netaddr_t addr;
+          dampening_position_t entry;
+          dampening_penalty_t points;
+
+          RUNTIME_CHECK(client != NULL);
+          INSIST(dampening_heap != NULL);
+
+          LOCK(&dampening_lock);
+          isc_stdtime_get(&now);
+          isc_netaddr_fromsockaddr(&addr, &client->peeraddr);
+
+          entry = dampening_search(&addr);
+          if(entry != DAMPENING_NOTHING) {
+             if(length <= 100)  points =   1; else
+             if(length <= 200)  points =   2; else
+             if(length <= 300)  points =   3; else
+             if(length <= 400)  points =   4; else
+             if(length <= 500)  points =   5; else
+             if(length <= 700)  points =  10; else
+             if(length <= 1000) points =  20; else
+             if(length <= 1500) points =  30; else
+             if(length <= 2000) points =  40; else
+             if(length <= 2500) points =  50; else
+             if(length <= 5000) points = 100; else
+                                points = 200;
+             dampening_update(entry, points, now);
+          }
+
+          UNLOCK(&dampening_lock);
+       }
+
+       /*
+        * Set up the heap: allocate the memory range. Change of size is possibe,
+        * so let dampening_last untouched.
+        */
+       void dampening_init(void) {
+          dampening_heap = realloc(dampening_heap, (1+dampening_configuration.heapsize) * sizeof(dampening_entry_t));
+          if(dampening_heap == NULL)
+            UNEXPECTED_ERROR(__FILE__, __LINE__, "realloc() failed");
+       }
+
+       /*
+        * Return the current state.
+        */
+       dampening_state_t dampening_get_state(dampening_position_t entry) {
+          REQUIRE(dampening_heap != NULL);
+          RUNTIME_CHECK(entry > 0 && entry <= dampening_last);
+
+          return dampening_heap[entry].state;
+       }
+
+       /*
+        * Simple search though the heap, exploiting the feature, that the heap
+        * is mapped to a continiously filled, linear array. High penalty values
+        * occur first, so they are searched first.
+        * Returns DAMPENING_NOTHING if no match.
+        */
+       dampening_position_t dampening_search(const isc_netaddr_t * netaddr) {
+          dampening_position_t i;
+
+          REQUIRE(dampening_heap != NULL);
+          for(i = 1; i <= dampening_last; i++) {
+             if(ISC_TRUE == isc_netaddr_equal(&dampening_heap[i].netaddr, netaddr))
+               return i;
+          }
+          return DAMPENING_NOTHING;
+       }
+
+
+       /*
+        * Add an element to the end (eventually overwrite the last one)
+        */
+       void dampening_add(const isc_netaddr_t * netaddr, dampening_penalty_t points, isc_stdtime_t now) {
+          REQUIRE(dampening_heap != NULL);
+
+          if(dampening_last < dampening_configuration.heapsize) {
+             dampening_last++;
+          } else {
+             if(dampening_heap[dampening_last].penalty > points) {
+                if(dampening_heap[dampening_last].penalty > dampening_configuration.limit.drop)
+                  isc_log_write(ns_g_lctx, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_QUERY, ISC_LOG_ERROR,
+                                "Dampening table overflow! Restart dampening.");
+                dampening_last = 1;
+                return;
+             }
+          }
+
+          memcpy(&dampening_heap[dampening_last].netaddr, netaddr, sizeof(isc_netaddr_t));
+          dampening_heap[dampening_last].last_updated = now;
+          dampening_heap[dampening_last].penalty = 0;
+          dampening_heap[dampening_last].last_id = 0;
+          dampening_heap[dampening_last].last_id_count = 0;
+
+          dampening_update(dampening_last, points, now);
+       }
+
+       /*
+        * Update an existing entry with new points
+        */
+       void dampening_update(dampening_position_t entry, dampening_penalty_t points, isc_stdtime_t now) {
+          dampening_update1(entry, points, now);
+          dampening_expire(now);
+       }
+
+       void dampening_update1(dampening_position_t entry, dampening_penalty_t points, isc_stdtime_t now) {
+          int timediff;
+          char onbuf[ISC_NETADDR_FORMATSIZE];
+
+          REQUIRE(dampening_heap != NULL);
+          RUNTIME_CHECK(entry > 0 && entry <= dampening_last);
+
+          timediff = now - dampening_heap[entry].last_updated;
+          if(timediff > dampening_configuration.updatedelay) {
+             float penalty = dampening_heap[entry].penalty;
+             penalty *= exp(-(0.693*timediff)/dampening_configuration.halflife);
+             dampening_heap[entry].penalty = penalty;
+             dampening_heap[entry].last_updated = now;
+          }
+          if(dampening_heap[entry].penalty >= dampening_configuration.limit.top - points)
+            dampening_heap[entry].penalty = dampening_configuration.limit.top;
+          else
+            dampening_heap[entry].penalty += points;
+
+          if(dampening_heap[entry].state == DAMPENING_SUPPRESS &&
+             dampening_heap[entry].penalty < dampening_configuration.limit.norm) {
+
+             isc_netaddr_format(&dampening_heap[entry].netaddr, onbuf, sizeof(onbuf));
+             isc_log_write(ns_g_lctx, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_QUERY, ISC_LOG_WARNING,
+                           "%s dampening removed.", onbuf);
+             dampening_heap[entry].state = DAMPENING_NORMAL;
+          }
+
+          if(dampening_heap[entry].state == DAMPENING_NORMAL &&
+             dampening_heap[entry].penalty > dampening_configuration.limit.damp) {
+
+             isc_netaddr_format(&dampening_heap[entry].netaddr, onbuf, sizeof(onbuf));
+             isc_log_write(ns_g_lctx, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_QUERY, ISC_LOG_WARNING,
+                           "%s dampening activated.", onbuf);
+             dampening_heap[entry].state = DAMPENING_SUPPRESS;
+          }
+
+          if(dampening_heap[entry].penalty < dampening_configuration.limit.drop &&
+             timediff > dampening_configuration.updatedelay) {
+             if(entry < dampening_last) {
+                dampening_heap_swap(entry, dampening_last);
+             } else {
+                entry = 0;
+             }
+             dampening_last--;
+          }
+
+          if(entry > 0) {
+             if(dampening_below_top(entry))
+               dampening_heap_down(entry);
+             else
+               dampening_heap_up(entry);
+          }
+       }
+
+       /*
+        * Expire penalties
+        */
+       void dampening_expire(isc_stdtime_t now) {
+          dampening_penalty_t poi[3] = { 1, dampening_last, 1+(random()%dampening_last) };
+          unsigned int i;
+
+          for(i=0; i<sizeof(poi)/sizeof(*poi); i++) {
+
+             if(0 < poi[i] && poi[i] <= dampening_last &&
+                now > dampening_heap[poi[i]].last_updated + dampening_configuration.updatedelay)
+               dampening_update1(poi[i], 0, now);
+          }
+       }
+
+       /*
+        * Heap operations
+        */
+       void dampening_heap_up(dampening_position_t entry) {
+          dampening_position_t e;
+
+          RUNTIME_CHECK(entry > 0 && entry <= dampening_last);
+
+          for(e = entry/2;
+              e > 0;
+              entry = e, e = entry/2) {
+             if(dampening_heap[entry].penalty <= dampening_heap[e].penalty)
+               break;
+             dampening_heap_swap(entry, e);
+          }
+       }
+
+       void dampening_heap_down(dampening_position_t entry) {
+          dampening_position_t e;
+
+          RUNTIME_CHECK(entry > 0 && entry <= dampening_last);
+
+          for(e = 2*entry;
+              e <= dampening_last;
+              entry = e, e = 2*entry) {
+
+             if(e+1 <= dampening_last &&
+                dampening_heap[e].penalty <= dampening_heap[e+1].penalty)
+               e = e+1;
+
+             if(dampening_heap[e].penalty <= dampening_heap[entry].penalty)
+               break;
+
+             dampening_heap_swap(entry, e);
+          }
+       }
+
+       void dampening_heap_swap(dampening_position_t a, dampening_position_t b) {
+          dampening_entry_t temp;
+
+          RUNTIME_CHECK(a > 0 && a <= dampening_last);
+          RUNTIME_CHECK(b > 0 && b <= dampening_last);
+          RUNTIME_CHECK(a != b);
+
+          memcpy(&temp,              &dampening_heap[a], sizeof(dampening_entry_t));
+          memcpy(&dampening_heap[a], &dampening_heap[b], sizeof(dampening_entry_t));
+          memcpy(&dampening_heap[b], &temp,              sizeof(dampening_entry_t));
+       }
+
+       int dampening_below_top(dampening_position_t entry) {
+          RUNTIME_CHECK(entry > 0 && entry <= dampening_last);
+          return entry <= 1 ||
+            (dampening_heap[entry].penalty <= dampening_heap[entry/2].penalty);
+       }
+
diff -rbTuN bind-9.9.1-P3/bin/named/dampening.h bind-9.9.1-P3-dampening/bin/named/dampening.h
--- bind-9.9.1-P3/bin/named/dampening.h 1970-01-01 01:00:00.000000000 +0100
+++ bind-9.9.1-P3-dampening/bin/named/dampening.h       2012-09-23 21:37:44.000000000 +0200
@@ -0,0 +1,14 @@
+       #ifndef _DAMPENING_H
+       #define _DAMPENING_H
+
+       #include <named/client.h>
+
+       typedef enum dampenting_state {
+          DAMPENING_NORMAL, DAMPENING_SUPPRESS
+       } dampening_state_t;
+
+       dampening_state_t dampening_query(ns_client_t * client);
+       void dampening_by_qtype(ns_client_t * client, dns_rdatatype_t qtype);
+       void dampening_by_size(ns_client_t * client, size_t length);
+
+       #endif /* _DAMPENING_H */
diff -rbTuN bind-9.9.1-P3/bin/named/Makefile.in bind-9.9.1-P3-dampening/bin/named/Makefile.in
--- bind-9.9.1-P3/bin/named/Makefile.in 2012-08-24 06:43:09.000000000 +0200
+++ bind-9.9.1-P3-dampening/bin/named/Makefile.in       2012-09-19 15:55:39.000000000 +0200
@@ -87,6 +87,7 @@
                        zoneconf.@O@ \
                        lwaddr.@O@ lwresd.@O@ lwdclient.@O@ lwderror.@O@ lwdgabn.@O@ \
                        lwdgnba.@O@ lwdgrbn.@O@ lwdnoop.@O@ lwsearch.@O@ \
+                       dampening.@O@ \
                        ${DLZDRIVER_OBJS} ${DBDRIVER_OBJS}

        UOBJS =         unix/os.@O@ unix/dlz_dlopen_driver.@O@
@@ -101,6 +102,7 @@
                        zoneconf.c \
                        lwaddr.c lwresd.c lwdclient.c lwderror.c lwdgabn.c \
                        lwdgnba.c lwdgrbn.c lwdnoop.c lwsearch.c \
+                       dampening.c \
                        ${DLZDRIVER_SRCS} ${DBDRIVER_SRCS}

        MANPAGES =      named.8 lwresd.8 named.conf.5
diff -rbTuN bind-9.9.1-P3/bin/named/query.c bind-9.9.1-P3-dampening/bin/named/query.c
--- bind-9.9.1-P3/bin/named/query.c     2012-08-24 06:43:09.000000000 +0200
+++ bind-9.9.1-P3-dampening/bin/named/query.c   2012-09-23 21:50:22.000000000 +0200
@@ -61,6 +61,8 @@
        #include <named/sortlist.h>
        #include <named/xfrout.h>

+       #include "dampening.h"
+
        #if 0
        /*
         * It has been recommended that DNS64 be changed to return excluded
@@ -7282,6 +7284,14 @@
                }

                /*
+                * Update the penalty and report the current state
+                */
+               if (dampening_query(client) == DAMPENING_SUPPRESS) {
+                       query_next(client, DNS_R_DROP);
+                       return;
+               }
+
+               /*
                 * Get the question name.
                 */
                result = dns_message_firstname(message, DNS_SECTION_QUESTION);
@@ -7323,6 +7333,7 @@
                INSIST(rdataset != NULL);
                qtype = rdataset->type;
                dns_rdatatypestats_increment(ns_g_server->rcvquerystats, qtype);
+               dampening_by_qtype(client, qtype);
                if (dns_rdatatype_ismeta(qtype)) {
                        switch (qtype) {
                        case dns_rdatatype_any:

If the patch allows runtime configuration, there will be a download button.

Horrible, horrible

Publishing source code is the only way to let somebody find the most horrible bug. Did nobody else notice this copy and wast desaster? Data is destroyed, the heap is corrupted!

+          memcpy(&temp,              &dampening_heap[a], sizeof(dampening_entry_t));
+          memcpy(&dampening_heap[a], &dampening_heap[a], sizeof(dampening_entry_t));
+          memcpy(&dampening_heap[b], &temp,              sizeof(dampening_entry_t));  

Let's take this straight: t=a; a=a; b=t; How should this ever work?

+          memcpy(&temp,              &dampening_heap[a], sizeof(dampening_entry_t));
+          memcpy(&dampening_heap[a], &dampening_heap[b], sizeof(dampening_entry_t));
+          memcpy(&dampening_heap[b], &temp,              sizeof(dampening_entry_t));

 That's correct! And the bug is already fixed in the code above. Many thanks to the employee of Suse, who dropped me a note.

And finally an other braindead bug: Calculation of decay misses a minus in the exponent. The penalty is increased expotentially over time. The patch above is already fixed, but the history of this bug will be presented soon.

Writing software for DNS Dampening moves only step by step. Testing in production is the only way to determine if the assumptions made correct code.

Release often, release early

The first unforeseen results of publishing my patch are:

  • I was pointed to several fundamental errors in my assumptions. I do understand those concerns and agree with many of them Please allow me to gain my own experience.
  • I was pointed to an awful error in the heap manipulation functions. It saved me days to nail down this error.
  • Kind encouragement (even in this early stage) helped me to continue with this work.
  • I learned more about internal processes in our own company, than I ever wished to know.

Thank you very much.

If I had tried to delay the publication, I'd choosen other ways and refused to throw away my code that easily. Probably I had given up.

First results

After deploying the patch to the involved server everything was sound. Outgoing traffic decreased, I was happy.

Some strange lines scrolled through the logs: The IPv6 enabled resolvers of DTAG, HE, and SiXXS went into dampening. But I did not found detailed information. That's why I extended the patch to some logging:

--- bind-9.9.1-P3/bin/named/query.c     2012-08-24 06:43:09.000000000 +0200
+++ bind-9.9.1-P3-dampening/bin/named/query.c   2012-09-25 22:37:26.000000000 +0
@@ -7146,7 +7148,7 @@
        }

        static inline void
-       log_query(ns_client_t *client, unsigned int flags, unsigned int extflags) {
+       log_query(ns_client_t *client, unsigned int flags, unsigned int extflags, unsigned int penalty) {
                char namebuf[DNS_NAME_FORMATSIZE];
                char typename[DNS_RDATATYPE_FORMATSIZE];
                char classname[DNS_RDATACLASS_FORMATSIZE];
@@ -7165,7 +7167,7 @@
                isc_netaddr_format(&client->destaddr, onbuf, sizeof(onbuf));

                ns_client_log(client, NS_LOGCATEGORY_QUERIES, NS_LOGMODULE_QUERY,
-                             level, "query: %s %s %s %s%s%s%s%s%s (%s)", namebuf,
+                             level, "query: %s %s %s %s%s%s%s%s%s (%s) %u", namebuf,
                              classname, typename, WANTRECURSION(client) ? "+" : "-",
                              (client->signer != NULL) ? "S": "",
                              (client->opt != NULL) ? "E" : "",
@@ -7173,7 +7175,7 @@
                                         "T" : "",
                              ((extflags & DNS_MESSAGEEXTFLAG_DO) != 0) ? "D" : "",
                              ((flags & DNS_MESSAGEFLAG_CD) != 0) ? "C" : "",
-                             onbuf);
+                             onbuf, penalty);
        }

        static inline void
@@ -7228,6 +7230,7 @@
                unsigned int saved_extflags = client->extflags;
                unsigned int saved_flags = client->message->flags;
                isc_boolean_t want_ad;
+               unsigned int penalty;

                CTRACE("ns_query_start");

@@ -7282,6 +7285,14 @@
                }

                /*
+                * Update the penalty and report the current state
+                */
+               if (dampening_query(client, &penalty) == DAMPENING_SUPPRESS) {
+                       query_next(client, DNS_R_DROP);
+                       return;
+               }
+
+               /*
                 * Get the question name.
                 */
                result = dns_message_firstname(message, DNS_SECTION_QUESTION);
@@ -7306,7 +7317,7 @@
                }

                if (ns_g_server->log_queries)
-                       log_query(client, saved_flags, saved_extflags);
+                       log_query(client, saved_flags, saved_extflags, penalty);

                /*
                 * Check for multiple question queries, since edns1 is dead.

Storing the value in dampening_query is a no-brainer.

But the system appears to respond unusually slowly. Bash prompts with the hostname, so let's debug this: strace -f hostname -f. There are heavy timeouts while name resolution by localhost. So let me extract the penalty over time, sorry Kris.

penalty3

Let's inspect the spikes in the evening:

  • At 22:57 I tried a tcpdump with activated name resolution in order to verify the basic DNS functionality.
  • The wide bars at 22:05 and 23:43 are brute force attacks via SSH. SSH checks reverse and forward DNS for each connection.
  • At midnight the system storm into the dampening.

Expanding the bar between 23:02:50 and 23:07:00 reveals the glory details:

penalty3-1

Penalty rises to about 40 points and drops to zero repeatedly. Dampening works. But it looks a bit inverted. Overwriting the data in the last element might be another explanation for this graph. But overall, it's fine for me.

Deeper inspection of the midnight even reveals a buch of scripts which cross check the consistency of DNS entries: Does foreward and reverse lookup correspond to each other, is the name server listed in the parent zone, etc. pp.? Yesm those tests will raise 40000 queries in a few minutes. That's a real problem.

The most prominent solution is to exclude the locally connected networks from dampening. If you really have a spoofing problem with those addresses, you are not better than any of the attackers exploiting missing implementations of BCP38.

A completely different client reached over thousend pealty points within 20 minutes only by sending a few queries. It took a long time starring on the graph, until I was able to draw the enlightening two lines. m(

penalty4

Instead of decaying the points (following the green line), the penalty increases expotentially. Really, the code misses the important minus in the exponent. What a braindead error!

On the other hand, the error had it's nuts. I was able to test the system under conditions of very heavy attackers from all sides. Even then a large part of relevant traffic was processed correctly. Only the fixed ressouce allocation prevents the system from exploding.

Bugfix in production

The fixed system respondes smoothly. And first statistics are promising:

penalty5

After the first learning dampening and relearning causes waves. Variations in attack rates let the waves disappear.

There is a difference between accumulating by address and classifying by query type. The question is if and how normal queries are suppressed by ANY-type attacks. So let's have a lock at penalty by time for different query types.

penalty6-1

None of the regular queries comes from a dampened address! Attackers did not destroy production. For reference, let's include the attacker packers:

penalty6

Attacks are identified within 40 packets and stopped with silence. The horizontal lines are characteristic for massive repeated ANY query with the same ID. But the output rate is now production ready. Welcome back, server.

Real world statistics about query types could be derived easily.

 165976 A
  56926 ANY
  46890 AAAA
  36527 PTR
  11336 MX
   8025 SOA
   4958 DNSKEY
   2974 NS
   2786 SRV
   2293 TXT
    587 A6
    568 SPF
    543 DLV
    170 DS
    105 CNAME
      4 HINFO
      4 AXFR
      3 TLSA
      2 NAPTR
      2 RRSIG
      1 NSEC

I'm thankful for the TLSA queries. Not so funny is any query for A6. DLV queries are quite usual, I'm running such a list. But the ratio of IPv6 to IPv4 is outstanding. A quarter of the clients out there interesting in our services have enough IPv6 connectivity to ask for AAAA.

Open problems

The system is usable at a very basic level. Next steps include:

  • Can I do better than the simple heap? It might be better to use a circular buffer for age management and a hash table for searching?
  • Add configuration, ACLs etc.
  • What can I do, to migrate an attack to the well known resolver IP of a major ISP? Are our zones unreachable by the customers of this ISP?
  • How does my ideas compare with others? What are the pros and the cons?

Die Anzeige der IP-Adresse des Besuchers ist eigentlich eine Trivialität. Schließlich besteht während des Seitenabrufs eine Verbindung mit genau der IP. Interessanter ist es, parallel auch legacy IP Adressen und DNSSEC Eigenschaften zu ermitteln. Und dann soll das Ganze noch aus CMS generierten Seiten, d.h. gecachtem Content heraus funktionieren.

Die Abfrage der aktuell abfragenden IP Adresse ist mit serverseitigen Scriptsprachen eine Standardaufgabe.

<div id='your-ip'>
  <?= htmlspecialchars($_SERVER['<span class="term">REMOTE_ADDR</span>']) ?>
</div>

Schwierig wird es, wenn die Webseiten nicht ständig serverseitig bearbeitet werden, sondern aus einem Cache kommen. Dies kann der Cache eines CMS oder der eines Proxies sein. Dann ist die Scriptbearbeitung schon lange abgeschlossen, die IP steht als normaler Text drin und ist somit in der Regel falsch. Es ist unerwünscht, nur wegen dieser Anzeige, die sinnvollen Cache-Funktionen zu deaktivieren.

Stattdessen lagert man den Code der IP Ermittlung aus in ein minimales serverseitiges Script, daß nur die IP Adresse ausgibt. Und dieses Script muß vom Webbrowser clientseitig nachgeladen werden. Dazu dient Javascript. Das Nachladen von Inhalten ist unter dem Schlagwort Ajax bekannt.

<div id='your-ip'>
  <script type='text/javascript' src='http://lutz.donnerhacke.de/check-ipv6-dnssec.js'>
  </script>
</div>

Worauf kann nun dieses Script selbst zurückgreifen? Normalerweise würde man für Ajax ein Framework wie jQuery nehmen. Dies ist aber hier nicht möglich. Das einbettende System kann unbekannt sein (bei Fremdeinbindung) oder ungeeignete Frameworks schon benutzen, mit denen sich das eigene Framework beissen könnte.

Es bleibt nur, Ajax von Hand zu machen. Glücklicherweise brauche ich hier keine Rücksicht auf seltsame und alte Browser zu nehmen, ich kann mich an den Standard halten. Wenn also der Browser diesen Standard unterstützt, wird das HTML Stück mit der IP-Adresse nachgeladen:

if (window.XMLHttpRequest) {
  var xmlhttp = new XMLHttpRequest();

  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4) {
      var mydiv = document.getElementById("your-ip");
      mydiv.innerHTML = xmlhttp.responseText;
    }
  };
  xmlhttp.open("GET","/cgi-bin/check-ipv6-dnssec.pl",true);
  xmlhttp.send(null);
}

Der serverseitige Code muß mit einem relativen Pfad geladen werden, dies verlangen die Sicherheitseinstellungen der gängigen Browser, um Cross-Site Angriffe zu vermindern. In diesem Fall wird serverseitig ein Perl-Script ausgeführt.

#! /usr/bin/perl

print q{Content-Type: text/html; charset=latin1

<html>
<body id="inhalt">};

print $ENV{'REMOTE_ADDR'};

print "</body></html>";

Damit ist das Cache-Problem gelöst.

IPv6 und IPv4 zusammen

Wenn der Client per IPv6 Adresse ankommt, dann soll auch noch seine IPv4 Adresse ermittelt werden. Dazu wird der Code leicht abgewandet. Es wird ein Marker zurückgeliefert, der dem Javascript zeigt, das noch etwas fehlt.

...
my $addr = $ENV{'REMOTE_ADDR'};
print "$addr<br/>";

if($addr =~ /:/) {
    print "<span id='your-legacy-ip'></span><br/>\n";
}
...

Und das Javascript selbst wird entsprechend erweitert.

...
      mydiv.innerHTML = xmlhttp.responseText;

      var legacy = document.getElementById("your-legacy-ip");
      if(legacy) {
        var fill_legacy  = document.createElement("script");
        fill_legacy.type = "text/javascript";
        fill_legacy.src  = "http://v4.lutz.donnerhacke.de/cgi-bin/check-v4.js"
        legacy.appendChild(fill_legacy);
      }
    }
...

Der große Trick besteht darin, dass Scripte von anderen Servern nachgeladen werden dürfen. In diesem Fall von einem Server, der kein AAAA Record im DNS anbietet. Damit ist der Client gezwungen, auf das veraltete IPv4 Protokoll zurückzuspringen. Genausogut kann man einen IPv6-only Server befragen, um das aktuelle IP Protokoll anzufordern. Dieses Javascript darf nicht mit "document.write" arbeiten, sondern muß in den bestehenden DOM des Dokuments eingreifen.

#! /usr/bin/perl

print <<END
Content-Type: text/javascript

document.getElementById("your-legacy-ip").innerHTML = "$ENV{'REMOTE_ADDR'}";
END

Mehrwerte hinzufügen

Alle weiteren Sachen sind problemlos. Das den Haupttext aufgerufene Perl-Script kann wesentlich mehr tun. Es kann auf Übergang-IPv6 Adressen testen ($addr =~ /^2001:0*:/ || $addr =~ /^2002:/) und validierende Resolver nach DNSSEC fragen dig +dnssec PTR.

Bleibt noch die Mehrsprachigkeit. Dazu muß das Script erfahren, in welchem Sprachkontext es aufgerufen wurde. Am einfachsten geht das mit einem URL Parameter beim Script-Aufruf. Aber wer soll diesen auswerten? Witzigerweise ist es auch clientseitig im Browser möglich, die Parameter von einer URL an die anderen zu hängen. Damit ergibt sich das folgende vollständige Script.

if (window.XMLHttpRequest) {
  var xmlhttp = new XMLHttpRequest();
  var urlargs = document.currentScript.src.match(/(\?.*)/);
  var args = urlargs ? urlargs[0] : "";

  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4) {
      var mydiv = document.getElementById("your-ip");
      mydiv.innerHTML = xmlhttp.responseText;

      var legacy = document.getElementById("your-legacy-ip");
      if(legacy) {
        var fill_legacy  = document.createElement("script");
        fill_legacy.type = "text/javascript";
        fill_legacy.src  = "http://v4.lutz.donnerhacke.de/cgi-bin/check-v4.js"+args
        legacy.appendChild(fill_legacy);
      }
    }
  };
  xmlhttp.open("GET","/cgi-bin/check-ipv6-dnssec.pl"+args,true);
  xmlhttp.send(null);
}

Wer es nutzen will, soll es so einbinden, wie am Anfang geschrieben. Der relevante URL Parameter lautet locale={ger,eng}.

Erzwingen von IPv6

Wie bei RIPE festgestellt wurde, funktioniert die Erzwingung von IPv6 Verbindungen nicht, wenn als Übergangstechnik nur Teredo zum Einsatz kommt. Der Grund liegt darin, dass die aktuellen Windows-Systeme keine AAAA Anfragen stellen, wenn sie keine halbwegs brauchbaren IPv6 Adressen lokal vorliegen haben.

Das Nachschlagen von v6.lutz.donnerhacke.de ist also sinnfrei. Trägt man dagegen die IP Adresse ein, klappt es problemlos.

Viel Spaß!