-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstructures.h
More file actions
319 lines (266 loc) · 8.57 KB
/
Copy pathstructures.h
File metadata and controls
319 lines (266 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
Container of header structures for
- Datalink layer
- Network layer
- Transport layer
Copyright (C) 2016-2024 Michele Campus <michelecampus5@gmail.com>
This file is part of decoder.
decoder is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
decoder is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
decoder. If not, see <http://www.gnu.org/licenses/>.
**/
#ifndef STRUCTURES_H_
#define STRUCTURES_H_
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <endian.h>
#include <net/ethernet.h>
#include "define.h"
#include "uthash.h"
#ifdef __GNUC__
/* GNU C */
#define PACK_OFF __attribute__ ((__packed__));
#endif
/* ++++++++++++++++++++++++ CISCO HDLC +++++++++++++++++++++++++ */
struct chdlc_hdr
{
u_int8_t addr; /* 0x0F (Unicast) - 0x8F (Broadcast) */
u_int8_t ctrl; /* always 0x00 */
u_int16_t proto_code; /* protocol type (e.g. 0x0800 IP) */
} PACK_OFF;
/* +++++++++++++++++++++ Ethernet header ++++++++++++++++++++++ */
struct ether_hdr
{
u_int8_t ether_dest_addr[ETHER_ADDR_LEN]; // Destination MAC address
u_int8_t ether_src_addr[ETHER_ADDR_LEN]; // Source MAC address
u_int16_t type_or_len; // Ethernet Type (for Eth II) or length (for Eth)
} PACK_OFF;
/* +++++++++++++++++ LLC SNAP header (IEEE 802.2) ++++++++++++ */
struct llc_snap_hdr
{
/* llc, should be 0xaa 0xaa 0x03 for snap */
u_int8_t dsap;
u_int8_t ssap;
u_int8_t control;
/* snap */
u_int8_t oui[3];
u_int16_t type;
} PACK_OFF;
/* +++++++++++++++ 802.1Q header (Virtual LAN) +++++++++++++++ */
struct vlan_hdr
{
u_int16_t tci;
u_int16_t type;
} PACK_OFF;
/* +++++++++++++++++++++++ MPLS header +++++++++++++++++++++++ */
struct mpls_header
{
#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
u_int32_t ttl:8, s:1, exp:3, label:20;
#elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
u_int32_t label:20, exp:3, s:1, ttl:8;
#endif
} __attribute__((packed));
/* ++++++++++ Radio Tap header (for IEEE 802.11) with timestamp +++++++++++++ */
struct radiotap_hdr
{
u_int8_t version; /* set to 0 */
u_int8_t pad;
u_int16_t len;
u_int32_t present;
} PACK_OFF;
/* ++++++++++++ Wireless header (IEEE 802.11) ++++++++++++++++ */
struct wifi_hdr
{
u_int16_t fc;
u_int16_t duration;
u_int8_t rcvr[6];
u_int8_t trsm[6];
u_int8_t dest[6];
u_int16_t seq_ctrl;
/* u_int64_t ccmp - for data encription only - check fc.flag */
} PACK_OFF;
/* +++++++++++++ Internet Protocol (IPv4) header +++++++++++++ */
struct ipv4_hdr
{
#if defined(__LITTLE_ENDIAN)
u_int8_t ihl:4, version:4;
#elif defined(__BIG_ENDIAN)
u_int8_t version:4, ihl:4;
#else
# error "Byte order must be defined"
#endif
u_int8_t ip_tos; // type of service
u_int16_t ip_len; // total length (ip header + payload)
u_int16_t ip_id; // identification number
u_int16_t ip_frag_offset; // fragment offset and flags
#define IP_RF 0x8000 /* reserved fragment flag */
#define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
u_int8_t ip_ttl; // time to live
u_int8_t ip_proto; // transport protocol type
u_int16_t ip_checksum; // checksum
u_int32_t ip_src_addr; // source IP address
u_int32_t ip_dst_addr; // destination IP address
} PACK_OFF;
/* +++++++++++++++++++++++ IPv6 header +++++++++++++++++++++++ */
struct ipv6_addr
{
/* union */
/* { */
u_int8_t ipv6_addr[16];
/* u_int16_t ipv6_addr16[8]; */
/* u_int32_t ipv6_addr32[4]; */
/* } ipv6_addr; /\* 128-bit IPV6 address *\/ */
};
struct ipv6_hdr
{
union {
struct ipv6_hdrctl {
u_int32_t ipv6_un1_flow; /* :4 version, :8 TC, :20 flow-ID */
u_int16_t ipv6_un1_plen; /* payload length */
u_int8_t ipv6_un1_next; /* next header */
u_int8_t ipv6_un1_hlim; /* hop limit */
} ipv6_un1;
u_int8_t ipv6_un2_vfc; /* 4 bits version, top 4 bits tclass */
} ipv6_ctlun;
struct ipv6_addr ipv6_src; /* source address */
struct ipv6_addr ipv6_dst; /* destination address */
} PACK_OFF;
/* +++++++++++++++++++++++ TCP header +++++++++++++++++++++++++ */
struct tcp_hdr
{
u_int16_t tcp_src_port; // source TCP port
u_int16_t tcp_dst_port; // destination TCP port
u_int32_t tcp_seq; // TCP sequence number
u_int32_t tcp_ack; // TCP acknowledgement number
#if defined(__LITTLE_ENDIAN)
u_int8_t reserved:4, tcp_offset:4;
#elif defined(__BIG_ENDIAN)
u_int8_t tcp_offset:4, reserved:4;
#else
# error "Byte order must be defined"
#endif
u_int8_t tcp_flags; // TCP flags (and 2-bits from reserved space)
#define TCP_FIN 0x01
#define TCP_SYN 0x02
#define TCP_RST 0x04
#define TCP_PUSH 0x08
#define TCP_ACK 0x10
#define TCP_URG 0x20
#define TH_FLAGS (TCP_FIN|TCP_SYN|TCP_RST|TCP_PUSH|TCP_ACK|TCP_URG)
u_int16_t tcp_window; // TCP window size
u_int16_t tcp_checksum; // TCP checksum
u_int16_t tcp_urgent; // TCP urgent pointer
} PACK_OFF;
/* +++++++++++++++++++++++ UDP header +++++++++++++++++++++++++ */
struct udp_hdr
{
u_int16_t udp_src_port;
u_int16_t udp_dst_port;
u_int16_t len;
u_int16_t check;
} PACK_OFF;
/* +++++++++++++++++++++++ SCTP header +++++++++++++++++++++++++ */
enum sctp_chunk_type {
SCTP_CHUNK_DATA,
SCTP_CHUNK_INIT,
SCTP_CHUNK_INIT_ACK,
SCTP_CHUNK_SACK,
};
struct sctp_hdr
{
u_int16_t sctp_src_port;
u_int16_t sctp_dst_port;
u_int32_t ver_tag;
u_int32_t check;
} PACK_OFF;
struct sctp_chunk_hdr {
u_int8_t type;
u_int8_t flags;
u_int16_t len;
} __attribute__((packed));
/* general stats filled by every pkts */
struct flow_stats
{
u_int16_t discarded_bytes;
u_int16_t ethernet_pkts;
u_int16_t wifi_pkts;
u_int16_t arp_pkts;
u_int16_t ipv4_pkts;
u_int16_t ipv6_pkts;
u_int16_t vlan_pkts;
u_int16_t mpls_pkts;
u_int16_t pppoe_pkts;
u_int16_t tcp_pkts;
u_int16_t udp_pkts;
u_int16_t num_tls_pkts; // count tls pkts
u_int16_t num_rtp_pkts; // count rtp pkts
u_int16_t num_rtcp_pkts; // count rtcp pkts
u_int16_t num_gtp_pkts; // count gtp pkts
u_int16_t num_diameter_pkts; // count diameter pkts
u_int16_t num_ngcp_pkts; // count ngcp pkts
u_int16_t num_rtsp_pkts; // count rtsp pkts
};
/* struct passed to the callback proto function */
/* for the detection process */
struct flow_callback_proto
{
pcap_t *pcap_handle;
struct flow_stats stats;
u_int8_t save;
};
/* Public Key from Server Certificate */
/* TODO */
// Handshake struct for the Flow (to put in Hashtable)
/*
- pre-master secret (encrypted)
- public key cert (from server in case of pms presence) TLS1
- public key cli (from client key exchange) TLS1.2
- random val (C-S)
- session ID (C-S)
- certificate
*/
struct Handshake
{
u_int8_t *enc_pre_master_secret; // Pre-Master Secret (encrypted)
u_int8_t *pubkey; // Public Key CLI Key Exch (TLS1.2)
u_int8_t *public_key_cert; // Public Key Cert (TLS1)
u_int8_t cli_rand[32]; // Client random num
u_int8_t srv_rand[32]; // Server random num
u_int8_t *sessID_c; // Client session ID
u_int8_t *sessID_s; // Server session ID
u_int8_t *certificate_S; // Server Certificate
u_int8_t *certificate_C; // [Opt] Client Certificate
u_int8_t chiph_serv[2]; // Chipher Suite Server
};
/* struct containing the fields used as the key in the hashmap for a flow */
struct Flow_key
{
// IPV4
u_int32_t ip_src;
u_int32_t ip_dst;
// IPV6
struct ipv6_addr ipv6_src;
struct ipv6_addr ipv6_dst;
u_int16_t src_port;
u_int16_t dst_port;
u_int8_t proto_id_l3;
};
/** HASH TABLE **/
struct Hash_Table
{
struct Flow_key flow_key_hash; // Key
struct Handshake *handshake;
u_int8_t is_handsk_fin;
UT_hash_handle hh;
};
#endif