-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from CAIDA/rsdos-attacks
Add code to support reading RS-DOS attack avro data
- Loading branch information
Showing
5 changed files
with
194 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
cimport cython | ||
from pyavro_stardust.baseavro cimport AvroRecord, AvroReader, parsedString | ||
|
||
cpdef enum RsdosAttribute: | ||
ATTR_RSDOS_TIMESTAMP = 0 | ||
ATTR_RSDOS_PACKET_LEN = 1 | ||
ATTR_RSDOS_TARGET_IP = 2 | ||
ATTR_RSDOS_TARGET_PROTOCOL = 3 | ||
ATTR_RSDOS_ATTACKER_IP_CNT = 4 | ||
ATTR_RSDOS_ATTACK_PORT_CNT = 5 | ||
ATTR_RSDOS_TARGET_PORT_CNT = 6 | ||
ATTR_RSDOS_PACKET_CNT = 7 | ||
ATTR_RSDOS_ICMP_MISMATCHES = 8 | ||
ATTR_RSDOS_BYTE_CNT = 9 | ||
ATTR_RSDOS_MAX_PPM_INTERVAL = 10 | ||
ATTR_RSDOS_START_TIME_SEC = 11 | ||
ATTR_RSDOS_START_TIME_USEC = 12 | ||
ATTR_RSDOS_LATEST_TIME_SEC = 13 | ||
ATTR_RSDOS_LATEST_TIME_USEC = 14 | ||
ATTR_RSDOS_LAST_ATTRIBUTE = 15 | ||
|
||
cdef class AvroRsdos(AvroRecord): | ||
|
||
cdef unsigned char *packetcontent | ||
cdef public unsigned int pktcontentlen | ||
|
||
cpdef dict asDict(self) | ||
cpdef void resetRecord(self) | ||
cpdef bytes getRsdosPacketString(self) | ||
cpdef int setRsdosPacketString(self, const unsigned char[:] buf, | ||
const unsigned int maxlen) | ||
|
||
cdef class AvroRsdosReader(AvroReader): | ||
cdef int _parseNextRecord(self, const unsigned char[:] buf, | ||
const unsigned int maxlen) | ||
|
||
|
||
# vim: set sw=4 tabstop=4 softtabstop=4 expandtab : |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
|
||
# cython: language_level=3 | ||
cimport cython | ||
from cpython.mem cimport PyMem_Free | ||
from pyavro_stardust.baseavro cimport AvroRecord, read_long, read_string, \ | ||
AvroReader, parsedString | ||
|
||
@cython.final | ||
cdef class AvroRsdos(AvroRecord): | ||
def __init__(self): | ||
super().__init__(ATTR_RSDOS_LAST_ATTRIBUTE, 0, 0) | ||
self.pktcontentlen = 0 | ||
self.packetcontent = NULL | ||
|
||
def __str__(self): | ||
return "%u %u.%06u %u.%06u %08x %u %u %u %u %u %u %u %u %u" % \ | ||
(self.attributes_l[<int>ATTR_RSDOS_TIMESTAMP], \ | ||
self.attributes_l[<int>ATTR_RSDOS_START_TIME_SEC], | ||
self.attributes_l[<int>ATTR_RSDOS_START_TIME_USEC], | ||
self.attributes_l[<int>ATTR_RSDOS_LATEST_TIME_SEC], | ||
self.attributes_l[<int>ATTR_RSDOS_LATEST_TIME_USEC], | ||
self.attributes_l[<int>ATTR_RSDOS_TARGET_IP], | ||
self.attributes_l[<int>ATTR_RSDOS_TARGET_PROTOCOL], | ||
self.attributes_l[<int>ATTR_RSDOS_PACKET_LEN], | ||
self.attributes_l[<int>ATTR_RSDOS_ATTACKER_IP_CNT], | ||
self.attributes_l[<int>ATTR_RSDOS_ATTACK_PORT_CNT], | ||
self.attributes_l[<int>ATTR_RSDOS_TARGET_PORT_CNT], | ||
self.attributes_l[<int>ATTR_RSDOS_PACKET_CNT], | ||
self.attributes_l[<int>ATTR_RSDOS_BYTE_CNT], | ||
self.attributes_l[<int>ATTR_RSDOS_MAX_PPM_INTERVAL], | ||
self.pktcontentlen) | ||
|
||
cpdef dict asDict(self): | ||
if self.pktcontentlen == 0: | ||
initpkt = None | ||
else: | ||
initpkt = self.getRsdosPacketString() | ||
|
||
return { | ||
"timestamp": self.attributes_l[<int>ATTR_RSDOS_TIMESTAMP], | ||
"start_time_sec": self.attributes_l[<int>ATTR_RSDOS_START_TIME_SEC], | ||
"start_time_usec": self.attributes_l[<int>ATTR_RSDOS_START_TIME_USEC], | ||
"latest_time_sec": self.attributes_l[<int>ATTR_RSDOS_LATEST_TIME_SEC], | ||
"latest_time_usec": self.attributes_l[<int>ATTR_RSDOS_LATEST_TIME_USEC], | ||
"target_ip": self.attributes_l[<int>ATTR_RSDOS_TARGET_IP], | ||
"target_protocol": self.attributes_l[<int>ATTR_RSDOS_TARGET_PROTOCOL], | ||
"packet_len": self.attributes_l[<int>ATTR_RSDOS_PACKET_LEN], | ||
"attacker_count": self.attributes_l[<int>ATTR_RSDOS_ATTACKER_IP_CNT], | ||
"attack_port_count": self.attributes_l[<int>ATTR_RSDOS_ATTACK_PORT_CNT], | ||
"target_port_count": self.attributes_l[<int>ATTR_RSDOS_TARGET_PORT_CNT], | ||
"packet_count": self.attributes_l[<int>ATTR_RSDOS_PACKET_CNT], | ||
"byte_count": self.attributes_l[<int>ATTR_RSDOS_BYTE_CNT], | ||
"max_ppm_interval": self.attributes_l[<int>ATTR_RSDOS_MAX_PPM_INTERVAL], | ||
"icmp_mismatches": self.attributes_l[<int>ATTR_RSDOS_ICMP_MISMATCHES], | ||
"initial_packet": initpkt, | ||
} | ||
|
||
|
||
cpdef void resetRecord(self): | ||
self.pktcontentlen = 0 | ||
if self.packetcontent != NULL: | ||
PyMem_Free(self.packetcontent) | ||
super(AvroRsdos, self).resetRecord() | ||
|
||
cpdef bytes getRsdosPacketString(self): | ||
return <bytes>self.packetcontent[:self.pktcontentlen] | ||
|
||
cpdef int setRsdosPacketString(self, const unsigned char[:] buf, | ||
const unsigned int maxlen): | ||
|
||
cdef parsedString astr | ||
|
||
astr = read_string(buf, maxlen, addNullTerm=False) | ||
if astr.toskip == 0: | ||
return 0 | ||
self.packetcontent = astr.start | ||
self.pktcontentlen = astr.strlen | ||
self.sizeinbuf += astr.toskip + astr.strlen | ||
return 1 | ||
|
||
@cython.final | ||
cdef class AvroRsdosReader(AvroReader): | ||
|
||
def __init__(self, filepath): | ||
super().__init__(filepath) | ||
self.currentrec = AvroRsdos() | ||
|
||
cdef int _parseNextRecord(self, const unsigned char[:] buf, | ||
const unsigned int maxlen): | ||
|
||
cdef unsigned int offset, offinc | ||
cdef RsdosAttribute i | ||
|
||
if maxlen == 0: | ||
return 0 | ||
offset = 0 | ||
|
||
self.currentrec.resetRecord() | ||
|
||
for i in range(0, ATTR_RSDOS_LATEST_TIME_USEC + 1): | ||
offinc = self.currentrec.parseNumeric(buf[offset:], | ||
maxlen - offset, i) | ||
if offinc == 0: | ||
return 0 | ||
offset += offinc | ||
|
||
return self.currentrec.setRsdosPacketString(buf[offset:], | ||
maxlen - offset); | ||
|
||
|
||
# vim: set sw=4 tabstop=4 softtabstop=4 expandtab : |