-
Notifications
You must be signed in to change notification settings - Fork 229
/
fwknop-launcher-lsof.pl
executable file
·350 lines (270 loc) · 10.3 KB
/
fwknop-launcher-lsof.pl
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/perl -w
#
#############################################################################
#
# File: fwknop-launcher-lsof.pl
#
# URL: http://www.cipherdyne.org/fwknop/
#
# Purpose: This script provides a lightweight mechanism to launch the fwknop
# client shortly after a local user tries to initiate a connection
# to a remote service that is protected by fwknopd. The advantage
# of using this script is that local users don't have to run the
# fwknop client themselves before initiating an outbound
# connection. The idea for this script came from Sebastien
# Jeanquier. It is not required to have the fwknopd daemon
# installed in order for this script to be effective - only the
# fwknop client needs to be available.
#
# Author: Michael Rash (mbr@cipherdyne.org)
#
# Version: 2.0.1
#
# Copyright (C) 2011 Michael Rash (mbr@cipherdyne.org)
#
# License - GNU Public License version 2 (GPLv2):
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
#
#############################################################################
#
use POSIX;
use Getopt::Long;
use strict;
#================== config ==================
my $launcher_config = '~/.fwknop-launcher.conf';
my $sleep_interval = 1;
my $lsof_cmd = '/usr/bin/lsof';
my $fwknop_cmd = '/usr/bin/fwknop';
#================ end config ================
my $ip_re = qr|(?:[0-2]?\d{1,2}\.){3}[0-2]?\d{1,2}|; ### IPv4
my %spa_dispatch_cache = ();
my %spa_config_dsts = ();
my $fwknop_args_append = '';
my $fwknop_args_override = '';
my $key_file = '';
my $home_dir = '';
my $user = '';
my $no_daemon = 0;
my $verbose = 0;
my $help = 0;
die "[*] Use --help for usage information.\n" unless(GetOptions (
'config=s' => \$launcher_config, # launcher config
'lsof-cmd=s' => \$lsof_cmd, # lsof path
'fwknop-cmd=s' => \$fwknop_cmd, # fwknop path
'sleep-interval=i' => \$sleep_interval, # seconds
'user=s' => \$user, # set the user
'home-dir=s' => \$home_dir, # set the home dir
'no-daemon' => \$no_daemon, # run in the foreground
'verbose' => \$verbose, # verbose mode
'help' => \$help # Print help
));
&usage() if $help;
&init();
&daemonize() unless $no_daemon;
&watch_lsof();
exit 0;
#============== end main =============
sub watch_lsof() {
my $lsof_exec_ctr = 0;
for (;;) {
my $cmd = "$lsof_cmd -u $user -a -n -P -i 4"; ### IPv4
print "[+] Executing: $cmd\n" if $no_daemon and $verbose;
open LSOF, "$cmd |" or die "[*] Could not execute $cmd: $!";
### COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
### telnet 31483 mbr 3u IPv4 54543 0t0 TCP 127.0.0.1:41619->127.0.0.1:1234 (SYN_SENT)
while (<LSOF>) {
if (/^(\S+)\s+(\d+)\s+\S+\s+.*\s(\S+)\s+($ip_re):
(\d+)\-\>($ip_re):(\d+)\s+\(SYN_SENT\)/x) {
my $command = $1;
my $pid = $2;
my $proto = lc($3);
my $src_ip = $4;
my $s_port = $5;
my $dst_ip = $6;
my $d_port = $7;
print "[+] Matched line: $_" if $no_daemon and $verbose;
my $connection_string = "$command:$proto:$src_ip:$s_port:$dst_ip:$d_port";
next if defined $spa_dispatch_cache{$connection_string};
if (&is_valid_fwknop_dst($command, $proto, $dst_ip, $d_port)) {
if ($no_daemon and $verbose) {
print "[+] Matched connection, launching fwknop.\n";
}
&dispatch_fwknop($proto, $dst_ip, $d_port);
### add this line to the dispatch cache so that we don't
### send multiple SPA packets for the same connection
$spa_dispatch_cache{$connection_string} = '';
} else {
if ($no_daemon and $verbose) {
print "[-] Attempted connection not matched by ",
"any SPA_ACCESS variable in $launcher_config\n";
}
}
}
}
close LSOF;
$lsof_exec_ctr++;
if ($lsof_exec_ctr == 3600) {
%spa_dispatch_cache = ();
$lsof_exec_ctr = 0;
}
sleep $sleep_interval;
}
return;
}
sub is_valid_fwknop_dst() {
my ($cmd, $proto, $dst_ip, $d_port) = @_;
CMD: for my $access_cmd (keys %spa_config_dsts) {
next CMD unless $access_cmd eq 'any' or $cmd eq $access_cmd;
PROTO: for my $access_proto (keys %{$spa_config_dsts{$access_cmd}}) {
next PROTO unless $access_proto eq 'any' or $proto eq $access_proto;
IP: for my $access_ip (keys %{$spa_config_dsts{$access_cmd}{$access_proto}}) {
next IP unless $access_ip eq 'any' or $access_ip eq $dst_ip;
PORT: for my $access_port
(keys %{$spa_config_dsts{$access_cmd}{$access_proto}{$access_ip}}) {
return 1 if $access_port eq 'any' or $d_port == $access_port;
}
}
}
}
return 0;
}
sub dispatch_fwknop() {
my ($proto, $dst_ip, $d_port) = @_;
my $fwknop_cmd_str = "$fwknop_cmd -A $proto/$d_port -s -D " .
"$dst_ip --get-key $key_file";
if ($fwknop_args_append) {
$fwknop_cmd_str .= " $fwknop_args_append";
} elsif ($fwknop_args_override) {
$fwknop_cmd_str = "$fwknop_cmd $fwknop_args_override";
}
print "[+] Executing: $fwknop_cmd_str\n" if $no_daemon;
open FWKNOP, "$fwknop_cmd_str |"
or die "[*] Could not execute: $fwknop_cmd_str: $!";
close FWKNOP;
return 0;
}
sub daemonize() {
my $pid = fork();
exit 0 if $pid;
die "[*] $0: Couldn't fork: $!" unless defined $pid;
POSIX::setsid() or die "[*] $0: Can't start a new session: $!";
return;
}
sub init() {
if ($launcher_config =~ m|^\~|) {
### get the path to the user's home directory
&get_homedir() unless $home_dir;
$launcher_config =~ s|^\~|$home_dir|;
}
my $found_key_file = 0;
my $found_access_var = 0;
my $line_ctr = 1;
open F, "< $launcher_config"
or die "[*] Could not open $launcher_config: $!";
while (<F>) {
if (/^SPA_ACCESS\s+(\S+):(\S+):(\S+):(\S+);/) {
my $cmd = $1;
my $proto = lc($2);
my $dst_ip = $3;
my $d_port = $4;
### validate the protocol
unless ($proto eq 'any'
or $proto eq 'tcp'
or $proto eq 'udp'
) {
die "[*] Invalid proto '$proto' ",
"in $launcher_config at line: $line_ctr";
}
### validate connection destination IP
unless ($dst_ip eq 'any'
or $dst_ip =~ /$ip_re/) {
die "[*] Invalid IP '$dst_ip' ",
"in $launcher_config at line: $line_ctr";
}
### validate connection destination port
unless ($d_port eq 'any'
or $d_port =~ /^\d+$/) {
die "[*] Invalid port '$d_port' ",
"in $launcher_config at line: $line_ctr";
}
### add this dst into the valid destinations cache
$spa_config_dsts{$cmd}{$proto}{$dst_ip}{$d_port} = '';
$found_access_var = 1;
} elsif (/^KEY_FILE\s+(\S+);/) {
$key_file = $1;
if ($key_file =~ m|^\~|) {
&get_homedir() unless $home_dir;
$key_file =~ s|^\~|$home_dir|;
}
$found_key_file = 1;
} elsif (/^USER\s+(\S+);/) {
### might have been set via the command line
$user = $1 unless $user;
} elsif (/^FWKNOP_ARGS_APPEND\s+\"(.*)\";/) {
$fwknop_args_append = $1;
} elsif (/^FWKNOP_ARGS_OVERRIDE\s+\"(.*)\";/) {
$fwknop_args_override = $1;
}
$line_ctr++;
}
close F;
unless ($found_key_file) {
die "[*] Must have a key file defined via KEY_FILE";
}
unless ($found_access_var) {
die "[*] Must have at least one SPA_ACCESS var defined";
}
&get_username() unless $user;
return;
}
sub get_homedir() {
$home_dir = (getpwuid($<))[7];
unless ($home_dir) {
$home_dir = $ENV{'HOME'} if defined $ENV{'HOME'};
}
die "[*] Could not determine home directory. Use the -d <homedir> option."
unless $home_dir;
return;
}
sub get_username() {
$user = (getpwuid($<))[0];
unless ($user) {
if (defined $ENV{'USER'}) {
$user = $ENV{'USER'};
} elsif (defined $ENV{'USERNAME'}) {
$user = $ENV{'USERNAME'};
}
}
die "[*] Could not determine username. Use the -u <user> option."
unless $user;
return;
}
sub usage() {
print <<_HELP_;
Usage: fwknop-launcher-lsof.pl [options]
Options:
-c, --config <file> - Path to fwknop-launcher.conf config file.
-l, --lsof-cmd <path> - Path to lsof command.
-f, --fwknop-cmd <path> - Path to fwknop client command.
-s, --sleep <seconds> - Specify sleep interval (default:
$sleep_interval seconds)
-n --no-daemon - Run in foreground mode.
-u, --user <username> - Specify username (usually this is not
needed).
--home-dir <dir> - Path to user's home directory (usually
this is not needed).
-v --verbose - Print verbose information to the terminal
(requires --no-daemon).
--help - Print usage info and exit.
_HELP_
exit 0;
}