Practice php detection of image Trojans
- /**
- +------------------------------------------------ -------------------------------
- * Upload file upload class
- +--------------------- -------------------------------------------------- ----------------
- * @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' => 'No error occurred, The file uploaded successfully. ',
- '1' => 'The uploaded file exceeded the value of the upload_max_filesize option in php.ini',
- '2' => 'The size of the uploaded file exceeded the MAX_FILE_SIZE option in the HTML form. Specified value. ',
- '3' => 'Only part of the file was uploaded',
- '4' => 'No file uploaded',
- '5' => 'Failed to pass security check. File. ',
- '6' => 'Temp folder not found. ',
- '7' => 'File writing failed.',
- '8' => 'File type not supported',
- '9' => 'The uploaded temporary file is lost. ',
- );
-
- //@ Start file upload
- 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 upload starts
- 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;
- }
- }
-
- //@ Private hexadecimal detection hacker
- private static function checkHex() {
- if (file_exists(self::$image)) {
- $resource = fopen(self::$image, 'rb');
- $fileSize = filesize(self::$image);
- fseek($resource, 0);
- if ($fileSize > 512) { // Get the head and tail
- $hexCode = bin2hex(fread($resource, 512));
- fseek($resource, $fileSize - 512);
- $hexCode . = bin2hex(fread($resource, 512));
- } else { // Get all
- $hexCode = bin2hex(fread($resource, $fileSize));
- }
- fclose($resource);
- /* Match 16 */ in base
- /* matches */ in hexadecimal
- /* matches
in hexadecimal Copy code
|