当前位置:Gxlcms > PHP教程 > php检测图片木马

php检测图片木马

时间:2021-07-01 10:21:17 帮助过:34人阅读

实践php检测图片木马
  1. /**
  2. +------------------------------------------------------------------------------
  3. * Upload 文件上传类
  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' => '没有错误发生,文件上传成功。',
  17. '1' => '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。',
  18. '2' => '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。',
  19. '3' => '文件只有部分被上传。',
  20. '4' => '没有文件上传。',
  21. '5' => '未能通过安全检查的文件。',
  22. '6' => '找不到临时文件夹。',
  23. '7' => '文件写入失败。',
  24. '8' => '文件类型不支持',
  25. '9' => '上传的临时文件丢失。',
  26. );
  27. //@ 开始执行文件上传
  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. //@ 私有 上传开始
  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. //@ 私有 16进制检测 黑客
  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) { // 取头和尾
  62. $hexCode = bin2hex(fread($resource, 512));
  63. fseek($resource, $fileSize - 512);
  64. $hexCode .= bin2hex(fread($resource, 512));
  65. } else { // 取全部
  66. $hexCode = bin2hex(fread($resource, $fileSize));
  67. }
  68. fclose($resource);
  69. /* 匹配16进制中的 */
  70. /* 匹配16进制中的 */
  71. /* 匹配16进制中的

php

人气教程排行