PHP Safety (2) PHP Safety (2) Original: John Coggeshall 08/28/2003 Original: http://www.onlamp.com/pub/A/php/2003/08/28/php_foundations.html Welcome to PHP Foundations. In my last article, I introduced you to the practice of practicing security in PHP, and continue to develop a series of articles in the development of good PHP programming habits. This article will continue our discussion with more potential security vulnerabilities and instances that fix their tools and methods. Today I will start talking about a very serious potential security vulnerability in PHP development - a program that writes the underlying operating system call. Performing a system call in PHP In PHP, there are many ways to perform system calls. For example, System (), Exec (), PASSTHRU (), POPEN (), and back single quotes (`) operators allow you to perform system calls in our program. If you use these functions, these functions will open the door to the malicious user on your server. When visiting the file, in most cases, security vulnerabilities occurred in system commands caused by unreliable external inputs. Using an example of the system call considers a program processed uploaded by an HTTP file, which uses the Zip program to compress file, then move it to the specified directory (default to / usr / local / archives /).
The code is as follows: Php $ zip = "/ usr / bin / zip"; $ store_path = "/ usr / local / archives /"; if (isset ($ _ files [file '])) {$ tmp_name = $ _files ['file'] ['TMP_NAME']; $ cmp_name = DIRNAME ($ _ files ['file'] ['TMP_NAME']). "/ }[files['file']['name']tage.zip"; $ filename = basename ($ cmp_name); if (file_exists ($ tmp_name)) {$ systemcall = "$ zip $ cmp_name $ tmp_name"; $ output = `$ systemcall`; if (file_exists ($ cmp_name)) {$ savepath = $ store_path. $ filename; rename ($ cmp_name, $ savepath);}}}?>