试图破坏处理 $_POST 表单数据的 PHP 脚本
Trying to break the PHP script which processes $_POST form data
我找不到任何关于此的明确信息。
我有一个HTML5
表格...
- 输出到外部
PHP
脚本
- 将表单输出的变量保存为
$_SESSION
个变量
- 然后传递到另一个页面
- 显示它们
我(还)没有从任何表单字段中转义任何数据。
然而,当我在表单的 <textarea>
中输入 '
或 "
或 &
时,一切都继续顺利进行,没有任何中断。
我很高兴它没有(因为我希望我的表单处理尽可能健壮),但是为什么不呢?
是否有一些我不知道的幕后自动转义?
我很想知道是否有权威来源可以解释正在发生的事情。
表单页面(HTML5):
<form class="contactform" method="post" action="/form-processing.php">
<fieldset>
<legend>Please Enter your Contact Details</legend>
<ul>
<li><label for="contactName">Contact Name:</label><input type="text" id="contactName" name="contactName" placeholder="Your Full Name" required /></li>
<li><label for="company">Company:</label><input type="text" id="company" name="company" placeholder="Your Company" required /></li>
<li><label for="telephone">Telephone:</label><input type="tel" id="telephone" name="telephone" placeholder="Your Work Telephone" required /></li>
<li><label for="email">Email:</label><input type="email" id="email" name="email" placeholder="Your Work Email" required /></li>
<li><label for="message">Message:</label>
<textarea id="message" name="message" placeholder="Write your message here..." required></textarea></li>
</ul>
</fieldset>
<input type="submit" value="Send your message" />
</form>
表单处理页面(PHP)
$Contact_Name = $_POST['contactName'];
$Company = $_POST['company'];
$Telephone = $_POST['telephone'];
$Email = $_POST['email'];
$Message = $_POST['message'];
if (($Contact_Name != '') && ($Company != '') && ($Telephone != '') && ($Email != '') && ($Message != '')) {
[...SCRIPT HERE...]
session_start();
$_SESSION['contactName'] = $Contact_Name;
$_SESSION['company'] = $Company;
$_SESSION['telephone'] = $Telephone;
$_SESSION['email'] = $Email;
$_SESSION['message'] = $Message;
header('Location: http://'.$_SERVER['HTTP_HOST'].'/confirmation-page.php');
}
确认页 (PHP)
if ((isset($_SESSION['contactName'])) && (isset($_SESSION['company'])) && (isset($_SESSION['telephone'])) && (isset($_SESSION['email'])) && (isset($_SESSION['message']))) {
$Contact_Name = $_SESSION['contactName'];
$Company = $_SESSION['company'];
$Telephone = $_SESSION['telephone'];
$Email = $_SESSION['email'];
$Message = $_SESSION['message'];
echo '<p>Your message has been sent.</p>
<dl>
<dt>Contact Name:</dt>
<dd>'.$Contact_Name.'</dd>
<dt>Company:</dt>
<dd>'.$Company.'<dd>
<dt>Telephone:</dt>
<dd>'.$Telephone.'<dd>
<dt>Email:</dt>
<dd>'.$Email.'</dd>
</dl>
<p>Message:</p>
<pre>'.$Message.'</pre>
<p>Thank you for your message.</p>
<p>We will be in touch.</p>';
我原以为 '
会破坏表单数据传递给的 PHP 脚本...但事实并非如此。知道为什么不吗?我以为 XSS 攻击应该是这样工作的?
我在回答表中的评论:
20 年前,& 和 "(而不是 & 和 ")在渲染 html 时可能会破坏某些浏览器,但现在它们能够正常处理它 (& & " ; 虽然是正确的)。
PHP 变量可以包含任意字符串(甚至是二进制数据).. 包含 ' 或 " 的 var 没有问题。
实用地赋值时,需要转义:
$myVar = 'sha\'zam!'; // ' needs escaped as the string is enclosed with '
$myVar = "dblquote -> \"!" // " needs escaped as the string is enclosed with "
因为你没有做任何数据库的事情,所以在服务器端没有什么可以"break",但是因为你没有清理东西....输入一些值,比如
</form>
或
<script>alert('shazam!')</script>
这就是攻击者最终获取(受害者的)会话 ID 或其他敏感信息的方式。
我找不到任何关于此的明确信息。
我有一个HTML5
表格...
- 输出到外部
PHP
脚本 - 将表单输出的变量保存为
$_SESSION
个变量 - 然后传递到另一个页面
- 显示它们
我(还)没有从任何表单字段中转义任何数据。
然而,当我在表单的 <textarea>
中输入 '
或 "
或 &
时,一切都继续顺利进行,没有任何中断。
我很高兴它没有(因为我希望我的表单处理尽可能健壮),但是为什么不呢?
是否有一些我不知道的幕后自动转义?
我很想知道是否有权威来源可以解释正在发生的事情。
表单页面(HTML5):
<form class="contactform" method="post" action="/form-processing.php">
<fieldset>
<legend>Please Enter your Contact Details</legend>
<ul>
<li><label for="contactName">Contact Name:</label><input type="text" id="contactName" name="contactName" placeholder="Your Full Name" required /></li>
<li><label for="company">Company:</label><input type="text" id="company" name="company" placeholder="Your Company" required /></li>
<li><label for="telephone">Telephone:</label><input type="tel" id="telephone" name="telephone" placeholder="Your Work Telephone" required /></li>
<li><label for="email">Email:</label><input type="email" id="email" name="email" placeholder="Your Work Email" required /></li>
<li><label for="message">Message:</label>
<textarea id="message" name="message" placeholder="Write your message here..." required></textarea></li>
</ul>
</fieldset>
<input type="submit" value="Send your message" />
</form>
表单处理页面(PHP)
$Contact_Name = $_POST['contactName'];
$Company = $_POST['company'];
$Telephone = $_POST['telephone'];
$Email = $_POST['email'];
$Message = $_POST['message'];
if (($Contact_Name != '') && ($Company != '') && ($Telephone != '') && ($Email != '') && ($Message != '')) {
[...SCRIPT HERE...]
session_start();
$_SESSION['contactName'] = $Contact_Name;
$_SESSION['company'] = $Company;
$_SESSION['telephone'] = $Telephone;
$_SESSION['email'] = $Email;
$_SESSION['message'] = $Message;
header('Location: http://'.$_SERVER['HTTP_HOST'].'/confirmation-page.php');
}
确认页 (PHP)
if ((isset($_SESSION['contactName'])) && (isset($_SESSION['company'])) && (isset($_SESSION['telephone'])) && (isset($_SESSION['email'])) && (isset($_SESSION['message']))) {
$Contact_Name = $_SESSION['contactName'];
$Company = $_SESSION['company'];
$Telephone = $_SESSION['telephone'];
$Email = $_SESSION['email'];
$Message = $_SESSION['message'];
echo '<p>Your message has been sent.</p>
<dl>
<dt>Contact Name:</dt>
<dd>'.$Contact_Name.'</dd>
<dt>Company:</dt>
<dd>'.$Company.'<dd>
<dt>Telephone:</dt>
<dd>'.$Telephone.'<dd>
<dt>Email:</dt>
<dd>'.$Email.'</dd>
</dl>
<p>Message:</p>
<pre>'.$Message.'</pre>
<p>Thank you for your message.</p>
<p>We will be in touch.</p>';
我原以为 '
会破坏表单数据传递给的 PHP 脚本...但事实并非如此。知道为什么不吗?我以为 XSS 攻击应该是这样工作的?
我在回答表中的评论:
20 年前,& 和 "(而不是 & 和 ")在渲染 html 时可能会破坏某些浏览器,但现在它们能够正常处理它 (& & " ; 虽然是正确的)。
PHP 变量可以包含任意字符串(甚至是二进制数据).. 包含 ' 或 " 的 var 没有问题。
实用地赋值时,需要转义:
$myVar = 'sha\'zam!'; // ' needs escaped as the string is enclosed with '
$myVar = "dblquote -> \"!" // " needs escaped as the string is enclosed with "
因为你没有做任何数据库的事情,所以在服务器端没有什么可以"break",但是因为你没有清理东西....输入一些值,比如
</form>
或
<script>alert('shazam!')</script>
这就是攻击者最终获取(受害者的)会话 ID 或其他敏感信息的方式。