Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@RobThree
Created December 5, 2016 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobThree/a54cbbcf636f8533db8ad2bd9b7d90fd to your computer and use it in GitHub Desktop.
Save RobThree/a54cbbcf636f8533db8ad2bd9b7d90fd to your computer and use it in GitHub Desktop.
PHP Tarpit response
<?php
$response = <<<EOD
HTTP/1.1 {{status}}
Server: {{server}}
Date: {{gmdate}}
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Page Not Found</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
line-height: 1.2;
margin: 0;
}
html {
color: #888;
display: table;
font-family: sans-serif;
height: 100%;
text-align: center;
width: 100%;
}
body {
display: table-cell;
vertical-align: middle;
margin: 2em auto;
}
h1 {
color: #555;
font-size: 2em;
font-weight: 400;
}
p {
margin: 0 auto;
width: 280px;
}
@media only screen and (max-width: 280px) {
body, p {
width: 95%;
}
h1 {
font-size: 1.5em;
margin: 0 0 0.3em;
}
}
</style>
</head>
<body>
<h1>Page Not Found</h1>
<p>Sorry, but the page you were trying to view does not exist.</p>
</body>
</html>
<!-- IE needs 512+ bytes: http://blogs.msdn.com/b/ieinternals/archive/2010/08/19/http-error-pages-in-internet-explorer.aspx -->
EOD;
Tarpit($response, 5);
function Tarpit(string $response, float $totalseconds = 30) {
// Gather all lines
$lines = preg_split('/\r|\n/', $response, null, PREG_SPLIT_NO_EMPTY);
// Calculate delay between output of each line in microseconds
$wait = ceil(($totalseconds / (sizeof($lines)-1)) * 1e6);
// We start with our output function initialized to header();
$out = function($a) { header($a); };
// Iterate all lines
foreach ($lines as $line) {
// If we encounter the doctype we switch from header-output to echo's.
if (strcasecmp($line,'<!doctype html>') === 0)
$out = function($a) { echo $a; };
// Output (using our $out function) a line, replacing some {{placeholders}} with desired values
$out(
preg_replace_callback_array([
'/{{(.*?)}}/' => function ($match) {
switch (strtolower($match[1])) {
case 'status':
return '200 OK'; // We explicitly serve as 200 OK since any decent bot will quit after receiving a 404 or any other status code and not wait for the rest of the response
case 'gmdate':
return gmdate('D, d M Y H:i:s T', time());
case 'server':
return $_SERVER['SERVER_SOFTWARE'];
default:
return null;
}
}
], $line) . "\r\n"
);
// Flush output and sleep for a while
ob_flush();
usleep($wait);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment