35
Dynamic Code Generation Marian HackMan Marinov 22.Nov.2014 Marian HackMan Marinov Dynamic Code Generation

Dynamic code generation in Perl

Embed Size (px)

DESCRIPTION

Dynamically change the code that your application is using.

Citation preview

Page 1: Dynamic code generation in Perl

Dynamic Code Generation

Marian HackMan Marinov

22.Nov.2014

Marian HackMan Marinov Dynamic Code Generation

Page 2: Dynamic code generation in Perl

Why would you need that?

Marian HackMan Marinov Dynamic Code Generation

Page 3: Dynamic code generation in Perl

Why would you need that?

Faster Development time

Marian HackMan Marinov Dynamic Code Generation

Page 4: Dynamic code generation in Perl

Why would you need that?

Faster Development time

Easier interfaces

Marian HackMan Marinov Dynamic Code Generation

Page 5: Dynamic code generation in Perl

Why would you need that?

Faster Development time

Easier interfaces

Abstraction

Marian HackMan Marinov Dynamic Code Generation

Page 6: Dynamic code generation in Perl

Why would you need that?

Faster Development time

Easier interfaces

Abstraction

Obfuscation

Marian HackMan Marinov Dynamic Code Generation

Page 7: Dynamic code generation in Perl

Why would you need that?

Faster Development time

Easier interfaces

Abstraction

Obfuscation/crypto

Performance

Marian HackMan Marinov Dynamic Code Generation

Page 8: Dynamic code generation in Perl

I will focus mainly on PERFORMANCE

Marian HackMan Marinov Dynamic Code Generation

Page 9: Dynamic code generation in Perl

Let’s start with a basic script

#!/usr/bin/perl

use warnings;

use strict;

sub DEBUG { 0; };

use constant ADEBUG => 0;

Marian HackMan Marinov Dynamic Code Generation

Page 10: Dynamic code generation in Perl

The following would be optimised out

if (DEBUG) {

print "Debuging\n";

}

if (ADEBUG) {

print "Debuging A\n";

}

Marian HackMan Marinov Dynamic Code Generation

Page 11: Dynamic code generation in Perl

What if we need to have something like this

if (defined($ARGV[0]) && $ARGV[0] eq ’ok’) {

sub DEBUG { 1; };

}

Marian HackMan Marinov Dynamic Code Generation

Page 12: Dynamic code generation in Perl

What if we need to have something like this

if (defined($ARGV[0]) && $ARGV[0] eq ’ok’) {

sub DEBUG { 1; };

}

$ perl f.pl ok

Subroutine DEBUG redefined at f.pl line 9.

$

Marian HackMan Marinov Dynamic Code Generation

Page 13: Dynamic code generation in Perl

Or something like this

if (defined($ARGV[0]) && $ARGV[0] eq ’ok’) {

ADEBUG => 1;

}

Marian HackMan Marinov Dynamic Code Generation

Page 14: Dynamic code generation in Perl

Or something like this

if (defined($ARGV[0]) && $ARGV[0] eq ’ok’) {

ADEBUG => 1;

}

$ perl f.pl ok

Useless use of a constant ("ADEBUG") in void context at f.pl line 9.

Marian HackMan Marinov Dynamic Code Generation

Page 15: Dynamic code generation in Perl

What if we start playing dirty :)

if (defined($ARGV[0]) && $ARGV[0] eq ’ok’) {

undef &ADEBUG;

use constant ADEBUG => 1;

}

$ perl f.pl ok

Constant subroutine main::ADEBUG redefined at /usr/share/perl5/constant.pm line 140.

Constant subroutine (anonymous) undefined at f.pl line 9.

Debuging

$

Marian HackMan Marinov Dynamic Code Generation

Page 16: Dynamic code generation in Perl

Let’s declare this as futile

Figure : Resistance is futile

Marian HackMan Marinov Dynamic Code Generation

Page 17: Dynamic code generation in Perl

So why did I tried to redifine a constant ?

What if we have a script which does:

while (my $line = <$fd>) {

if ($line =~ /RE/) { };

if ($line =~ /RE/) { };

....

....

if ($line =~ /RE/) { };

}

Marian HackMan Marinov Dynamic Code Generation

Page 18: Dynamic code generation in Perl

So why did I tried to redifine a constant ?

We can update it to:

while (my $line = <$fd>) {

if ($config{’type1’} && $line =~ /RE/) { };

if ($config{’type2’} && $line =~ /RE/) { };

....

....

if ($config{’typeN’} && $line =~ /RE/) { };

}

Marian HackMan Marinov Dynamic Code Generation

Page 19: Dynamic code generation in Perl

Hook::Filter

First looked at this

use Hook::Filter::RulePool qw(get_rule_pool);

