Hack Forums
[PHP] Creating a script that creates scripts?? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Creating a script that creates scripts?? (/showthread.php?tid=976770)



Creating a script that creates scripts?? - The Penguin - Jan 15, 2011

Hello everyone.

Im trying to make a little script that creates a script in a different directory.

Heres what im trying to do :

PHP Code

<?php 
 $File 
= "text.txt"; 
 
$Handle = fopen($File, 'w');
?>

<?php 
 $content 
= "<?

$fh = fopen( 'log.txt', 'w' );
fclose(
$fh);

$red = $_SERVER['REQUEST_URI'];

$file = "log.txt";

$hostname = $_SERVER['HTTP_HOST'];

$Browser = $_SERVER['HTTP_USER_AGENT'];

$ip = $_SERVER['REMOTE_ADDR'];

$today = date("F j, Y, g:i a");

$handle = fopen($file, 'a');

fwrite(
$handle, "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");

fwrite(
$handle, "\n");

fwrite(
$handle, "IP Address: ");

fwrite(
$handle, "$ip");

fwrite(
$handle, "\n");

fwrite(
$handle, "Date: ");

fwrite(
$handle, "$today");

fwrite(
$handle, "\n");

fwrite(
$handle, "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");

fwrite(
$handle, "\n");

fwrite(
$handle, "\n");

fclose(
$handle);



?>

<html><head>

<title>404 Not Found</title>

</head><body bgcolor=black>

<font color=lime><h1>Not Found</h1></font>

<p><font color=lime>The requested URL / was not found on this server.</font></p>

<hr>

<address><font color=lime><?  echo 
$_SERVER['SERVER_SIGNATURE']; ?></font></address>

</body></html>"
;
 
$File = "text.txt"; 
 
$Handle = fopen($File, 'w'); 
 
fwrite($Handle, $content); 
 print 
"Data Written"; 
 
fclose($Handle); 
?>

I'm trying to have it create a dir called "newdir" and create a file called "log.php" in that dir, then fill the file with the content bellow :

Code
<?

$fh = fopen( 'log.txt', 'w' );
fclose($fh);

$red = $_SERVER['REQUEST_URI'];

$file = "log.txt";

$hostname = $_SERVER['HTTP_HOST'];

$Browser = $_SERVER['HTTP_USER_AGENT'];

$ip = $_SERVER['REMOTE_ADDR'];

$today = date("F j, Y, g:i a");

$handle = fopen($file, 'a');

fwrite($handle, "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");

fwrite($handle, "\n");

fwrite($handle, "IP Address: ");

fwrite($handle, "$ip");

fwrite($handle, "\n");

fwrite($handle, "Date: ");

fwrite($handle, "$today");

fwrite($handle, "\n");

fwrite($handle, "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");

fwrite($handle, "\n");

fwrite($handle, "\n");

fclose($handle);



?>

<html><head>

<title>404 Not Found</title>

</head><body bgcolor=black>

<font color=lime><h1>Not Found</h1></font>

<p><font color=lime>The requested URL / was not found on this server.</font></p>

<hr>

<address><font color=lime><?  echo $_SERVER['SERVER_SIGNATURE']; ?></font></address>

</body></html>

Not sure where im going wrong here.. also, when i try to mkdir() and create the file at the same time it just refuses to do it.

Would appreciate some help on this<3

Kind Regards 🙂


* - Manzzup - Jan 15, 2011

first do
mkdir("newdir");

then new file


* - The Penguin - Jan 15, 2011

(Jan 15, 2011 01:35 AM)Manzzup Wrote:  first do
mkdir("newdir");

then new file

yes tried that, my main dilema is placing the script inside the php file.


* - Uncomplicated - Jan 15, 2011

Script that creates scripts is just like any other ordinary PHP script that can write to a file :D

PHP Code

<?php

$dest_dir
="."; // Destination directory 

$dest_name="logger.php"; // Destination script name

$dest_contents=<<<'UNCOMPLICATED'
<?php

$fh = fopen( 'log.txt', 'w' );
fclose($fh);

$red = $_SERVER['REQUEST_URI'];

$file = "log.txt";

$hostname = $_SERVER['HTTP_HOST'];

$Browser = $_SERVER['HTTP_USER_AGENT'];

$ip = $_SERVER['REMOTE_ADDR'];

$today = date("F j, Y, g:i a");

$handle = fopen($file, 'a');

fwrite($handle, "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");

fwrite($handle, "\n");

fwrite($handle, "IP Address: ");

