-
-
$fp = fopen("aaa.conf", 'r'); - $configfile = fread($fp, filesize("aaa.conf"));
- fclose($fp);
- //Do it through regular replacement
- $configfile = preg_replace("/\n[password](.+?)\n/is", "", $configfile);//This is only To match the content between [password] and the next blank line, just write /[password](.+?)\n/is, but I want to remove the blank line in front of this line, so add Got a n
//Rewrite the file back to its original location
- $fp = fopen("aaa.conf", 'w');
- fwrite($fp, trim($configfile) );
- fclose($fp);
- //Add two new password lines at the end of the file
- $newpassword = "456";
- $filename="aaa.conf";//Define the operation file
- $fcontent = file( $filename); //file() reads the entire file into an array
- $fp = fopen("$filename","a");
- $str = "nn[password]n$newpassword";
- fwrite( $fp, $str);
- //by bbs.it-home.org
-
Copy code
Today I was changing the password of a php web shell program, and I encountered a problem. The password and The program is in the same file. How can it be modified seamlessly without affecting the normal execution of the program?
The format of the configuration file is similar to the following:
-
-
- $lines = file("config.php");
- $count =sizeof($lines);
- for($i=0; $i<$count; $i++) {
- $tmp = explode($lines[$i], '=');
- if($tmp==null || sizeof($tmp)!=2)
- continue;
- if(trim($tmp[0 ])=='$manage["user"]'){
- $lines[$i] = $tmp[0]."= ".$manage["user"];
- break;
- }
- }
- $ str = implode($lines, "rn");
-
Copy the code
and write $str back to the file
Indeed, according to my idea, the code should be like this, but when I execute it, it doesn’t work.
Why half? I have been thinking for a long time whether I can do it through regular expressions.
So I also considered that the form $manage[''user''] does not appear many times in the program, and it may be modified through regular replacement.
Idea: Read all the program code into a variable, and then replace the corresponding content in the string through regular expressions.
Code:
-
-
- //Open file
- $fp = fopen($manage["file"], 'r');
/ / Read the file into $configfile
- $configfile = fread($fp, filesize($manage["file"]));
- fclose($fp);
// Replace with regular expression Do it
- $configfile = preg_replace("/[$]manage["user"]s*=s*["'].*?["']/is", "$manage["user"] = "$ user_name"", $configfile);
- $configfile = preg_replace("/[$]manage["pass"]s*=s*["'].*?["']/is", "$manage[" pass"] = "$user_pass"", $configfile);
-
- // Rewrite the file back to its original location
- $fp = fopen($manage["file"], 'w');
- fwrite($fp , trim($configfile));
- fclose($fp);
-
Copy code
|