get_rule_pool->add_rule("subname eq ’mydebug’ &&

from =~ /^My::Filthy::Attempt/");

->add_rule("from =~ /^My::Filthy::Attempt::func$/");

->add_rule("args(1) =~ /bob/");

Marian HackMan Marinov Dynamic Code Generation

Page 20: Dynamic code generation in Perl

Welcome to perlfilter

Preprocessor like directives for Perl

Marian HackMan Marinov Dynamic Code Generation

Page 21: Dynamic code generation in Perl

perlfilter

Filter::Util::Call - the main filter module

Marian HackMan Marinov Dynamic Code Generation

Page 22: Dynamic code generation in Perl

perlfilter

Filter::Util::Call - the main filter module

Filter::Simple - simplified version of the Util::Call

Marian HackMan Marinov Dynamic Code Generation

Page 23: Dynamic code generation in Perl

perlfilter

Filter::Util::Call - the main filter module

Filter::Simple - simplified version of the Util::Call

Filter::sh - filter the source trough cmd with shell

Marian HackMan Marinov Dynamic Code Generation

Page 24: Dynamic code generation in Perl

perlfilter

Filter::Util::Call - the main filter module

Filter::Simple - simplified version of the Util::Call

Filter::sh - filter the source trough cmd with shell

Filter::exec - filter the source trough cmd

Marian HackMan Marinov Dynamic Code Generation

Page 25: Dynamic code generation in Perl

perlfilter

Filter::Util::Call - the main filter module

Filter::Simple - simplified version of the Util::Call

Filter::sh - filter the source trough cmd with shell

Filter::exec - filter the source trough cmd

Filter::tee - copies all text from the line after the use

Marian HackMan Marinov Dynamic Code Generation

Page 26: Dynamic code generation in Perl

perlfilter

Filter::Util::Call - the main filter module

Filter::Simple - simplified version of the Util::Call

Filter::sh - filter the source trough cmd with shell

Filter::exec - filter the source trough cmd

Filter::tee - copies all text from the line after the use

Filter::cpp - pipes all souce trough the cpp pre-processor

Marian HackMan Marinov Dynamic Code Generation

Page 27: Dynamic code generation in Perl

Crypto filters

Filter::decrypt - example documentation for enc/decrypting

Marian HackMan Marinov Dynamic Code Generation

Page 28: Dynamic code generation in Perl

Crypto filters

Filter::decrypt - example documentation for enc/decrypting

Filter::Rijndael - decrypting source filter based on Rijndaelencryption

Marian HackMan Marinov Dynamic Code Generation

Page 29: Dynamic code generation in Perl

Crypto filters

Filter::decrypt - example documentation for enc/decrypting

Filter::Rijndael - decrypting source filter based on Rijndaelencryption

Filter::CBC - Source filter for Cipher Block Chaining

Marian HackMan Marinov Dynamic Code Generation

Page 30: Dynamic code generation in Perl

Crypto filters

Filter::decrypt - example documentation for enc/decrypting

Filter::Rijndael - decrypting source filter based on Rijndaelencryption

Filter::CBC - Source filter for Cipher Block Chaining

Filter::Crypto::CryptFile & Filter::Crypto::Decrypt

Marian HackMan Marinov Dynamic Code Generation

Page 31: Dynamic code generation in Perl

Filter::Util::Call part I

package Debug;

use strict;

use warnings;

use Filter::Util::Call;

use constant TRUE => 1;

use constant FALSE => 0;

sub import {

my ($type) = @_;

my (%context) = (

Enabled => defined $ENV{DEBUG},

InTraceBlock => FALSE,

Filename => (caller)[1],

LineNo => 0,

LastBegin => 0,

);

filter_add(bless \%context);

}Marian HackMan Marinov Dynamic Code Generation

Page 32: Dynamic code generation in Perl

Filter::Util::Call part II

sub filter {

my ($self) = @_;

my ($status);

$status = filter_read();

++ $self->{LineNo};

if ($self->{InTraceBlock}) {

if (/^\s*##\s*DEBUG_END/) {

$self->{InTraceBlock} = FALSE;

}

s/^/#/ if ! $self->{Enabled};

} elsif ( /^\s*##\s*DEBUG_BEGIN/ ) {

$self->{InTraceBlock} = TRUE;

$self->{LastBegin} = $self->{LineNo};

}

return $status;

}

1;Marian HackMan Marinov Dynamic Code Generation

Page 33: Dynamic code generation in Perl

Filter::Simple

package BANG;

use Filter::Simple;

FILTER {

s/BANG\s+BANG/die ’BANG’ if \$BANG/g;

};

1;

Marian HackMan Marinov Dynamic Code Generation

Page 34: Dynamic code generation in Perl

Filter::cpp

use Filter::cpp;

#define FRED 1

$a = 2 + FRED;

print "a = $a\n";

#ifdef FRED

print "Hello FRED\n";

#else

print "Where is FRED\n";

#endif

Marian HackMan Marinov Dynamic Code Generation

Page 35: Dynamic code generation in Perl

Thank you

Questions ?

Additional readinghttp://www.bytelabs.org/pub/papers/hburg07.pdf

Contact me:

Marian HackMan Marinov

mm [maybe at] 1h.com

icq: 7556201

irc: irc.freenode.net HackMan

jabber: [email protected]

Marian HackMan Marinov Dynamic Code Generation