note 111300 added to function.preg-match

From: Date: Mon, 04 Feb 2013 13:31:25 +0000
Subject: note 111300 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-193520@lists.php.net to get a copy of this message
Some times a Hacker use a php file or shell as a image to hack your website. so if you try to use move_uploaded_file() function as in example to allow for users to upload files, you must check if this file contains a bad codes or not so we use this function. preg match in this function we use unlink() - http://php.net/unlink after you upload file check a file with below function. <?php /** * A simple function to check file from bad codes. * * @param (string) $file - file path. * @author Yousef Ismaeil - Cliprz[at]gmail[dot]com. */ function is_clean_file ($file) { if (file_exists($file)) { $contents = file_get_contents($file); } else { exit($file." Not exists."); } if (preg_match('/(base64_|eval|system|shell_|exec|php_)/i',$contents)) { return true; } else if (preg_match("#&\#x([0-9a-f]+);#i", $contents)) { return true; } elseif (preg_match('#&\#([0-9]+);#i', $contents)) { return true; } elseif (preg_match("#([a-z]*)=([\`\'\"]*)script:#iU", $contents)) { return true; } elseif (preg_match("#([a-z]*)=([\`\'\"]*)javascript:#iU", $contents)) { return true; } elseif (preg_match("#([a-z]*)=([\'\"]*)vbscript:#iU", $contents)) { return true; } elseif (preg_match("#(<[^>]+)style=([\`\'\"]*).*expression\([^>]*>#iU", $contents)) { return true; } elseif (preg_match("#(<[^>]+)style=([\`\'\"]*).*behaviour\([^>]*>#iU", $contents)) { return true; } elseif (preg_match("#</*(applet|link|style|script|iframe|frame|frameset|html|body|title|div|p|form)[^>]*>#i", $contents)) { return true; } else { return false; } } ?> Use <?php // If image contains a bad codes $image = "simpleimage.png"; if (is_clean_file($image)) { echo "Bad codes this is not image"; unlink($image); } else { echo "This is a real image."; } ?> ---- Server IP: 69.147.83.199 Probable Submitter: 31.214.75.11 ---- Manual Page -- http://php.net/manual/en/function.preg-match.php Edit -- https://master.php.net/note/edit/111300 Del: integrated -- https://master.php.net/note/delete/111300/integrated Del: useless -- https://master.php.net/note/delete/111300/useless Del: bad code -- https://master.php.net/note/delete/111300/bad+code Del: spam -- https://master.php.net/note/delete/111300/spam Del: non-english -- https://master.php.net/note/delete/111300/non-english Del: in docs -- https://master.php.net/note/delete/111300/in+docs Del: other reasons-- https://master.php.net/note/delete/111300 Reject -- https://master.php.net/note/reject/111300 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#193520) next »