php檢測圖片木馬多進位編程實踐_PHP教程

來源:互聯網
上載者:User
前不久,我申請加入了某開源組織,他們要我寫一個功能用來檢測圖片中是否有木馬指令碼。
其實一開始我什麼也不知道,只是後來在網上查了一些資料,找到的全是有製作圖片木馬的教程,並沒有找到檢測的程式。

經過幾番思索之後,決定從製作原理來分析這種木馬程式。這種木馬程式是十六進位編碼寫的,我靈機一動,寫了以下這個上傳類。最終通過了組織測驗。呵呵

現在把它拿出來給大家分享,有什麼不好的地方,還請指正! anyon@139.com;
複製代碼 代碼如下:
/**
+------------------------------------------------------------------------------
* Upload 檔案上傳類
+------------------------------------------------------------------------------
* @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' => '沒有錯誤發生,檔案上傳成功。',
'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進位中的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.