fwrite($handle, "$ip");

fwrite($handle, "\n");

fwrite($handle, "Date: ");

fwrite($handle, "$today");

fwrite($handle, "\n");

fwrite($handle, "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");

fwrite($handle, "\n");

fwrite($handle, "\n");

fclose($handle);

?>

<html><head>

<title>404 Not Found</title>

</head><body bgcolor=black>

<font color=lime><h1>Not Found</h1></font>

<p><font color=lime>The requested URL / was not found on this server.</font></p>

<hr>

<address><font color=lime><?  echo $_SERVER['SERVER_SIGNATURE']; ?></font></address>

</body></html>
UNCOMPLICATED;

$dest_dir=preg_replace("@[^a-z0-9- ]@is","",$dest_dir);

$dest_name=preg_replace("@[^a-z0-9- ]@is","",$dest_name);

if(
$dest_dir=="" || $dest_name=="")exit("<br />OMG ? Something was left empty");

if(
is_dir($dest_dir)==false)if(mkdir($dest_dir,0755)==false)exit("<br />Unable to create directory");
else echo
"<br />Directory created";;

if(
is_writable($dest_dir)==false)if(chmod($dest_dir,0755)==false)exit("<br />Write permissions denied");
else echo
"<br />Directory permissions changed";

if(
is_file($dest_dir.DIRECTORY_SEPARATOR.$dest_name))exit("<br />Destination script already exists");

$link=fopen($dest_dir.DIRECTORY_SEPARATOR.$dest_name,"a");

if(
$link){fputs($link,$dest_contents);fclose($link);}

else{exit(
"<br />Unable to write to the file");}

?>



* - moneyshotkid - Jan 17, 2011

Make sure you have write permissions CHMOD777, use with caution o na production server. just dont dont do it.


* - Uncomplicated - Jan 17, 2011

@Moneyshotkid, who said that 0777 is write permission ?


* - The Penguin - Jan 26, 2011

(Jan 15, 2011 07:06 AM)Uncomplicated Wrote:  Script that creates scripts is just like any other ordinary PHP script that can write to a file :D

PHP Code

<?php

$dest_dir
="."; // Destination directory 

$dest_name="logger.php"; // Destination script name

$dest_contents=<<<'UNCOMPLICATED'
<?php

$fh = fopen( 'log.txt', 'w' );
fclose($fh);

$red = $_SERVER['REQUEST_URI'];

$file = "log.txt";

$hostname = $_SERVER['HTTP_HOST'];

$Browser = $_SERVER['HTTP_USER_AGENT'];

$ip = $_SERVER['REMOTE_ADDR'];

$today = date("F j, Y, g:i a");

$handle = fopen($file, 'a');

fwrite($handle, "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");

fwrite($handle, "\n");

fwrite($handle, "IP Address: ");

fwrite($handle, "$ip");

fwrite($handle, "\n");

fwrite($handle, "Date: ");

fwrite($handle, "$today");

fwrite($handle, "\n");

fwrite($handle, "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");

fwrite($handle, "\n");

fwrite($handle, "\n");

fclose($handle);

?>

<html><head>

<title>404 Not Found</title>

</head><body bgcolor=black>

<font color=lime><h1>Not Found</h1></font>

<p><font color=lime>The requested URL / was not found on this server.</font></p>

<hr>

<address><font color=lime><?  echo $_SERVER['SERVER_SIGNATURE']; ?></font></address>

</body></html>
UNCOMPLICATED;

$dest_dir=preg_replace("@[^a-z0-9- ]@is","",$dest_dir);

$dest_name=preg_replace("@[^a-z0-9- ]@is","",$dest_name);

if(
$dest_dir=="" || $dest_name=="")exit("<br />OMG ? Something was left empty");

if(
is_dir($dest_dir)==false)if(mkdir($dest_dir,0755)==false)exit("<br />Unable to create directory");
else echo
"<br />Directory created";;

if(
is_writable($dest_dir)==false)if(chmod($dest_dir,0755)==false)exit("<br />Write permissions denied");
else echo
"<br />Directory permissions changed";

if(
is_file($dest_dir.DIRECTORY_SEPARATOR.$dest_name))exit("<br />Destination script already exists");

$link=fopen($dest_dir.DIRECTORY_SEPARATOR.$dest_name,"a");

if(
$link){fputs($link,$dest_contents);fclose($link);}

else{exit(
"<br />Unable to write to the file");}

?>

Thanks honey but i cant figure out why that script wont work..appreciate your help though<3