| 1 | package Sparrow::Commands::TaskBox; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use base 'Exporter'; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use JSON; | ||
| 9 | use YAML; | ||
| 10 | use Sparrow::Commands::Project; | ||
| 11 | use Sparrow::Commands::Task; | ||
| 12 | use Sparrow::Commands::Plugin; | ||
| 13 | use Sparrow::Constants; | ||
| 14 | |||
| 15 | our @EXPORT = qw{ | ||
| 16 | |||
| 17 | box_run | ||
| 18 | |||
| 19 | }; | ||
| 20 | |||
| 21 | sub box_run { | ||
| 22 | |||
| 23 | my $path = shift or confess 'usage box_run(path, opts)'; | ||
| 24 | |||
| 25 | my %opts = @_; | ||
| 26 | |||
| 27 | my $quiet_mode = ( $opts{'--mode'} && $opts{'--mode'} eq 'quiet' ) ? 1 : 0; | ||
| 28 | |||
| 29 | delete $opts{'--mode'}; | ||
| 30 | |||
| 31 | open TXBSPEC, $path or confess "can't open file $path to read: $!"; | ||
| 32 | |||
| 33 | my $spec_str = join "", <TXBSPEC>; | ||
| 34 | |||
| 35 | close TXBSPEC; | ||
| 36 | |||
| 37 | my $tasklist = []; | ||
| 38 | |||
| 39 | if ($path=~/\.(yaml|yml)$/){ | ||
| 40 | $tasklist = Load($spec_str); | ||
| 41 | } elsif ($path=~/\.(json|js)$/) { | ||
| 42 | $tasklist = decode_json($spec_str); | ||
| 43 | } else { | ||
| 44 | die "unsupported task box spec format: $path"; | ||
| 45 | } | ||
| 46 | |||
| 47 | my %plg_seen; | ||
| 48 | |||
| 49 | for my $task (@{$tasklist}){ | ||
| 50 | install_plugin($task->{plugin}) unless $plg_seen{$task->{plugin}}++; | ||
| 51 | } | ||
| 52 | |||
| 53 | project_remove('taskbox', { quiet => $quiet_mode }); | ||
| 54 | |||
| 55 | project_create('taskbox', { quiet => $quiet_mode }); | ||
| 56 | |||
| 57 | |||
| 58 | my $i=0; | ||
| 59 | |||
| 60 | my %task_seen; | ||
| 61 | |||
| 62 | for my $task (@{$tasklist}) { | ||
| 63 | |||
| 64 | $i++; | ||
| 65 | |||
| 66 | my $path = sparrow_root()."/cache/task_$i.json"; | ||
| 67 | |||
| 68 | open JSON, ">", $path or confess "can't open file $path to read: $!"; | ||
| 69 | |||
| 70 | print JSON encode_json($task->{data}||{}); | ||
| 71 | |||
| 72 | close JSON; | ||
| 73 | |||
| 74 | (my $safe_task_name = $task->{task})=~s/[^\w_-]/-/g; | ||
| 75 | |||
| 76 | $safe_task_name.="_".$i if $task_seen{$safe_task_name}++; | ||
| 77 | |||
| 78 | if ($task->{type} eq 'swat'){ | ||
| 79 | task_add('taskbox', $safe_task_name,$task->{plugin}, '--quiet', $quiet_mode, 'task_desc' , $task->{task}, '--host', $task->{host} ); | ||
| 80 | } else { | ||
| 81 | task_add('taskbox', $safe_task_name,$task->{plugin}, '--quiet', $quiet_mode, 'task_desc' , $task->{task} ); | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | $i=0; | ||
| 89 | |||
| 90 | print "running task box from $path ... \n"; | ||
| 91 | |||
| 92 | for my $task (@{$tasklist}){ | ||
| 93 | |||
| 94 | $i++; | ||
| 95 | |||
| 96 | my $path = sparrow_root()."/cache/task_$i.json"; | ||
| 97 | |||
| 98 | (my $safe_task_name = $task->{task})=~s/[^\w_-]/-/g; | ||
| 99 | |||
| 100 | #print "task_run $safe_task_name, $path\n"; | ||
| 101 | |||
| 102 | if ($task->{type} eq 'swat'){ | ||
| 103 | task_run('taskbox',$safe_task_name,'--no-exec'); | ||
| 104 | } else { | ||
| 105 | task_run('taskbox',$safe_task_name,'--no-exec', '--json', $path, %opts ); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | } | ||
| 111 | |||
| 112 | 1; | ||
| 113 |