Security issues in Perl web apps

       Viacheslav Tykhanovskyi


            May 12, 2012
Common web security issues
Validate input data!
SQL injections



     use   DBI;
     use   DBIx::Class;
     use   Rose::DB::Object;
     use   ObjectDB;
XSS


 Blind escaping <, >, ’, " and & is not enough!
XSS


 Blind escaping <, >, ’, " and & is not enough!
      Various HTML attributes (href, refresh meta
      tag, ...)
XSS


 Blind escaping <, >, ’, " and & is not enough!
      Various HTML attributes (href, refresh meta
      tag, ...)
      Not validated JSON response
XSS


 Blind escaping <, >, ’, " and & is not enough!
      Various HTML attributes (href, refresh meta
      tag, ...)
      Not validated JSON response
      Using template variables in JavaScript code
XSS


 Blind escaping <, >, ’, " and & is not enough!
      Various HTML attributes (href, refresh meta
      tag, ...)
      Not validated JSON response
      Using template variables in JavaScript code
 Escape taking context into account.
Cookies



     Sign cookies
Cookies



     Sign cookies
     XSS preventing is hard. Set HttpOnly cookie
     flag for better protection.
CSRF




       Plack::Middleware::CSRF
Path traversal



      ../../../../../../etc/passwd
Path traversal



      ../../../../../../etc/passwd


     Detect ..
Path traversal



      ../../../../../../etc/passwd


     Detect ..
     File::Spec->no_upwards(@paths);
Perl-specific security issues
No buffer overflow
No buffer overflow
Most system commands are embedded
No buffer overflow
Most system commands are embedded
Written by smart people
use strict;
use warnings;
Tainting




     -T
sub is_tainted {
    return !eval {
        eval("#"
            . substr(join("", @_), 0, 0)
        );
        1
    };
}
system()


       system("program $arg");


  vs

       system(’program’, $arg);
open()


       open my $fh, ">$file";


  vs

       open my $fh, ’>’, $file;
eval()



         eval "require $class";
eval()



         eval "require $class";


         load_class("Foo;print ’nice feature!’")
0


    0
0


    0


    $file = "/bin/ls0 /etc|";
    if (-e $file) {
        open my $fh, $file;
    }
CGI & ARGV


    script.pl?foo
CGI & ARGV


    script.pl?foo


    ...
    $app->run(@ARGV);
    ...
Regular expressions

  if ($string =~ m/$user_supplied_re/) {
      ...
  }


  vs

  if ($string =~ m/Q$user_supplied_reE/) {
      ...
  }
Unicode


       utf8


  vs

       UTF-8
rand()




  ”rand()” is not cryptographically secure
How to make life easier?
How to make life easier?
   Use modules from CPAN. Many of them are
   time-proved
How to make life easier?
   Use modules from CPAN. Many of them are
   time-proved
   Google ”OWASP”
How to make life easier?
   Use modules from CPAN. Many of them are
   time-proved
   Google ”OWASP”
   Follow Best Practices
How to make life easier?
   Use modules from CPAN. Many of them are
   time-proved
   Google ”OWASP”
   Follow Best Practices
   Use scanners
        nikto http://cirt.net/nikto2
        skipfish http://code.google.com/p/skipfish/
        w3af http://w3af.sourceforge.net/
Questions?

Slides

  • 1.
    Security issues inPerl web apps Viacheslav Tykhanovskyi May 12, 2012
  • 3.
  • 4.
  • 5.
    SQL injections use DBI; use DBIx::Class; use Rose::DB::Object; use ObjectDB;
  • 6.
    XSS Blind escaping<, >, ’, " and & is not enough!
  • 7.
    XSS Blind escaping<, >, ’, " and & is not enough! Various HTML attributes (href, refresh meta tag, ...)
  • 8.
    XSS Blind escaping<, >, ’, " and & is not enough! Various HTML attributes (href, refresh meta tag, ...) Not validated JSON response
  • 9.
    XSS Blind escaping<, >, ’, " and & is not enough! Various HTML attributes (href, refresh meta tag, ...) Not validated JSON response Using template variables in JavaScript code
  • 10.
    XSS Blind escaping<, >, ’, " and & is not enough! Various HTML attributes (href, refresh meta tag, ...) Not validated JSON response Using template variables in JavaScript code Escape taking context into account.
  • 11.
    Cookies Sign cookies
  • 12.
    Cookies Sign cookies XSS preventing is hard. Set HttpOnly cookie flag for better protection.
  • 13.
    CSRF Plack::Middleware::CSRF
  • 14.
    Path traversal ../../../../../../etc/passwd
  • 15.
    Path traversal ../../../../../../etc/passwd Detect ..
  • 16.
    Path traversal ../../../../../../etc/passwd Detect .. File::Spec->no_upwards(@paths);
  • 17.
  • 18.
  • 19.
    No buffer overflow Mostsystem commands are embedded
  • 20.
    No buffer overflow Mostsystem commands are embedded Written by smart people
  • 22.
  • 23.
  • 24.
    sub is_tainted { return !eval { eval("#" . substr(join("", @_), 0, 0) ); 1 }; }
  • 25.
    system() system("program $arg"); vs system(’program’, $arg);
  • 26.
    open() open my $fh, ">$file"; vs open my $fh, ’>’, $file;
  • 27.
    eval() eval "require $class";
  • 28.
    eval() eval "require $class"; load_class("Foo;print ’nice feature!’")
  • 29.
    0 0
  • 30.
    0 0 $file = "/bin/ls0 /etc|"; if (-e $file) { open my $fh, $file; }
  • 31.
    CGI & ARGV script.pl?foo
  • 32.
    CGI & ARGV script.pl?foo ... $app->run(@ARGV); ...
  • 33.
    Regular expressions if ($string =~ m/$user_supplied_re/) { ... } vs if ($string =~ m/Q$user_supplied_reE/) { ... }
  • 34.
    Unicode utf8 vs UTF-8
  • 35.
    rand() ”rand()”is not cryptographically secure
  • 36.
    How to makelife easier?
  • 37.
    How to makelife easier? Use modules from CPAN. Many of them are time-proved
  • 38.
    How to makelife easier? Use modules from CPAN. Many of them are time-proved Google ”OWASP”
  • 39.
    How to makelife easier? Use modules from CPAN. Many of them are time-proved Google ”OWASP” Follow Best Practices
  • 40.
    How to makelife easier? Use modules from CPAN. Many of them are time-proved Google ”OWASP” Follow Best Practices Use scanners nikto http://cirt.net/nikto2 skipfish http://code.google.com/p/skipfish/ w3af http://w3af.sourceforge.net/
  • 41.