This repository was archived by the owner on Apr 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcapFilterExpression.php
More file actions
312 lines (274 loc) · 8.8 KB
/
pcapFilterExpression.php
File metadata and controls
312 lines (274 loc) · 8.8 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
<?php
/**
* Pcap filter expression generator
* Generate tcpdump pcap-filter expressions
* Support: host, port and portrange primitives
*
* @author Kim Henriksen <kh@ipimp.at>
* @see http://www.manpagez.com/man/7/pcap-filter/
* @license http://philsturgeon.co.uk/code/dbad-license DBAD License
*/
class pcapFilterExpression {
/**
* Expression holder
*
* @var string
*/
private $expression = "";
/**
* pcap filter expression stack
*
* @var array
*/
private $expression_stack = array();
/**
* Init
* initialize the class
*/
public function init() {
$this->expression_stack = array();
$this->expression = "";
}
/**
* Generate a host primitive
*
* @param string $ip
* @param string $direction optional packet direction 'src' or 'dst' or emptry string
* @return \pcapFilterExpression
* @throws InvalidArgumentException
*/
public function host($ip, $direction = '') {
// Validate packet direction
if (!$this->isValidDirection($direction))
throw new InvalidArgumentException(sprintf("expression error: invalid packet direct flow %s, must be 'src', 'dst' or none (any)", $direction));
// Validate ip
if (!$this->isValidIP($ip))
throw new InvalidArgumentException(sprintf("expression error: %s is not a valid ip", $ip));
// Parse primitive
$this->expression .= trim(sprintf("%s host %s", $direction, $ip));
return $this;
}
/**
* Generate a port primitive
*
* @param int $port
* @param string $direction optional packet direction src, dst
* @param string $direction optional protocol tcp or udp
* @return \pcapFilterExpression
* @throws InvalidArgumentException
*/
public function port($port, $direction = '', $protocol = '') {
// Validate packet direction
if (!$this->isValidDirection($direction))
throw new InvalidArgumentException(sprintf("expression error: invalid packet direct flow %s", $direction));
// Validate protocol
if (!$this->isValidProtocol($protocol))
throw new InvalidArgumentException(sprintf("expression error: invalid protocol %s", $protocol));
// Validate port number
if (!$this->isValidPortNumber($port))
throw new InvalidArgumentException(sprintf("expression error: %s doesn't look like a valid port number", $port));
// Type cast port to an integer
$port = (int) $port;
// Parse primitive
$this->expression .= trim(sprintf("%s %s port %d", $protocol, $direction, $port));
return $this;
}
/**
* Generate a port range primitive
*
* @param int $start_port
* @param iint $end_port
* @param string $direction optional packet direction 'src', 'dst' or emptry string
* @param string $direction optional protocol 'tcp' or 'udp' or emptry string
* @return \pcapFilterExpression
* @throws InvalidArgumentException
*/
public function port_range($start_port, $end_port, $direction = '', $protocol = '') {
// Validate packet direction
if (!$this->isValidDirection($direction))
throw new InvalidArgumentException(sprintf("expression error: invalid packet direct flow %s", $direction));
// Validate protocol
if (!$this->isValidProtocol($protocol))
throw new InvalidArgumentException(sprintf("expression error: invalid protocol %s", $protocol));
// Validate port number
if (!$this->isValidPortNumber($start_port) OR !$this->isValidPortNumber($end_port))
throw new InvalidArgumentException(sprintf("expression error: %s-%s doesn't look like a valid port range", $start_port, $end_port));
// Type cast ports
$start_port = (int) $start_port;
$end_port = (int) $end_port;
// Parse primitive
$this->expression .= trim(sprintf("%s %s portrange %s-%s", $protocol, $direction, $start_port, $end_port));
return $this;
}
/**
* Append a concatenation operator
*
* @return \pcapFilterExpression
*/
public function concate() {
$this->expression .= " and ";
return $this;
}
/**
* Append a alternation operator
*
* @return \pcapFilterExpression
*/
public function alternate() {
$this->expression .= " or ";
return $this;
}
/**
* Append a negation operator
*
* @return \pcapFilterExpression
*/
public function negate() {
$this->expression .= " not ";
return $this;
}
/**
* Group primitives
* parenthesize previous primitives and operators
* also stacks it, so no need to call end()
*
*
* @param string $operator prepend a operator to the expression
* can be operators are:
* concate: (concate, and, &&)
* alternate: (alternate, or, ||)
* negate: (negate, not, !)
* @return \pcapFilterExpression
* @throws UnexpectedValueException
*/
public function group($operator = NULL) {
if (!empty($this->expression)) {
// Enclose primitives in parenthesize
$this->expression = "(" . $this->expression . ")";
switch ($operator) {
case "alternate":
case "or":
case "||":
$this->expression = ' or ' . $this->expression;
break;
case "negate":
case "not":
case "!":
$this->expression = ' not ' . $this->expression;
break;
case "concate":
case "and":
case "&&":
$this->expression = ' and ' . $this->expression;
break;
default :
break;
}
$this->end();
$this->begin();
} else {
throw new UnexpectedValueException('expression is empty');
}
return $this;
}
/**
* Begin
* start a new expression
*
* @return \pcapFilterExpression
*/
public function begin() {
$this->expression = "";
return $this;
}
/**
* End
* end expression and stack it
*
* @return \pcapFilterExpression
*/
public function end() {
$this->stack();
return $this;
}
/**
* Add it to the expression stack
*
* @return \pcapFilterExpression
* @throws UnexpectedValueException
*/
private function stack() {
// add expr to stack
if (!empty($this->expression))
$this->expression_stack[] = $this->expression;
else
throw new UnexpectedValueException("Expression empty, not stacking");
return $this;
}
/**
* Get pcap filter expression string
* Returns the pcap filter expression stack array as string
*
* @return string
*/
public function getPcapFilterExpressionString() {
// implode expression stack
$expr_str = "";
foreach ($this->expression_stack as $expr) {
// trim whitespaces
$expr_str = trim($expr_str .= " " . trim($expr));
}
return $expr_str;
}
/**
* Get pcap fitler expression stack
* Returns the pcap filter expression stack array
*
* @return array
*/
public function getPcapFilterStack() {
return $this->expression_stack;
}
/**
* Validate ip
*
* @param string $ip
* @return boolean
*/
private function isValidIP($ip) {
return filter_var($ip, FILTER_VALIDATE_IP);
}
/**
* Validate packet direction
*
* @param string $dest
* @return boolean
*/
private function isValidDirection($dest) {
return ($dest != 'dst' OR $dest != 'src' OR $dest == '');
}
/**
* Validate protocol
*
* @param string $protocol
* @return boolean
*/
private function isValidProtocol($protocol) {
return ($protocol != 'tcp' OR $protocol != 'udp' OR $protocol == '');
}
/**
* Validate port number
*
* @param int $port
* @return boolean
*/
private function isValidPortNumber($port) {
if (preg_match("/^[0-9]+$/", $port)) {
$port = (int) $port;
return ($port >= 0 || $port <= 65535);
}
else
return false;
}
}
?>