PHP programming function security. For those who want to do web security, it is best to learn it. but what we want is not fish but fish. In China, a variety of php 1.0 and 2.0 versions are the best thing for people who want to do web security after rain, we don't want fish but fish. In China, a variety of php 1.0 and 2.0 programs have sprung up. However, we are all concerned about some famous cms, Forum, and blog programs, few people are conducting security checks on unspecified programs. for more and more php programmers and webmasters, apart from relying on server bastion settings, you must understand the security of php programs.
Some people say that php security is nothing more than injection and cross-site anything. it is a big mistake. if so, a magic_quotes_gpc or some security settings on the server will make us completely dead :(. What I want to talk about today is not injection, not cross-site, but some security details in php programs. OK! Start with the topic.
Pay attention to the filtering of some functions.
Some functions are frequently used in programs, such as include (), require (), fopen (), fwrite (), readfile (), unlink (), eval () and their variant functions. These functions are very practical, and they do not mean that you have to worry about them. you have to pay more for them. :)
1. include (), require (), fopen (), include_once (), and require_once () can all remotely call files. if you search for these files, you will be very clear about the harm they cause, if you do not filter out the variables that contain the call, you can include any file and execute it. For example, let's look at print. php.
...
If (empty ($ bn) {// check whether the variable $ bn is null
Include ("$ cmd_dir/site _ $ {site}. php"); // include site _ $ {site}. php in the $ cmd_dir path
...
No matter the $ External _dir directory does not exist, you can use the $ site variable naturally because it does not check the $ site variable. You can specify a remote file to call the variable $ site or a local file. write the php statement in the specified file, then it will include the file that executes this php statement. just like this
Listing file directories can even be extended to include some administrator files to Improve permissions, typically like previous phpwind and bo-blog vulnerabilities. Besides relying on php. set allow_url_fopen in ini to off to prohibit remote use of files and open_base_dir to prohibit files other than directories. you have to declare in advance which files can only be included, so I will not talk about it here.
2. fopen (), file (), readfile (), openfile (), and so on are also important points. The function itself has nothing to do. its function is to open the file. However, if the variable is not thoroughly filtered, the source code will be leaked. There will be many such function text forums.
...
$ Articlearray = openfile ("$ dbpath/$ fid/$ tid. php"); // open the $ tid. php file in the $ dbpath/$ fid path
$ Topic_detail = explode ("|", $ articlearray [0]); // use a delimiter | read the post content
...
Familiar with this. this is the read. php version of ofstar. $ fid and $ tid are not filtered. if $ tid is specified as a file for submission, the original code is leaked. Just like this.
$ Tid will be suffixed with php, so you can directly write the index. This is just an example. let's take a look.
3. fwrite () and its variant functions. if you do not filter the characters submitted by the user, it is not impossible to write a php backdoor.
4. unlink () function. some time ago, this function was used to delete any file in phpwind. if the variable to be deleted is not filtered, the variable can be specified as any file, of course, you can delete any file variable.
5. eval (), preg_replace () functions are used to execute php code. if the string is not filtered, what will happen? I often see some cms functions used in it, think about it. isn't a php Trojan built based on the eval () principle?
6. for system (), you will say in php. this is also a good way to disable system functions in ini. but as some programs need it, isn't it necessary? Just like the beautiful php album I saw last time. In addition, you must pay special attention to the popen (), proc_open (), proc_close () functions, even though they do not directly output the output after executing the command, but do you think this is useful to hackers. Here, php provides two functions, escapeshellarg () and escapeshellcmd (), which are used to defend against system function call attacks, that is, filtering.
For hazards, let's take a look at a forum's prod. php.
07 $ doubleApp = isset ($ argv [1]); // Initialization variable $ doubleApp
...
14 if ($ doubleApp) // if statement
15 {
16 $ appDir = $ argv [1]; // initialize $ appDir
17 system ("mkdir $ prodDir/$ appDir"); // use the system function system to create the directory $ prodDir/$ appDir
It was originally used to create the $ prodDir/$ appDir Directory. then it looks like the program only checks whether $ argv [1] exists and does not need to filter $ argv [1, then you can
/Prod. php? Argv [1] = | ls % 20-la or/prod. php? Argv [1] = | cat % 20/etc/passwd
(Delimiter | here is the UNIX pipeline parameter. you can execute multiple commands .)
By now, you should know the common vulnerability types.
Therefore, do not count on server-side settings. it is recommended that the background program also pay attention to the fact that, in general, the server should be better configured for a site. However, many operations with databases are not so easy to control.
Bytes. In China, a variety of php 1.0 versions, 2.0 versions like rain...