Php multi-system programming for detecting image Trojans _ PHP Tutorial

Source: Internet
Author: User
Php multi-base programming practices for detecting image Trojans. Not long ago, I applied to join an open-source organization. They asked me to write a function to check whether there was a Trojan script in the image. In fact, I didn't know anything at the beginning, but recently I applied to join an open-source organization. They asked me to write a function to check whether there was a Trojan script in the image.
In fact, I didn't know anything at first, but I checked some information on the internet and found all the tutorials for creating image Trojans. I didn't find any programs for detection.

After several thoughts, I decided to analyze this Trojan program from the production principle. This kind of Trojan program was written in hexadecimal code. I had the chance to write the following Upload class. Finally, the organization quiz was passed. Haha

Now I will share it with you. please correct me if there is anything bad! Anyon@139.com;

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 is 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. ',
'3' => 'only part of the file is uploaded. ',
'4' => 'no file is uploaded. ',
'5' => 'indicates the file that failed the security check. ',
'6' => 'cannot find the temporary folder. ',
'7' => 'file writing failed. ',
'8' => 'File type not supported ',
'9' => 'the temporary file to be uploaded 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]);
}
}
// @ Start private Upload
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 check
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 header and tail
$ HexCode = bin2hex (fread ($ resource, 512 ));
Fseek ($ resource, $ fileSize-512 );
$ HexCode. = bin2hex (fread ($ resource, 512 ));
} Else {// retrieve all
$ HexCode = bin2hex (fread ($ resource, $ fileSize ));
}
Fclose ($ resource );
/* Match <% () %> */
/* Match in hexadecimal notation */
/* Match in hexadecimal notation

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.