2018年9月1日 星期六

pcapng file format

一般存放擷取封包的 pcap 檔案有一些限制,所以發展下一代的格式 -- The PCAP Next Generation Dump File Format (pcapng)。

檔案內容分成如下的區塊串接起來,前後有相同的長度欄位 (Block Total Length),限定出每個區塊的範圍,方便往後、或往前跳過不需要或不認得的 Block Type,容易擴展新定義的區塊。
32-bit Block Type
32-bit Block Total Length
Block Body
variable length, padded to 32 bits
32-bit Block Total Length
  • Block Total Length:整個區塊 byte 數。
  • Block Type:決定區塊內容 (Block Body) 的格式,區塊內容都會補齊 32-bit,所以區塊長度也會是 4 的倍數。
  • Block Body:依據 Block Type,除了必要欄位外,可放多個 TLV 選項。都 32-bit 對齊。

區塊雖然是一維線性串起來的,但有邏輯上的階層,最頂層一定是叫 Section Header Block 的區塊,一個檔案可以有多個頂層的 Section Header Block。

例如傳統 pcap 檔案轉成 pcapng 的區塊樹狀階層架構會是這樣:
Section Header Block
+- Interface Description Block
   +- Enhanced Packet Block
   +- Enhanced Packet Block
   +- ...
   +- Enhanced Packet Block

選項表

每個選項有 Type、Length、Value 三個欄位,可依 Length 跳過不需要或不認得的選項。
  • Option Type (2-byte):Option Type 的編碼。Most Significant Bit 為 1 保留為內部使用。
  • Option Length (2-byte):Option Value 不含補齊 32-bit 的實際長度。
  • Option Value (variable length):Option 的值,補到 32-bit 對齊。如果是字串,不能假設是以 0 結束。Option Length 為 0 則無此項。
有些 Option Type 可以重複,有些不行。選項表最後應該要有選項 'End of Option' (opt_endofopt)。

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|      Option Code              |         Option Length         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/                       Option Value                            /
/              variable length, padded to 32 bits               /
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/                                                               /
/                 . . . other options . . .                     /
/                                                               /
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|   Option Code == opt_endofopt  |  Option Length == 0          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

名稱Code長度可多個?說明
opt_endofopt00no選項表結束。
opt_comment1可變utf-8 編碼的註解,字串沒有結束的 0。換行用「\r\n」或「\n」。
opt_custom
客訂選項
0x0BAC可變utf-8 編碼的字串,沒有結束的 0。複製到新檔案。
0x0BAD可變二進位資料。複製到新檔案。
0x4BAC可變utf-8 編碼的字串,沒有結束的 0。不複製到新檔案。
0x4BAD可變二進位資料。不複製到新檔案。

客訂選項有 4 種,選項值開頭放登記在 IANA 的 32-bit Private Enterprise Number (PEN) 來避免衝突。

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|     Custom Option Code        |         Option Length         |
+---------------------------------------------------------------+
|                Private Enterprise Number (PEN)                |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/                        Custom Data                            /
/              variable length, padded to 32 bits               /
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Section Header Block (SHB)

最頂層階層的區塊
   0                   1                   2                   3
   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +---------------------------------------------------------------+
 0 |           Section Header Block Type = 0x0A0D0D0A              |
   +---------------------------------------------------------------+
 4 |                      Block Total Length                       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 8 |                Byte-Order Magic = 0x1A2B3C4D                  |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12 |        Major Version = 1      |       Minor Version = 0       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16 |                                                               |
   |                          Section Length                       |
   |                                                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24 /                                                               /
   /                      Options (variable)                       /
   /                                                               /
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                      Block Total Length                       |
   +---------------------------------------------------------------+
  • Block Type 0x0A0D0D0A (字串 "\n\r\r\n"):無論位元組順序怎樣都一樣都可以辨認到。透過 FTP 或 HTTP 傳送時,用來偵測不適當的 ASCII 轉換。
  • Byte-Order Magic 0x1A2B3C4D:用來偵測位元組順序 (byte order)。
  • Major Version 和 Minor Version:格式版本,目前 1.0。
  • Section Length (signed 64-bit value):不含 Section Header Block 自己的 section 長度。可用來快速跳過 section。-1 代表未知。4 的倍數。
  • Options:選項表。區塊特有選項有擷取機器的機型、作業系統、應用程式等。

Interface Description Block (IDB)

