This repository was archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmsfweb
More file actions
executable file
·2577 lines (2027 loc) · 64 KB
/
Copy pathmsfweb
File metadata and controls
executable file
·2577 lines (2027 loc) · 64 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
###############
##
# Name: msfweb
# Author: H D Moore <hdm [at] metasploit.com>
# Version: $Revision$
# Description: Web interface to the Metasploit Exploit Framework
# License:
#
# This file is part of the Metasploit Exploit Framework
# and is subject to the same licenses and copyrights as
# the rest of this package.
#
##
require 5.6.0;
use strict;
use FindBin qw{$RealBin};
use lib "$RealBin/lib";
use Msf::WebUI;
use POSIX;
use IO::Socket;
use Getopt::Std;
no utf8;
no locale;
Msf::UI::ActiveStateSucks();
Msf::UI::BrokenUTF8();
my $ui = Msf::WebUI->new($RealBin);
my $VERSION = $ui->Version;
$ui->SetGlobalEnv('_Console', 'Msf::PayloadComponent::WebConsole');
my @sections = qw{ EXPLOITS PAYLOADS SESSIONS };
my $exploitsIndex;
my $payloadsIndex;
my $encodersIndex;
my $nopsIndex;
my $exploits = { };
my $payloads = { };
my $moduleTypes = { };
my $moduleKeys = { };
my $moduleArch = { };
my $moduleOS = { };
my $moduleList = { };
my $modules = { };
my $burl;
my %args;
# Cache control date
my $cache_start = HTTPDate();
my %cache_hosts = ();
# Configuration defaults...
my %config =
(
'BindAddr' => '127.0.0.1',
'BindPort' => 55555,
'LogFile' => '-',
'LogLevel' => 0,
'Reload' => 0,
'Theme' => 'default',
'ThemeDir' => "$RealBin/data/msfweb/themes",
'IconDir' => "$RealBin/data/msfweb/icons",
'CacheDir' => $ui->_DotMsfDir. "/msfweb",
'Defanged' => 0,
);
# Process the command line options
getopts("a:l:v:p:t:T:C:r:hD", \%args);
# Show the help output
if ($args{'h'}) { Usage() }
# IP address and possible TCP port
if (exists($args{'a'}))
{
my ($host, $port) = split(/:/, $args{'a'});
$config{'BindAddr'} = $host;
if ($port) {
$config{'BindPort'} = $port;
}
}
# TCP port
if (exists($args{'p'})) {
$config{'BindPort'} = $args{'p'};
}
# Log file name
if (exists($args{'l'})) {
$config{'LogFile'} = $args{'l'};
}
# Log verbosity level
if (exists($args{'v'})) {
$config{'LogLevel'} = $args{'v'};
}
# Theme name
if (exists($args{'t'})) {
$config{'Theme'} = $args{'t'};
}
# Theme directory
if (exists($args{'T'})) {
$config{'ThemeDir'} = $args{'T'};
}
# Cache directory
if (exists($args{'C'})) {
$config{'CacheDir'} = $args{'C'};
}
# Reload modules
if (exists($args{'r'})) {
$config{'Reload'}++;
}
# Check for 'defanged' option
if (exists($args{'D'})) {
$config{'Defanged'}++;
}
my $bindstr = $config{'BindAddr'}. ':' .$config{'BindPort'};
print STDERR "+----=[ Metasploit Framework Web Interface ($bindstr)\n\n";
# Create the .msf directory if it does not exist
if (! -d $ui->_DotMsfDir) {
mkdir($ui->_DotMsfDir, 0700);
};
# Create the cache directory if it does not exist
if (! -d $config{'CacheDir'}) {
mkdir($config{'CacheDir'}, 0700);
}
# Check to see if we can write files to the cache directory
if (! -d $config{'CacheDir'} || ! open(T, ">>".$config{'CacheDir'}.'/.test_'.$$) ) {
print STDERR "ERROR: the specified cache directory is not accessible: $!\n";
exit(1);
}
unlink($config{'CacheDir'}.'/.test_'.$$) && close(T);
$ui->SetTempEnv('_CacheDir', $config{'CacheDir'} );
# Create a cache prefix based on the master pid
# This prevents locking issues in Cygwin...
$ui->SetTempEnv('_CachePath', sprintf('%s/%.4x_', $config{'CacheDir'}, $$));
# XXX Clear old session logs on startup
# Verify that the specified theme actually exists
if (! -r $config{'ThemeDir'} .'/'. $config{'Theme'} .'/style.css') {
print STDERR "ERROR: the specified theme does not exist\n";
exit(1);
}
$ui->SetTempEnv('_Theme', $config{'ThemeDir'} .'/'. $config{'Theme'} );
# Verify that the icon directory exists
if (! -r $config{'IconDir'} .'/win32.gif') {
print STDERR "ERROR: the specified icon directory does not exist\n";
exit(1);
}
$ui->SetTempEnv('_IconDir', $config{'IconDir'});
# Set defanged mode if needed...
$ui->SetTempEnv('_Defanged', $config{'Defanged'});
# Create the inital list of modules
LoadAllModules();
# Pregenerate the complete lists without any filters
if (! $config{'Reload'} ) {
ExploitList();
PayloadList();
}
my $ghettoWeb = GhettoWeb->new
(
'host' => $config{'BindAddr'},
'port' => $config{'BindPort'},
'fnWeb' => \&ProcessWebRequest,
'fnIPC' => \&ProcessIPCRequest,
'fnHRP' => \&HTTPRequest,
);
$ghettoWeb->LogFile($config{'LogFile'});
$ghettoWeb->LogLevel($config{'LogLevel'});
$ghettoWeb->Run();
if ($ghettoWeb->IsError) {
print STDERR "[*] Error: ".$ghettoWeb->GetError."\n";
}
exit(0);
########################################################
# !!! WARNING !!! #
# #
# Viewing the code below may result in the loss #
# of vision, nausea, indigestion, insomnia, or #
# even INSTANT DEATH. You have been warned. #
# #
########################################################
######################################################################
######################################################################
######################################################################
sub Usage {
print STDERR qq{
Usage: $0 <options>
Options:
-a <ip address> Bind to this IP instead of the loopback address
-p <tcp port> Bind to this TCP port instead of 55555
-l <log file> The path name to use for a log file (stderr)
-v <log level> A number between 0 and 10 that controls log verbosity
-t <theme name> Select a specific theme: default, gwhite, gblack
-T <theme dir> Use an alternate directory for msfweb themes
-C <cache dir> Use a specific directory for session cache files
-r <boolean> Reload all modules with each new web request
};
exit(0);
}
sub ProcessResRequest {
my ($req, $cli, $ipc) = @_;
my ($is_icon, $is_theme);
# Process requests for shared icons
if (exists($req->{'params'}->{'ICON'})) {
my $os = $req->{'params'}->{'ICON'};
my $icondir = $ui->GetTempEnv('_IconDir') || "$RealBin/data/msfweb/icons";
my %icons =
(
'aix' => ['image/gif', 'aix.gif'],
'amiga' => ['image/gif', 'amiga.gif'],
'beos' => ['image/gif', 'be.gif'],
'bsd' => ['image/gif', 'bsd.gif'],
'cisco' => ['image/gif', 'cisco.gif'],
'hpux' => ['image/gif', 'hpux.gif'],
'irix' => ['image/gif', 'irix.gif'],
'linux' => ['image/gif', 'linux.gif'],
'novell'=> ['image/gif', 'novell.gif'],
'os2' => ['image/gif', 'os2.gif'],
'osx' => ['image/gif', 'osx.gif'],
'sun' => ['image/gif', 'sun.gif'],
'win32' => ['image/gif', 'win32.gif'],
# special case icons
'any' => ['image/gif', 'any.gif'],
'unknown' => ['image/gif', 'unknown.gif'],
'favorite' => ['image/x-icon', 'favicon.ico'],
);
my ($type, $path) = @{ $icons{$os} || $icons{'unknown'} };
my $data;
if ( open (X, "<$icondir/$path") ) {
binmode (X);
while (<X>) { $data .= $_ }
close (X);
}
$cli->Send(HTTPResponse(200, $data, $type));
return;
}
# Process requests for theme-based resources
if (exists($req->{'params'}->{'ID'})) {
my $theme = $ui->GetTempEnv('_Theme') || "$RealBin/data/msfweb/themes/default";
my %files =
(
'LOGO' => ['image/jpg', 'logo.jpg'],
'STYLE' => ['text/css', 'style.css'],
);
if ( exists( $files{ $req->{'params'}->{'ID'} } ) ) {
my ($type, $path) = @{ $files{ $req->{'params'}->{'ID'} } };
my $data;
if ( open (X, "<$theme/$path") ) {
binmode (X);
while (<X>) { $data .= $_ }
close (X);
}
$cli->Send(HTTPResponse(200, $data, $type));
}
return;
}
}
sub ProcessIPCRequest {
my $self = shift;
my $ipc = shift;
my $req = $ipc->getline || return;
chomp($req);
my ($cmd, @args) = split(/\s+/, $req);
$self->Log(3, "IPC: $ipc $$ $cmd ($args[0] | $args[1])");
return if ! $cmd;
if ($cmd eq 'SESSION') {
my $sid = $self->SessionNew($ipc);
$ipc->printflush("SID $sid\n");
}
# NEW <sid> <pid of exploit pipe handler>
if ($cmd eq 'NEW') {
$self->SessionPipePID(@args);
$ipc->printflush("SHELL\n");
}
if ($cmd eq 'SHUTDOWN') {
$ipc->printflush("SHUTDOWN\n");
$self->SessionRemove($args[0]);
}
if ($cmd eq 'CMD' || $cmd eq 'DATA') {
my $out = $self->SessionIPC($args[0]);
if ($out) {
$out->printflush("$cmd ".$args[1]."\n");
$ipc->printflush("$cmd OK\n");
}
else {
$ipc->printflush("$cmd ERROR\n");
}
}
if ($cmd eq 'INFO') {
$self->SessionInfo($args[0], $args[1]);
}
# XXX replace this :/
if ($cmd eq 'LIST') {
my @list = $self->SessionList();
my $data;
foreach my $sid (@list) {
$data .= 'sid='. unpack('H*', $sid). ',' .$self->SessionInfo($sid). ' ';
}
$ipc->printflush("LIST $data\n");
}
}
sub ProcessWebRequest {
my $self = shift;
my ($req, $cli, $ipc) = @_;
my $state = $req->{'params'};
my $mbase = $req->{'base'};
my $res;
my $cinfo = $cli->PeerAddr .':'. $cli->PeerPort;
my $log = "HTTP: $cinfo $$ ". $req->{'path'} ." ". $state->{'MODE'};
# Dirty hack to support /favicon.ico requests
if ($req->{'path'} eq '/favicon.ico') {
$req->{'path'} = '/RESOURCE?ICON=favorite';
$req->{'base'} = 'RESOURCE';
$state->{'ICON'} = 'favorite';
}
if (exists($state->{'MODULE'})) {
$log .= " module=". $state->{'MODULE'};
}
if ($req->{'base'} eq 'RESOURCE') {
$log .= " ICON=". $state->{'ICON'} if $state->{'ICON'};
$log .= " ID=". $state->{'ID'} if $state->{'ID'};
}
$self->Log(3, $log);
# Process resource requests
if ($req->{'base'} eq 'RESOURCE') {
ProcessResRequest($req, $cli, $ipc);
exit(0);
}
# Generate the base URL
$burl = "/$mbase?";
# Reload all modules only when the -r option has been specified
if ($config{'Reload'}) {
LoadAllModules();
}
# Start with a standard header
$res .= HTML_Header($req);
my $logaction;
if (defined (my $mid = $state->{'MODULE'} )) {
my $mname;
my $icons;
if ($mbase eq 'EXPLOITS' && exists($exploits->{$mid}) ) {
$mname = $exploits->{$mid}->Name;
$mname .= ' ('.$state->{'PAYLOAD'}.')' if exists($state->{'PAYLOAD'});
foreach ( KeysToIcons( @{ $exploits->{$mid}->OS } ) ) {
$icons .= "<img src='/RESOURCE?ICON=$_' border=0 class='iconset' alt='$_'> ";
}
}
if ($mbase eq 'PAYLOADS' && exists($payloads->{$mid}) ) {
$mname = $payloads->{$mid}->Name;
foreach ( KeysToIcons( @{ $payloads->{$mid}->OS } ) ) {
$icons .= "<img src='/RESOURCE?ICON=$_' border=0 class='iconset' alt='$_'> ";
}
}
if (! defined($mname)) {
$mname = 'Invalid Module';
}
$res .= "<table width='100%' cellspacing=0 border=0 cellpadding=0>\n";
$res .= "<tr><td class='moduleIcons' align='center'>$icons</td>";
$res .= "<td class='moduleName'><div class='textBold'>$mname</div></td></tr>\n";
$res .= "</table><br>\n";
}
$state->{'client'} = $cli;
$state->{'parent'} = $ipc;
if ($req->{'base'} eq 'EXPLOITS') {
if ($state->{'MODE'} eq 'MAIN') { $res .= ExploitList($state) }
if ($state->{'MODE'} eq 'SELECT') { $res .= ExploitTarget($state) }
if ($state->{'MODE'} eq 'PAYLOAD') { $res .= ExploitPayload($state) }
if ($state->{'MODE'} eq 'OPTIONS') { $res .= ExploitOptions($state) }
if ($state->{'MODE'} eq 'TARGETS') { $res .= ExploitTargets($state) }
if ($state->{'MODE'} eq 'CHECK') { $res .= ExploitCheck($state) }
# ExploitExec doesn't return on success
if ($state->{'MODE'} eq 'EXPLOIT') { $res .= ExploitExec($state) }
$res .= HTML_Footer();
}
if ($req->{'base'} eq 'PAYLOADS') {
if ($state->{'MODE'} eq 'MAIN') { $res .= PayloadList($state) }
if ($state->{'MODE'} eq 'SELECT') { $res .= PayloadOptions($state) }
if ($state->{'MODE'} eq 'GENERATE') { $res .= PayloadGenerate($state) }
$res .= HTML_Footer();
}
if ($req->{'base'} eq 'SESSIONS') {
if ($state->{'MODE'} eq 'MAIN') { $res .= SessionList($state) }
if ($state->{'MODE'} eq 'LOAD') { $res = SessionLoad($state) }
if ($state->{'MODE'} eq 'COMMAND') { $res = SessionCommand($state) }
if ($state->{'MODE'} eq 'UPDATE') { $res = SessionUpdate($state) }
}
$cli->Send(HTTPResponse(200, $res));
$cli->Close();
exit(0);
}
sub HTML_Header {
my $req = shift;
my $header = qq
[<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Metasploit Framework Web Console v$VERSION</title>
<link type='text/css' rel='stylesheet' href='/RESOURCE?ID=STYLE'>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<br>
<div align='center'><img src='/RESOURCE?ID=LOGO' alt='msfweb'></div>
<br>
<table align='center' cellpadding=8 border=0 cellspacing=1 width='100%' class='tblInner'>
<tr><td>
<table align='center' cellpadding=8 cellspacing=1 width='100%' class='tblOuter' >
<tr>
];
my $lastTab = 'tabLight';
my $currTab;
$header .= "\t\t\t";
my $width = int( 100 / scalar(@sections) ) .'%';
foreach my $section (@sections) {
$lastTab = ($lastTab eq 'tabLight') ? 'tabDark' : 'tabLight';
$currTab = ($section eq $req->{'base'}) ? 'tabActive' : $lastTab;
$header .= qq[ <td class='$lastTab' width='$width' align='center'> <a href='/$section' class='$currTab'>$section</a> </td> ];
}
$header .= qq[
</tr>
</table>
</td></tr>
<tr><td colspan=5>
<br>
];
return $header;
}
sub HTML_Footer {
return qq[
<br>
</td>
</tr>
</table>
<div align='center' class='copy'>
<br>copyright © 2003-2005 metasploit.com<br><br>
</div>
</body>
</html>
];
}
sub KeysToIcons {
my @keys = @_;
my %icons;
my %match;
@icons { qw { aix amiga beos bsd cisco hpux irix linux novell os2 osx sun win32 unknown } } = ();
my %osmaps =
(
'solaris' => 'sun',
'ios' => 'cisco',
'openbsd' => 'bsd',
'freebsd' => 'bsd',
'netbsd' => 'bsd',
'bsdi' => 'bsd',
'macos' => 'mac',
);
if (! scalar(@keys) ) {
return ('any');
}
foreach (@keys) {
$match{ $_ }++ if exists($icons{$_});
$match{ $osmaps{ $_ } }++ if exists($icons{ $osmaps{ $_ } });
}
if (! scalar(keys %match)) {
$match{'unknown'}++;
}
return sort keys %match;
}
sub URLEncode {
my ($data) = @_;
my $res;
foreach (split(//, $data))
{ $res .= sprintf("%%%.2x", ord($_)) }
return $res;
}
sub StateToURL {
my ($mode, $state) = @_;
my (%tmp, $res);
$res = $burl;
foreach (keys(%{$state})) {
next if $_ eq "client";
next if $_ eq "parent";
my $value = $_ eq "MODE" ? $mode : $state->{$_};
$res .= $_ . "=" . URLEncode($value) . "&";
}
return $res;
}
sub StateToOptions {
my ($state) = @_;
my $res = {};
foreach (keys(%{$state})) {
if (m/^OPT\_(.*)/ && defined($state->{$_})) {
my $name = $1;
# Block all options starting with underscore (thanks Dino!)
if ($name !~ /^_/) {
$res->{$name} = $state->{$_};
}
else {
# XXX - report a possible "refang" attack?
next;
}
}
}
return $res;
}
sub SessionList {
my $state = shift;
my $ipc = $state->{'parent'};
my $res;
my %slist;
$ipc->printflush("LIST\n");
if ( defined(my $raw = $ipc->getline) ) {
if ( (my ($data) = $raw =~ m/^LIST\s+(.*)\n/) ) {
foreach my $entry (split(/\s+/, $data)) {
my %hash = IPCDataToHash($entry);
my $sid = $hash{'sid'};
$slist{$sid} = \%hash;
}
}
}
if (! scalar(keys(%slist))) {
$res .= "<p class='textBold'>There are no active sessions.<br\><br\></p>\n";
$res .= HTML_Footer();
return $res;
}
$res .= "<p class='textBold'>Session List:<br\><br\></p>\n";
$res .= "<table align='center' cellpadding=0 border=0 cellspacing=0 width='95%'>\n";
$res .= "<tr>";
foreach (qw{Time Session User Exploit Target Payload}) {
$res .= "<td class='textBold' align='center'>$_</td>";
}
$res .= "</tr><tr><td colspan=6><br\></td></tr>\n";
foreach my $sid (sort { $a <=> $b } keys %slist ) {
my $ses = $slist{$sid};
my $s_module = $ses->{'module'};
my $s_payload = $ses->{'payload'};
my $s_client = $ses->{'client'};
my $s_target = $ses->{'target'};
my $s_time = scalar(localtime($ses->{'start'}));
my $mlink = '/EXPLOITS?MODE=SELECT&MODULE='.URLEncode($s_module);
my $plink = ($s_payload ne 'unknown') ? '/PAYLOADS?MODE=SELECT&MODULE='.URLEncode($s_payload) : '#';
$res .= "<tr>";
$res .= "<td class='textNormal'>$s_time</td>";
$res .= "<td class='textNormal'><a href='/SESSIONS?MODE=LOAD&SID=$sid' target='_blank'>Session $sid</a></td>";
$res .= "<td class='textNormal'>$s_client</td>";
$res .= "<td class='textNormal'><a href='$mlink'>$s_module</a></td>";
$res .= "<td class='textNormal'>$s_target</td>";
$res .= "<td class='textNormal'><a href='$plink'>$s_payload</a></td>";
$res .= "</tr>";
}
$res .= "</table>\n";
$res .= HTML_Footer();
return $res;
}
sub SessionLoad {
my $state = shift;
my $sid = $state->{'SID'} + 0;
my $res = qq
[<html>
<head>
<title>Metasploit Framework v$VERSION - Session $sid </title>
<link type='text/css' rel='stylesheet' href='/RESOURCE?ID=STYLE'>
</head>
<frameset rows="*, 90" border=0 frameborder=0 framespacing=0>
<frame src="/SESSIONS?MODE=UPDATE&SID=$sid" name="update">
<frame src="/SESSIONS?MODE=COMMAND&SID=$sid" name="command">
<noframes><body>This feature requires frames...</body></noframes>
</frameset>
];
return $res;
}
# Quick command bar to send commands to the shell
sub SessionCommand {
my $state = shift;
my $data = $state->{'IDATA'};
my $ipc = $state->{'parent'};
my $sid = $state->{'SID'} + 0;
my $cmd = $state->{'CMD'};
my $cmdurl = "/SESSIONS?MODE=COMMAND&SID=$sid&CMD=";
my $lspace = " " x 4;
my $verify = 'javascript:if(!confirm("Are you sure that you want to kill this session?")){return false; }';
my $cmdbar =
"<div class='textBold' class='CommandBar'>\n".
"<ul id='CommandBarList'>\n".
"<li>Session Commands:$lspace".
"<li><a href='".$cmdurl."DIE' onClick='$verify'>Session::Kill</a>$lspace".
"<li><a href='".$cmdurl."INT'>Session::Break</a>$lspace".
"<li><a href='http://metasploit.com/' target='_blank'>Metasploit::Website</a>$lspace".
"<li><a href='http://metasploit.com/donate.html' target='_blank'>Metasploit::Donate</a> ".
"</ul>\n".
"</div>";
my $res = qq
[<html>
<head>
<title>Metasploit Framework Web Console v$VERSION</title>
<link type='text/css' rel='stylesheet' href='/RESOURCE?ID=STYLE'>
</head>
<body>
<form action='/SESSIONS' name='cmdform'>
<input type='hidden' name='MODE' value='COMMAND'>
<input type='hidden' name='SID' value='$sid'>
<input type='text' name='IDATA' size=80 maxsize=4000>
<input type='submit' name='RUN' value='Run' class='button'>
</form>
$cmdbar
<script language='javascript'>
<!--
window.focus();
document.cmdform.IDATA.value="";
document.cmdform.IDATA.focus();
//-->
</script>
</body>
</html>
];
# Process any special command sequences
if ($cmd) {
$ipc->printflush("CMD $sid ".unpack("H*", $cmd)."\n");
my $raw = $ipc->getline;
if ($raw =~ /ERROR/) {
$ipc->printflush("SHUTDOWN $sid\n");
}
}
# Process incoming data (shell commands)...
elsif ($data) {
$data .= "\n";
$ipc->printflush("DATA $sid ".unpack("H*", $data)."\n");
my $raw = $ipc->getline;
if ($raw =~ /ERROR/) {
$ipc->printflush("SHUTDOWN $sid\n");
}
}
return $res;
}
sub SessionUpdate {
my $state = shift;
my $ipc = $state->{'parent'};
my $chi = $state->{'client'};
my $sid = $state->{'SID'} + 0;
my $cmd = $state->{'CMD'};
my $res = qq
[<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Metasploit Framework Web Shell v$VERSION - Session $sid </title>
<link type='text/css' rel='stylesheet' href='/RESOURCE?ID=STYLE'>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<pre>
];
# Use chunked transfer mode to return partial responses
my $out = "HTTP/1.1 200 OK\r\n".
"Connection: close\r\n".
"Date: ". HTTPDate()."\r\n".
"Content-Type: text/html\r\n".
"Transfer-Encoding: chunked\r\n\r\n";
$chi->Send($out);
$chi->Send(sprintf("%x\r\n%s\r\n", length($res), $res));
my $init = 0;
my $foff = 0;
my $idle = 0;
my $tick = time();
my $cache_file = $ui->GetEnv('_CachePath'). sprintf("cache%.8x.dat", $sid);
my $cache_data;
my $count = 10;
while ($count && ! open(CACHE, "<$cache_file")) {
sleep(1);
$count--;
}
if (! $count) {
$res = "<div class='textBold'>!!! Timeout reached waiting for the session log</div>\n";
$res .= "</pre></body></html>\n";
$chi->Send(sprintf("%x\r\n%s\r\n", length($res), $res));
exit(0);
}
binmode(CACHE);
while (1) {
$res = undef;
if (! $chi->Socket->connected) {
exit(0);
}
# Read the init time stamp from the top
seek(FILE, 0, 0);
my $init_data = <CACHE>;
# Seek to the last data marker
seek(CACHE, $foff, 0);
# Read until we hit EOF
for ($foff = tell(CACHE); my $data = <CACHE>; $foff = tell(CACHE)) {
$res .= $data;
}
# Clear stdio errors and seek to the last tell
seek(CACHE, $foff, 1);
seek(CACHE, $foff, 0);
# If there is a new data, display it
if ($res) {
$res .= "<script language='javascript'>self.scrollTo(0, 999999999)</script>";
$chi->Send(sprintf("%x\r\n%s\r\n", length($res), $res));
$tick = time();
$idle = 0;
}
# Send a comment as a keep alive
if ($tick + 10 < time()) {
$res = "<!-- MSF -->";
$chi->Send(sprintf("%x\r\n%s\r\n", length($res), $res));
$tick = time();
$idle++;
}
# If we have been idle for five minutes, shut down the reader
if ($idle > 30) {
$res = "<div class='textBold'>!!! Idle timeout reached, reload to start again.</div>\n";
$res .= "</pre></body></html>\n";
$chi->Send(sprintf("%x\r\n%s\r\n", length($res), $res));
exit(0);
}
# Sleep a quarter of a second to reduce CPU usage
select(undef, undef, undef, 0.25);
}
$res = "</pre></body></html>\n";
$chi->Send(sprintf("%x\r\n%s\r\n", length($res), $res));
exit(0);
}
sub PayloadList {
my $state = shift;
my $mtype = 'payloads';
my $mfilt = exists($state->{'FILTER'}) ? $state->{'FILTER'} : 'ALL';
if (! $config{'Reload'} || ! defined( $moduleList->{$mtype}->{$mfilt} )) {
$moduleList->{$mtype}->{$mfilt} = ModuleList($mtype, $mfilt);
}
return $moduleList->{$mtype}->{$mfilt};
}
sub ExploitList {
my $state = shift;
my $mtype = 'exploits';
my $mfilt = exists($state->{'FILTER'}) ? $state->{'FILTER'} : 'ALL';
if (! $config{'Reload'} || ! defined( $moduleList->{$mtype}->{$mfilt} )) {
$moduleList->{$mtype}->{$mfilt} = ModuleList($mtype, $mfilt);
}
return $moduleList->{$mtype}->{$mfilt};
}
sub ModuleList
{
my $mtype = shift;
my $mfilt = shift;
my $mList;
my @links;
my $moduleMerge = {};
my $mTypes = $moduleTypes->{$mtype};
my $mKeys = $moduleKeys->{$mtype};
my $mArch = $moduleArch->{$mtype};
my $mOS = $moduleOS->{$mtype};
my $mALL = {};
$mList = "<form action='/".uc($mtype)."' method='GET'>\n";
$mList .= "<div align='center' class='navHead'>\n";
$mList .= "<select name='FILTER' onChange='javascript:form.submit()'>\n";
$mList .= "<option value='ALL'> \n";
# List of mTypes
if ($mtype eq 'exploits' && scalar(keys %{ $mTypes } )) {
$mList .= "<option value='ALL'>--- Exploit Class ---\n";
foreach my $kname (sort( keys %{ $mTypes }) ) {
my $sel = ($mfilt eq $kname) ? 'SELECTED' : '';
$mList .= "<option value='$kname' $sel> class :: $kname\n";
$moduleMerge->{$kname} = $mTypes->{$kname};
if ($mfilt eq 'ALL') {
foreach my $mname (@{ $mTypes->{$kname} }) {
$mALL->{$mname}++;
}
}
}
}
# List of mKeys
if (scalar(keys %{ $mKeys } )) {
$mList .= "<option value='ALL'> \n";
$mList .= "<option value='ALL'>--- Application ---\n";
foreach my $kname (sort( keys %{ $mKeys }) ) {
my $sel = ($mfilt eq $kname) ? 'SELECTED' : '';
$mList .= "<option value='$kname' $sel> app :: $kname\n";
$moduleMerge->{$kname} = $mKeys->{$kname};
if ($mfilt eq 'ALL') {
foreach my $mname (@{ $mKeys->{$kname} }) {
$mALL->{$mname}++;
}
}
}
}
# List of mOS
if (scalar(keys %{ $mOS } )) {
$mList .= "<option value='ALL'> \n";
$mList .= "<option value='ALL'>--- Operating System ---\n";
foreach my $kname (sort( keys %{ $mOS }) ) {
my $sel = ($mfilt eq $kname) ? 'SELECTED' : '';
$mList .= "<option value='$kname' $sel> os :: $kname\n";
$moduleMerge->{$kname} = $mOS->{$kname};
if ($mfilt eq 'ALL') {
foreach my $mname (@{ $mOS->{$kname} }) {
$mALL->{$mname}++;
}
}
}
}
# List of mArch
if (scalar(keys %{ $mArch } )) {
$mList .= "<option value='ALL'> \n";
$mList .= "<option value='ALL'>--- Architecture ---\n";
foreach my $kname (sort( keys %{ $mArch}) ) {
my $sel = ($mfilt eq $kname) ? 'SELECTED' : '';
$mList .= "<option value='$kname' $sel> cpu :: $kname\n";
$moduleMerge->{$kname} = $mArch->{$kname};
if ($mfilt eq 'ALL') {
foreach my $mname (@{ $mArch->{$kname} }) {
$mALL->{$mname}++;
}
}
}
$mList .= "</select>\n";