實踐php檢測圖片木馬
實踐php檢測圖片木馬
<?php
/** +------------------------------------------------------------------------------* Upload 文件上傳類 +------------------------------------------------------------------------------ * @package Upload * @author nicegy * @version $Id: Upload.class.php 2014-4-11 19:00:23 nicegy $ +------------------------------------------------------------------------------ */ class Upload { private static $image = null; private static $status = 0; private static $suffix = null; private static $imageType = array('.jpg', '.bmp','.gif','.png'); private static $message = array( '0' => '沒有錯誤發生,文件上傳成功。', '1' => '上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值。', '2' => '上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值。', '3' => '文件只有部分被上傳。', '4' => '沒有文件上傳。', '5' => '未能通過安全檢查的文件。', '6' => '找不到臨時文件夾。', '7' => '文件寫入失敗。', '8' => '文件類型不支持', '9' => '上傳的臨時文件丟失。', ); //@ 開始執行文件上傳 public static function start($feild = 'file') { if (!empty($_FILES)) { self::$status = $_FILES[$feild]['error']; if (self::$status > 0) return array('status' => self::$status, 'msg' => self::$message[self::$status]); self::$image = $_FILES[$feild]['tmp_name']; self::$suffix = strtolower(strrchr($_FILES[$feild]['name'], '.')); return array('status' => self::_upload(), 'path' => self::$image, 'msg' => self::$message[self::$status]); } else { return array('status' => self::$status, 'msg' => self::$message[self::$status]); } } //@ 私有 上傳開始 private static function _upload($path = './upload/') { date_default_timezone_set('PRC'); $newFile = $path . date('Y/m/d/His') . rand(100, 999) . self::$suffix; self::umkdir(dirname($newFile)); if (is_uploaded_file(self::$image) && move_uploaded_file(self::$image, $newFile)) { self::$image = $newFile; if (in_array(self::$suffix, self::$imageType)) return self::checkHex(); else return self::$status = 0; } else { return self::$status = 9; } } //@ 私有 16進制檢測 黑客 private static function checkHex() { if (file_exists(self::$image)) { $resource = fopen(self::$image, 'rb'); $fileSize = filesize(self::$image); fseek($resource, 0); if ($fileSize > 512) { // 取頭和尾 $hexCode = bin2hex(fread($resource, 512)); fseek($resource, $fileSize - 512); $hexCode .= bin2hex(fread($resource, 512)); } else { // 取全部 $hexCode = bin2hex(fread($resource, $fileSize)); } fclose($resource); /* 匹配16進制中的 */ /* 匹配16進制中的 */ /* 匹配16進制中的 </pre>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!