Page MenuHomePhabricator
Paste P2498

gorfix.php
ActivePublic

Authored by EBernhardson on Jan 20 2016, 9:05 PM.
Project Tags
Referenced Files
F3250649: gorfix.php
Feb 24 2016, 5:27 AM
Subscribers
None
<?php
const REQUEST_PAYLOAD = '1';
$stats = array(
'filteredBulk' => 0,
'invalidRequest' => 0,
'malformedPayload' => 0,
'noContentLength' => 0,
'shortContent' => 0,
'stripExpect' => 0,
'success' => 0,
'totalPayloads' => 0,
);
function is_valid_request( $header ) {
switch ( true ) {
case substr( $header, 0, 3 ) === 'GET':
case substr( $header, 0, 4 ) === 'POST':
case substr( $header, 0, 4 ) === 'HEAD':
return true;
default:
return false;
}
}
function split_requests( $contents ) {
global $stats;
$requests = array();
$offset = 0;
$contentLen = strlen( rtrim( $contents ) );
while ( $offset < $contentLen ) {
echo ".";
$pos = strpos( $contents, "\r\n\r\n", $offset );
if ( $pos === false ) {
$stats['shortContent']++;
//var_dump( $requests, substr( $contents, $offset ), $offset );die();
return array();
}
$header = substr( $contents, $offset, $pos - $offset );
$headerLines = explode( "\r\n", $header );
$pos += 4; // the "\r\n\r\n"
$contentLength = null;
foreach ( $headerLines as $line ) {
if (preg_match( "/^Content-Length: (\d+)$/", $line, $matches )) {
$contentLength = $matches[1];
break;
}
}
if ( $contentLength === null ) {
if ( $headerLines[0] === 'GET / HTTP/1.0' ) {
// LVS uptime monitoring
break;
}
if ( end( $headerLines ) === 'User-Agent: Python-urllib/2.7' ) {
// stats monitoring
break;
}
$stats['noContentLength']++;
break;
}
if ( $headerLines[count( $headerLines ) - 1] === 'Expect: 100-continue' ) {
array_pop( $headerLines );
$stats['stripExpect']++;
}
$content = substr( $contents, $pos, $contentLength );
// sometimes we get a partial capture and the header of a second request is
// at the end of a payload for another. Throw the invalid one away but try
// and continue
if ( is_valid_request( $headerLines[0] ) ) {
$requests[] = array( $headerLines, $content );
} else {
//var_dump( $header, $content, $offset );
$stats['invalidRequest']++;
return array();
}
$offset = $pos + $contentLength;
}
echo "\n";
return $requests;
}
function handle_payload( array $lines, $output ) {
global $stats;
static $first = false;
$meta = explode( " ", array_shift( $lines ) );
$contents = implode( "", $lines );
$stats['totalPayloads']++;
if ( !is_valid_request( $contents ) ) {
$stats['malformedPayload']++;
return;
}
if ( preg_match( "|PUT /[a-z_]+/page/_bulk\?timeout=1ms HTTP/1.1|", $contents ) ) {
$stats['filteredBulk']++;
return;
}
$requests = split_requests( $contents );
foreach ( $requests as $data ) {
list( $headerLines, $content ) = $data;
if ( $first ) {
$first = false;
} else {
fwrite( $output, PAYLOAD_SEPARATOR );
}
$meta[1] = bin2hex( openssl_random_pseudo_bytes( 20 ) );
$req = implode( "\r\n", $headerLines ) . "\r\n\r\n" . $content;
fwrite( $output, implode( " ", $meta ) . $req );
if ( substr( $req, -1 ) !== "\n" ) {
fwrite( $output, "\n" );
}
$stats['success']++;
}
}
const PAYLOAD_SEPARATOR = "🐵🙈🙉\n";
$payload = array();
foreach ( glob( './*.gor' ) as $file ) {
fwrite( STDERR, "START: $file\n" );
$output = fopen( "$file.out", "w" );
foreach ( new SplFileObject( $file ) as $line ) {
if ( $line === PAYLOAD_SEPARATOR ) {
handle_payload( $payload, $output );
$payload = array();
} else {
$payload[] = $line;
}
}
if ( $payload ) {
handle_payload( $payload, $output );
$payload = array();
}
ob_start();
var_dump( $stats );
fwrite( STDERR, ob_get_clean() );
}