描述擷取封包的界面資訊。處理檔案的工具程式,會將每個 Section 的 IDB,獨立從 0 開始編號,稱為 Interface ID。Section 內其它有 Interface ID 欄位的區塊,就要有對應的 IDB。Simple Packet Block 沒有 Interface ID 欄位隱含 Interface ID 0。

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +---------------------------------------------------------------+
 0 |                    Block Type = 0x00000001                    |
   +---------------------------------------------------------------+
 4 |                      Block Total Length                       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 8 |           LinkType            |         Reserved = 0          |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12 |                            SnapLen                            |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16 /                                                               /
   /                      Options (variable)                       /
   /                                                               /
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                      Block Total Length                       |
   +---------------------------------------------------------------+
  • LinkType: 跟 pcap 的 header type 一樣。
  • SnapLen:每個封包最多擷取前面的 byte 數。0 表示不限制。
特有的選項有:
Name Code Length 可多個? 說明
if_name 2 Variable no
if_description 3 Variable no
if_IPv4addr 4 8 yes
if_IPv6addr 5 17 yes
if_MACaddr 6 6 no
if_EUIaddr 7 8 no
if_speed 8 8 no
if_tsresol91no時間精度。MSB 0:剩下是 10 的 negative power。MSB 1:剩下是 2 的 negative power。預設是 10-6,跟 pcap 預設的微秒一致。
if_tzone 10 4 no
if_filter 11 variable no
if_os 12 variable no
if_fcslen 13 1 no
if_tsoffset 14 8 no

Enhanced Packet Block (EPB)

標準放封包的區塊。另外封包也可以放在 Simple Packet Block,用來加速擷取檔案的產生。
   0                   1                   2                   3
   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +---------------------------------------------------------------+
 0 |            Enhanced Packet Block Type = 0x00000006            |
   +---------------------------------------------------------------+
 4 |                      Block Total Length                       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 8 |                         Interface ID                          |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12 |                        Timestamp (High)                       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16 |                        Timestamp (Low)                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20 |                    Captured Packet Length                     |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24 |                    Original Packet Length                     |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28 /                                                               /
   /                          Packet Data                          /
   /              variable length, padded to 32 bits               /
   /                                                               /
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   /                                                               /
   /                      Options (variable)                       /
   /                                                               /
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                      Block Total Length                       |
   +---------------------------------------------------------------+
  • Interface ID (32-bit):對應的 IDB。原本 Packet Block 只有 16-bit,另外 16-bit Drops Count 改移到 option。
  • Timestamp (High) and Timestamp (Low): 64-bit 的 epoch 時間,單位在 IDB 的 'if_tsresol' 選項,預設是微秒。在 pcap 是分成 32-bit 秒和 32-bit 微秒。
  • Captured Packet Length:Packet Data 的封包長度,不含補齊。會是 Original Packet Length 和 IDB 的 SnapLen 比較,值較小的。
  • Original Packet Length:在網路實際傳送的封包長度。
  • Packet Data:擷取的封包加上補到 32-bit 對齊。封包格式依據 IDB 的 LinkType 欄位。

Simple Packet Block (SPB)

SPB 簡化 EPB,只保留 Original Packet Length,只有 16-byte overhead,不需要取得 timestamp 和計算,適合有效能或空間瓶頸的情況。EPB 和 SPB 可以交錯使用。
    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +---------------------------------------------------------------+
 0 |                Simple Block Type = 0x00000003                 |
   +---------------------------------------------------------------+
 4 |                      Block Total Length                       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 8 |                    Original Packet Length                     |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12 /                                                               /
   /                          Packet Data                          /
   /              variable length, padded to 32 bits               /
   /                                                               /
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                      Block Total Length                       |
   +---------------------------------------------------------------+
  • Original Packet Length: 在網路實際傳送的封包長度。和 IDB 的 SnapLen 比較,較小的就是實際擷取的封包長度。
  • Packet Data:擷取的封包和 padding。封包格式依據 IDB 的 LinkType 欄位。

其它區塊

有 Name Resolution Block、Interface Statistics Block、Custom Block、及一些實驗性的區塊。 32-bit Block Type 的 Most Significant Bit (MSB) 為 1,保留為內部使用。

參考

  1. https://github.com/pcapng/pcapng

沒有留言:

張貼留言

SIP header Via

所有 SIP 訊息 都要有 Via,縮寫 v。一開始的 UAC 和後續途經的每個 proxy 都會疊加一個 Via 放傳送的位址,依序作為回應的路徑。 格式 sent-protocol sent-by [ ;branch= branch ][ ; 參數 ...] s...