PHP detection Picture Trojan

Source: Internet
Author: User
Tags fread html form
Practice PHP Detection Picture Trojan
  1. /**
  2. +------------------------------------------------------------------------------
  3. * Upload File Upload class
  4. +------------------------------------------------------------------------------
  5. * @package Upload
  6. * @author Nicegy
  7. * @version $Id: Upload.class.php 2014-4-11 19:00:23 Nicegy $
  8. +------------------------------------------------------------------------------
  9. */
  10. Class Upload {
  11. private static $image = null;
  12. private static $status = 0;
  13. private static $suffix = null;
  14. private static $imageType = Array ('. jpg ', '. bmp ', '. gif ', '. png ');
  15. private static $message = Array (
  16. ' 0 ' = ' no error occurred and the file upload was successful. ',
  17. ' 1 ' + ' uploaded file exceeded the upload_max_filesize option limit in php.ini. ',
  18. ' 2 ' + ' the size of the uploaded file exceeds the value specified by the Max_file_size option in the HTML form. ',
  19. ' 3 ' + ' files are only partially uploaded. ',
  20. ' 4 ' = ' no file upload. ',
  21. ' 5 ' = ' Failed to pass security check file. ',
  22. ' 6 ' = ' cannot find the Temp folder. ',
  23. ' 7 ' = ' file failed to write. ',
  24. ' 8 ' = ' file type not supported ',
  25. ' 9 ' = ' upload temporary files are missing. ',
  26. );
  27. @ Start performing file uploads
  28. public static function Start ($feild = ' file ') {
  29. if (!empty ($_files)) {
  30. Self:: $status = $_files[$feild] [' ERROR '];
  31. if (self:: $status > 0)
  32. Return array (' status ' = + Self:: $status, ' msg ' + ' self:: $message [Self:: $status]);
  33. Self:: $image = $_files[$feild] [' tmp_name '];
  34. Self:: $suffix = Strtolower (STRRCHR ($_files[$feild [' name '], '. '));
  35. Return array (' status ' = = Self::_upload (), ' path ' = + Self:: $image, ' msg ' = ' self:: $message [Self:: $status]);
  36. } else {
  37. Return array (' status ' = + Self:: $status, ' msg ' + ' self:: $message [Self:: $status]);
  38. }
  39. }
  40. @ Private Upload Start
  41. private static function _upload ($path = './upload/') {
  42. Date_default_timezone_set (' PRC ');
  43. $newFile = $path. Date (' Y/m/d/his '). RAND (100, 999). Self:: $suffix;
  44. Self::umkdir (DirName ($newFile));
  45. if (Is_uploaded_file (self:: $image) && move_uploaded_file (self:: $image, $newFile)) {
  46. Self:: $image = $newFile;
  47. if (In_array (self:: $suffix, Self:: $imageType))
  48. return Self::checkhex ();
  49. Else
  50. Return self:: $status = 0;
  51. } else {
  52. Return self:: $status = 9;
  53. }
  54. }
  55. @ Private 16 binary detection hack
  56. private static function Checkhex () {
  57. if (File_exists (self:: $image)) {
  58. $resource = fopen (self:: $image, ' RB ');
  59. $fileSize = FileSize (self:: $image);
  60. Fseek ($resource, 0);
  61. if ($fileSize > 512) {//Pick head and tail
  62. $hexCode = Bin2Hex (Fread ($resource, 512));
  63. Fseek ($resource, $fileSize-512);
  64. $hexCode. = Bin2Hex (Fread ($resource, 512));
  65. } else {//Fetch all
  66. $hexCode = Bin2Hex (Fread ($resource, $fileSize));
  67. }
  68. Fclose ($resource);
  69. /* Match 16 in the binary */
  70. /* Match 16 in the binary */
  71. /* Match 16 in the binary
Copy Code
Php
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.