Home  >  Article  >  Backend Development  >  Php detection of image Trojans polyary programming practice_PHP tutorial

Php detection of image Trojans polyary programming practice_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:12:35813browse

Not long ago, I applied to join an open source organization, and they asked me to write a function to detect whether there is a Trojan script in the image.
Actually, I didn’t know anything at first, but later I checked some information on the Internet, and all I found were tutorials on making image Trojans, but no detection program.

After some thought, I decided to analyze this Trojan program from the perspective of its production principles. This Trojan program is written in hexadecimal encoding. I had an idea and wrote the following upload class. Finally passed the organizational test. Haha

Now take it out and share it with everyone. Please correct me if there is anything wrong! anyon@139.com;

Copy code The code is as follows:

/**
+------------------------------------------------ --------------------------------
* Upload file upload class
+------ -------------------------------------------------- -----------------------
* @package Upload
* @author Anyon
* @version $Id: Upload.class.php 2013-3-20 21:47:23 Anyon $
+-------------------------- -------------------------------------------------- --
*/
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 was uploaded successfully.',
'1' => 'The uploaded file exceeds the limit of the upload_max_filesize option in php.ini',
'2' => 'The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. value. ',
'3' => 'Only part of the file was uploaded',
'4' => 'No file uploaded',
'5' => 'Failed. File passed security check. ',
'6' => 'Temp folder not found',
'7' => 'File writing failed',
'8' => 'The file type is 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
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 in hexadecimal <% ( ) %> */
/* matches */
/* matches