Hướng dẫn htmlspecialchars bypass

Also, it's important to mention that allowing people to inject HTML or JavaScript into your page (and not your datasource) carries no inherent security risk itself. There already exist browser extensions that allow you to modify the DOM and scripts on web pages, but since it's only client-side, they're the only ones that will know.

Where XSS becomes a problem is when people a) use it to bypass client-side validation or input filtering or b) when people use it to manipulate input fields (for example, changing the values of OPTION tags in an ACL to grant them permissions they shouldn't have). The ONLY way to prevent against these attacks is to sanitize and validate input on the server-side instead of, or in addition to, client-side validation.

For sanitizing HTML out of input, htmlspecialchars is perfectly adequate unless you WANT to allow certain tags, in which case you can use a library like HTMLPurifier. If you're placing user input in HREF, ONCLICK, or any attribute that allows scripting, you're just asking for trouble.

EDIT: Looking at your code, it looks like you aren't quoting your attributes! That's pretty silly. If someone put their username as:

john onclick="alert('hacking your megabits!1')"

Then your script would parse as:

<input type=text name=username value=john onclick="alert('hacking your megabits!1')">

ALWAYS use quotes around attributes. Even if they aren't user-inputted, it's a good habit to get into.

<input type="text" name="username" value="<?php echo htmlspecialchars($_POST['username']); ?>">

Permalink

Bypass htmlentities & htmlspecialchars

  • We Know That htmlentities() ,htmlspecialchars() it's Function to Prevent XSS
  • When I Write This Code until I see how these functions are prevented XSS

<?php
$a = "!@#$%^&*()}{\'\"|?><`~}";
echo "htmlentities : ".htmlentities($a)."<br>";
echo "htmlspecialchars : ".htmlspecialchars($a);
 ?>

  • I Got This Result

htmlentities : !@#$%^&amp;*()}{\'&quot;|?&gt;&lt;`~}
htmlspecialchars : !@#$%^&amp;*()}{\'&quot;|?&gt;&lt;`~}

  • We See That The Two Function Change ><"& to There Entity Name and We See That This Char '\ doesn't change these mean that if developer write this code By using htmlentities or By Using htmlspecialchars You Can Get XSS !!

//By Using  htmlentities Function 
$src = htmlentities($_GET['src']);
$see = "<img src='".$src."' width=30% height=30%> ";
echo $see;

// By Using htmlspecialchars Function
$src = htmlspecialchars($_GET['src']);
$see = "<img src='".$src."' width=30% height=30%> ";
echo $see;

  • Let's Try Using This Payload 'onerror='alert("XSS")'' with the Developer Code

Hướng dẫn htmlspecialchars bypass

  • As We See it is possible to skip this function if developer use single quotation in his code .

How To Prevent XSS :

  • To Prevent XSS and To Be Safe You Must replace ' with &apos; and remove \ and use each of There Function
  • You Can Use This Simple Code To Prevent XSS

function check($str)
{
  $str = preg_replace('#\'#','&apos;',$str);    // Change [ ' ] To [ &apos; ]
  $str = preg_replace('#\\#','',$str);   // To remove [ / ]
  $str = htmlspecialchars($str); // or $str = htmlentities($str);
  return $str;
}

  • Or You Can Filter You input By

// By htmlentities
$value = htmlentities($_GET['src'], ENT_QUOTES);

// By htmlspecialchars
$value = htmlspecialchars($_GET['src'], ENT_QUOTES);

Ngoài ra, điều quan trọng cần đề cập là việc cho phép mọi người đưa HTML hoặc JavaScript vào trang của bạn (chứ không phải nguồn dữ liệu của bạn) không mang lại rủi ro bảo mật vốn có. Đã tồn tại các tiện ích mở rộng trình duyệt cho phép bạn sửa đổi DOM và tập lệnh trên các trang web, nhưng vì nó chỉ ở phía máy khách nên chúng là những thứ duy nhất biết được.

Trường hợp XSS trở thành vấn đề là khi mọi người a) sử dụng nó để bỏ qua xác thực phía máy khách hoặc lọc đầu vào hoặc b) khi mọi người sử dụng nó để thao tác các trường đầu vào (ví dụ: thay đổi giá trị của thẻ OPTION trong ACL để cấp cho họ quyền mà họ không nên có). Cách DUY NHẤT để ngăn chặn các cuộc tấn công này là khử trùng và xác thực đầu vào ở phía máy chủ thay vì hoặc ngoài xác thực phía máy khách.

Để làm sạch HTML ra khỏi đầu vào, htmlspecialchars là hoàn toàn phù hợp trừ khi bạn MUỐN cho phép một số thẻ nhất định, trong trường hợp đó, bạn có thể sử dụng một thư viện như HTMLPurifier. Nếu bạn đang đặt đầu vào của người dùng trong HREF, ONCLICK hoặc bất kỳ thuộc tính nào cho phép tạo tập lệnh, bạn chỉ đang gặp rắc rối.

CHỈNH SỬA: Nhìn vào mã của bạn, có vẻ như bạn không trích dẫn các thuộc tính của mình! Thật là ngớ ngẩn. Nếu ai đó đặt tên người dùng của họ là:

john onclick="alert('hacking your megabits!1')"

Sau đó, tập lệnh của bạn sẽ phân tích cú pháp thành:

<input type=text name=username value=john onclick="alert('hacking your megabits!1')">

LUÔN LUÔN sử dụng dấu ngoặc kép xung quanh các thuộc tính. Ngay cả khi chúng không được người dùng nhập vào, đó là một thói quen tốt để thực hiện.

<input type="text" name="username" value="<?php echo htmlspecialchars($_POST['username']); ?>">

15 hữu ích 5 bình luận chia sẻ