source: repository/lib/Metabrik/String/Javascript.pm

Last change on this file was 901:246044148483, checked in by GomoR <gomor@…>, 5 months ago
  • bugfix: client::elasticsearch::query: use precision_threshold to 100% when counting unique values in unique_term Command
  • new: client::imap: reset_current, read_next_with_an_attachment and save_attachments Commands
  • new: client::smbclient: download_in_background Command
  • bugfix: client::smbclient: download Command to better parse Arguments to easily download files by specifying a full remote Windows path
  • new: email::message: save_attachments Command
  • new: file::compress: handle bzip2 compress/uncompress
  • new: forensic::pcap: show_sessions Command
  • new: forensic::sysmon: use_regex_match Attribute (disables by default)
  • new: forensic::sysmon: clean_ps_from_list Command to accept an optional Argument to only compare from specified sources
  • bugfix: format::latex: copy style file in current directory before compiling Latex
  • update: remote::sandbox: no more imap stuff here.
  • new: remote::sandbox: use_regex_match Attribute, diff_ps_target_filename_created, loop_and_download_created_files and memdump_as_volatility Commands
  • bugfix: xorg::xrandr: added set_output_resolution alias
File size: 2.6 KB
Line 
1#
2# $Id$
3#
4# string::javascript Brik
5#
6package Metabrik::String::Javascript;
7use strict;
8use warnings;
9
10use base qw(Metabrik::Shell::Command Metabrik::System::Package);
11
12sub 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      },
30   };
31}
32
33sub eval {
34   my $self = shift;
35   my ($js) = @_;
36
37   $self->brik_help_run_undef_arg('eval', $js) or return;
38
39   $@ = undef;
40
41   my $context = JavaScript::V8::Context->new;
42   if (defined($@) && ! defined($context)) {
43      chomp($@);
44      return $self->log->error("eval: cannot init V8 context: $@");
45   }
46
47   my $r = $context->eval($js);
48   if (defined($@) && ! defined($r)) {
49      chomp($@);
50      return $self->log->error("eval: cannot eval JS: $@");
51   }
52
53   return $r;
54}
55
56sub deobfuscate {
57   my $self = shift;
58   my ($js) = @_;
59
60   $self->brik_help_run_undef_arg('deobfuscate', $js) or return;
61
62   $@ = undef;
63
64   my $context = JavaScript::V8::Context->new;
65   if (defined($@) && ! defined($context)) {
66      chomp($@);
67      return $self->log->error("deobfuscate: cannot init V8 context: $@");
68   }
69
70   my $buf = '';
71   $context->bind_function(eval => sub { $buf .= join(' ', @_); });
72   $context->bind_function(alert => sub { $buf .= join(' ', @_); });
73   $context->bind_function(ActiveXObject => sub { $buf .= join(' ', @_); });
74   $context->bind_function(WScript => sub { $buf .= join(' ', @_); });
75   # Google V8 has no XMLHttpRequest nor network related functions.
76   # We should replace it by NodeJS at some point.
77   $context->bind_function(XMLHttpRequest => sub { $buf .= join(' ', @_); });
78
79   my $r = $context->eval($js);
80   if (defined($@) && ! defined($r)) {
81      chomp($@);
82      return $self->log->error("deobfuscate: cannot eval JS: $@");
83   }
84
85   my $b = JavaScript::Beautifier::js_beautify($buf, {
86      indent_size => 2,
87      indent_character => ' ',
88   });
89
90   return $b;
91}
92
931;
94
95__END__
96
97=head1 NAME
98
99Metabrik::String::Javascript - string::javascript Brik
100
101=head1 COPYRIGHT AND LICENSE
102
103Copyright (c) 2014-2017, Patrice E<lt>GomoRE<gt> Auffret
104
105You may distribute this module under the terms of The BSD 3-Clause License.
106See LICENSE file in the source distribution archive.
107
108=head1 AUTHOR
109
110Patrice E<lt>GomoRE<gt> Auffret
111
112=cut
Note: See TracBrowser for help on using the repository browser.