在SFTP服务器上编辑文件
我编写了一个脚本,该脚本从文本区域获取用户输入并将其放入文本文件中。它还检查用户的文本片段是否已存在于文本文件中,在这种情况下,它不会写入文件(以防止重复条目)。 在我下面的代码中,有问题的文件是“textfile.txt”。我想编辑 SFTP 服务器上的文件,而不是那个。我读过一些有关 ssh2_sftp 的内容,但我不明白如何使用它。请帮我!
另外,让人们使用下面的代码在服务器上编辑文本文件是否存在安全风险? (除了垃圾邮件和文件变得非常大,因为我在输入表单中使用验证码)。
谢谢你!
<?
$text = $_POST['update'];
$handle = file_get_contents("textfile.txt",NULL);
$text=str_replace(",","",$text);
$text=explode(" ",$text);
$c=0;
foreach($text as $y){
if (stristr($handle,"$text[$c]")) $b[]= 'yes';
else $b[]='no';
$c++;
}
echo $handle;
if (in_array("no",$b)) /*här */if($_POST['Submit']){
$open = fopen("textfile.txt","a+");
$text = $_POST['update'];
fwrite($open, "".$text."\n");
fclose($open);
echo "<br/><br/><br/>".$text." has been saved.";
foreach($file as $text) {
echo $text."<br />";
}
}else{
}
else echo '<br/><br/>Thats already in there.';
?>
i have written a script that takes the users input from an textarea and puts it in a text file. It also checks if the users piece of text already exists in the text file, in that case it does not write to the file (to prevent duplicate entries).
In my code below, the file in question is 'textfile.txt'. Instead of that one i want to edit a file on a SFTP server. I've read something about ssh2_sftp but i didn't understand how to use it. Please help me!
Also, is there any security risk to let people edit a text file on a server using the code below? (except spamming and the file getting ridicously large, as i am using a CAPTCHA for the input form).
Thank you!
<?
$text = $_POST['update'];
$handle = file_get_contents("textfile.txt",NULL);
$text=str_replace(",","",$text);
$text=explode(" ",$text);
$c=0;
foreach($text as $y){
if (stristr($handle,"$text[$c]")) $b[]= 'yes';
else $b[]='no';
$c++;
}
echo $handle;
if (in_array("no",$b)) /*här */if($_POST['Submit']){
$open = fopen("textfile.txt","a+");
$text = $_POST['update'];
fwrite($open, "".$text."\n");
fclose($open);
echo "<br/><br/><br/>".$text." has been saved.";
foreach($file as $text) {
echo $text."<br />";
}
}else{
}
else echo '<br/><br/>Thats already in there.';
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。除非您正在清理文件内容的显示方式(据我所知,您正在使用
echo $handle;
来显示它),否则人们可以提交精心设计的 HTML 并创建一个XSS 攻击。您可能需要考虑在输入数据上使用 strip_tags() 来帮助防止这种情况。
此外,由于对未知大小的文件使用了
file_get_contents
,因此可以相当容易地发起 DOS 攻击。通过简单地逐行循环文件或限制用户提交的文本长度可以减少这种情况。这种攻击不太可能那么严重,因为您使用的是验证码,这会减慢大多数用户快速提交文本的速度,但是如果在不使用验证码的情况下调用file_get_contents()
(例如,为了查看文件的内容)那么你仍然会遇到问题。编辑:我为您重写了大部分代码片段并添加了很多注释。希望您能从中学到一些提示和技巧,并更好地理解最佳编程实践。 (我还没有尝试运行代码,但它应该可以正常工作。根据需要对其进行修改。)
http://pastebin.com/W1EQ3fSm
Yes. Unless you're sanitizing how the content of the file is shown (from what I see, you're using an
echo $handle;
to display it), then a person could submit crafted HTML and create an XSS attack.You might want to consider using strip_tags() on the input data to help prevent this.
Also, a DOS attack could be launched fairly easily because of the usage of
file_get_contents
on a file of unknown size. This can be lessened by simply looping through the file line-by-line or by putting a limit on how long the user submitted text can be. This attack isn't likely as serious because you're using CAPTCHA which will slow down most users from submitting text rapidly, but iffile_get_contents()
is called without usage of CAPTCHA (say, for viewing the file's contents) then you'll still have a problem.Edit: I rewrote most of your code snippet for you and added lots of comments. Hopefully you can pick up a few tips and tricks from it and gain a better understanding of best programming practices. (I haven't tried running the code, but it should work fine. Make modifications to it as needed.)
http://pastebin.com/W1EQ3fSm
使用 phpseclib,一个纯 PHP SFTP 实现...
Using phpseclib, a pure PHP SFTP implementation...