净流量
Netflow数据包主要有三种版本:
- ``Netflow V5``
- ``Netflow V7``
- ``Netflow V9 / V10 (IPfix)``
虽然前两个版本非常简单,但是构建或剖析netflowv9/v10并不容易。
Netflow V1
netflow = NetflowHeader()/NetflowHeaderV1()/NetflowRecordV1()
pkt = Ether()/IP()/UDP()/netflow
Netflow V5
netflow = NetflowHeader()/NetflowHeaderV5(count=1)/NetflowRecordV5(dst="192.168.0.1")
pkt = Ether()/IP()/UDP()/netflow
NetflowV9/IPfix
netflowv9和IPfix使用基于模板的系统。这意味着,通过线路发送的记录需要在流集数据包中预先发送一个“模板”。
此模板用于理解记录的格式,因此在构建或剖析这些格式时需要提供该模板。
幸运的是,Scapy知道如何检测模板,并将提供相应的解析方法。
备注
以下示例适用于Netflow V9。使用IPfix时,使用完全相同的格式,但是用V10对应的类名替换类名(如果存在的话!斯帕伊在两人之间分享一些课程)。看看 netflow
Build
header = Ether()/IP()/UDP()
netflow_header = NetflowHeader()/NetflowHeaderV9()
# Let's first build the template. Those need an ID > 255.
# The (full) list of possible fieldType is available in the
# NetflowV910TemplateFieldTypes list. You can also use the int value.
flowset = NetflowFlowsetV9(
templates=[NetflowTemplateV9(
template_fields=[
NetflowTemplateFieldV9(fieldType="IN_BYTES", fieldLength=1),
NetflowTemplateFieldV9(fieldType="IN_PKTS", fieldLength=4),
NetflowTemplateFieldV9(fieldType="PROTOCOL"),
NetflowTemplateFieldV9(fieldType="IPV4_SRC_ADDR"),
NetflowTemplateFieldV9(fieldType="IPV4_DST_ADDR"),
],
templateID=256,
fieldCount=5)
],
flowSetID=0
)
# Let's generate the record class. This will be a Packet class
# In case you provided several templates in ghe flowset, you will need
# to pass the template ID as second parameter
recordClass = GetNetflowRecordV9(flowset)
# Now lets build the data records
dataFS = NetflowDataflowsetV9(
templateID=256,
records=[ # Some random data.
recordClass(
IN_BYTES=b"\x12",
IN_PKTS=b"\0\0\0\0",
PROTOCOL=6,
IPV4_SRC_ADDR="192.168.0.10",
IPV4_DST_ADDR="192.168.0.11"
),
recordClass(
IN_BYTES=b"\x0c",
IN_PKTS=b"\1\1\1\1",
PROTOCOL=3,
IPV4_SRC_ADDR="172.0.0.10",
IPV4_DST_ADDR="172.0.0.11"
)
],
)
pkt = header / netflow_header / flowset / dataFS
Dissection
Scapy提供了两种解析NetflowV9/IPFix的方法:
NetflowSession
: to use withsniff(session=NetflowV9Session, [...])
netflowv9_defragment()
:用于数据包或数据包列表。
在前面的例子中:
pkt = Ether(raw(pkt)) # will loose the defragmentation
pkt = netflowv9_defragment(pkt)[0]