Advertisement
Guest User

CVE-2012-1823 mitigation wrapper

a guest
May 4th, 2012
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.15 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. use File::Copy;
  7.  
  8. my $gcc = '/usr/bin/gcc';
  9.  
  10. my $tempfile = "/var/tmp/$$.c";
  11.  
  12. my $c = <<CPROG;
  13. /*
  14.  * Small wrapper which strips all arguments to invocations
  15.  * of php-cgi when it is called as a normal CGI handler.
  16.  * This prevents attackers to pass arguments from the query
  17.  * string as defined in RFC 3875. [1]
  18.  *
  19.  * [1] http://www.ietf.org/rfc/rfc3875
  20.  *
  21.  */
  22.  
  23. #include <sys/socket.h>
  24. #include <sys/un.h>
  25. #include <netinet/in.h>
  26.  
  27. #include <unistd.h>
  28. #include <errno.h>
  29.  
  30. #define PHP_ORIG "REALPATH" /* Original binary */
  31.  
  32. typedef union _sa_t {
  33.     struct sockaddr     sa;
  34.     struct sockaddr_un  sa_unix;
  35.     struct sockaddr_in  sa_inet;
  36.     /* struct sockaddr_in6 should probably be here as well,
  37.      * doesn't matter though, since struct sockaddr_un
  38.      * is big.
  39.      */
  40. } sa_t;
  41.  
  42. int is_fastcgi(void)
  43. {
  44.     sa_t sa;
  45.     socklen_t len = sizeof(sa);
  46.  
  47.     return ( getpeername(0, (struct sockaddr *)&sa, &len) != 0 &&
  48.              errno == ENOTCONN );
  49. }
  50.  
  51. int main(int argc, char **argv)
  52. {
  53.     /* mimic php's cgi detection */
  54.     if ( !is_fastcgi() &&
  55.          (getenv("SERVER_SOFTWARE") ||
  56.           getenv("SERVER_NAME") ||
  57.           getenv("GATEWAY_INTERFACE") ||
  58.           getenv("REQUEST_METHOD") ) )
  59.       argv[1] = NULL;
  60.  
  61.     execv(PHP_ORIG, argv);
  62. }
  63.  
  64. CPROG
  65.  
  66. #print $c;
  67.  
  68. while (<STDIN>) {
  69.     chomp;
  70.     my $orig = $_;
  71.     my $real = "${orig}.real";
  72.     if ( -e $orig ) {
  73.         my $prog = $c;
  74.         $prog =~ s,REALPATH,$real,gs;
  75.         if ( move( $orig, $real ) ) {
  76.             open my $f, '>', $tempfile;
  77.             print $f $prog;
  78.             close $f;
  79.         my $fh;
  80.             if ( open( $fh, '-|', $gcc, '-o', $orig, $tempfile ) ) {
  81.  
  82.                 #fine
  83.             }
  84.             else {
  85.                 print STDERR "problem compiling $orig, moving on.\n";
  86.                 next;
  87.             }
  88.             close $fh;
  89.         }
  90.         else {
  91.             print STDERR "problem moving $orig, moving on.\n";
  92.             next;
  93.         }
  94.     }
  95.     else {
  96.         print STDERR "$orig does not exist, moving on.\n";
  97.         next;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement