String/Javascript.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« String/Ini.pm ↑ Summary String/Json.pm »
1 #
2 # $Id$
3 #
4 # string::javascript Brik
5 #
6 package Metabrik::String::Javascript;
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use base qw(Metabrik::Shell::Command Metabrik::System::Package);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 commands => {
19 eval => [ qw(js_code) ],
20 deobfuscate => [ qw(js_code) ],
21 },
22 require_modules => {
23 'JavaScript::V8' => [ ],
24 'JavaScript::Beautifier' => [ qw(js_beautify) ],
25 },
26 need_packages => {
27 ubuntu => [ qw(libv8-dev) ],
28 debian => [ qw(libv8-dev) ],
29 kali => [ qw(libv8-dev) ],
30 },
31 };
32 }
33  
34 sub eval {
35 my $self = shift;
36 my ($js) = @_;
37  
38 $self->brik_help_run_undef_arg('eval', $js) or return;
39  
40 $@ = undef;
41  
42 my $context = JavaScript::V8::Context->new;
43 if (defined($@) && ! defined($context)) {
44 chomp($@);
45 return $self->log->error("eval: cannot init V8 context: $@");
46 }
47  
48 my $r = $context->eval($js);
49 if (defined($@) && ! defined($r)) {
50 chomp($@);
51 return $self->log->error("eval: cannot eval JS: $@");
52 }
53  
54 return $r;
55 }
56  
57 sub deobfuscate {
58 my $self = shift;
59 my ($js) = @_;
60  
61 $self->brik_help_run_undef_arg('deobfuscate', $js) or return;
62  
63 $@ = undef;
64  
65 my $context = JavaScript::V8::Context->new;
66 if (defined($@) && ! defined($context)) {
67 chomp($@);
68 return $self->log->error("deobfuscate: cannot init V8 context: $@");
69 }
70  
71 my $buf = '';
72 $context->bind_function(eval => sub { $buf .= join(' ', @_); });
73 $context->bind_function(alert => sub { $buf .= join(' ', @_); });
74 $context->bind_function(ActiveXObject => sub { $buf .= join(' ', @_); });
75 $context->bind_function(WScript => sub { $buf .= join(' ', @_); });
76 # Google V8 has no XMLHttpRequest nor network related functions.
77 # We should replace it by NodeJS at some point.
78 $context->bind_function(XMLHttpRequest => sub { $buf .= join(' ', @_); });
79  
80 my $r = $context->eval($js);
81 if (defined($@) && ! defined($r)) {
82 chomp($@);
83 return $self->log->error("deobfuscate: cannot eval JS: $@");
84 }
85  
86 my $b = JavaScript::Beautifier::js_beautify($buf, {
87 indent_size => 2,
88 indent_character => ' ',
89 });
90  
91 return $b;
92 }
93  
94 1;
95  
96 __END__
« String/Ini.pm ↑ Summary String/Json.pm »