<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hồ Bảo Huy - Chuyên Gia SEO - Chuyên Gia Marketing &#187; bảo mật</title>
	<atom:link href="http://www.hobaohuy.com/tag/bao-mat/feed" rel="self" type="application/rss+xml" />
	<link>http://www.hobaohuy.com</link>
	<description>www.hobaohuy.com</description>
	<lastBuildDate>Wed, 01 Oct 2014 07:44:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>Tăng cường tối đa tính bảo mật trong WordPress bằng Nonce</title>
		<link>http://www.hobaohuy.com/tang-cuong-toi-da-tinh-bao-mat-trong-wordpress-bang-nonce.html</link>
		<comments>http://www.hobaohuy.com/tang-cuong-toi-da-tinh-bao-mat-trong-wordpress-bang-nonce.html#comments</comments>
		<pubDate>Tue, 30 Sep 2014 05:31:46 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Phát Triển WordPress]]></category>
		<category><![CDATA[bảo mật]]></category>
		<category><![CDATA[wordpress api]]></category>

		<guid isPermaLink="false">http://www.hobaohuy.com/?p=112</guid>
		<description><![CDATA[Trong khi xây dựng 1 website thì vấn đề quan trọng không thể thiếu đó là bảo mật. Có rất nhiều cách để tăng cường tính bảo mật trong WordPress. Bạn Thạch cũng đã có bài viết về một số cách bảo mật trong WordPress. Các bạn có thể xem lại tại đây. Trong bài]]></description>
				<content:encoded><![CDATA[<p>Trong khi xây dựng 1 website thì vấn đề quan trọng không thể thiếu đó là bảo mật. Có rất nhiều cách để tăng cường tính bảo mật trong WordPress. Bạn Thạch cũng đã có bài viết về một số cách bảo mật trong WordPress. Các bạn có thể xem lại tại đây. Trong bài viết này mình sẽ hướng dẫn thêm cho các bạn cách <strong>bảo mật với</strong> <strong>Nonce</strong>, một cách hiệu quả để hạn chế việc tấn công theo kỹ thuật <strong>Cross Site Request Forgery</strong> <strong>(CSRF).</strong></p>
<p><img class="size-full  aligncenter" style="opacity: 1;" title="Sử dụng Nonce để tăng cường tính bảo mật trong WordPress" src="/images/post/2014/09/30/05//nonce-trong-wordpress.png" alt="nonce trong wordpress Tăng cường tối đa tính bảo mật trong WordPress bằng Nonce" width="588" height="330" /></p>
<h3>1. Nonce là gì?</h3>
<p>Hiểu đơn giản nó là 1 chuỗi duy nhất được tạo ra bởi WordPress cho mỗi người dùng. Nó giúp bảo vệ website khỏi những request tự tạo với mục đích xấu.</p>
<p>Ví dụ như thế này cho các bạn dễ hiểu. Giả sử mình có 1 link dùng để xóa bài viết như thế này:</p>
<div>
<div id="highlighter_408563" class="syntaxhighlighter  xml">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="xml plain">&lt;</code><code class="xml keyword">a</code> <code class="xml color1">href</code><code class="xml plain">=</code><code class="xml string">"http://domain/?action=delete&amp;id=1"</code><code class="xml plain">&gt;Xóa&lt;/</code><code class="xml keyword">a</code><code class="xml plain">&gt;</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Khi mình click vào link thì bài viết sẽ được xóa bình thường. Ok, không vấn đề gì. Nhưng vấn đề xảy ra nếu người dùng có dụng ý xấu. Họ sử dụng 1số thủ thuật nào đó để thông qua quyền của mình và thực hiện việc chạy lại đường link trên với 1 id khác thì sao? Bài viết khác có thể bị xóa cho dù mình không click vào nút xóa. Bây giờ các bạn đã thấy vấn đề rồi chứ. Ngoài ra thì khi dùng form để submit dữ liệu cũng vẫn bị vấn đề trên.</p>
<p>Vậy làm sao để xác định được chính xác 1 request do người dùng thực hiện thông qua click link hay submit form ?</p>
<p>Việc cần làm là chúng ta phải tạo ra thêm 1 nonce cho request và trang xử lý phải xác nhận cái nonce này. Nếu nonce không hợp lệ thì không xử lý request. Làm như vậy thì chúng ta mới xác định được chính xác thao tác đấy(click link, submit form) là thực sự của người dùng tạo ra.</p>
<h3>2. Các sử dụng</h3>
<p>Như đã nói ở trên nonce là 1 chuỗi vậy chuỗi này được tạo ra như thế nào? Mở <strong>file wp_config.php</strong> lên và tìm đến 2 hằng <strong>NONCE_KEY</strong> và <strong>AUTH_SALT</strong>. Các bạn thấy chưa ạ? Một dãy các ký tự đúng không nào? WordPress đã sử dụng dụng 2 hằng này và hash cùng với một số thông số khác sau như action, user id … sau đó cắt ra 1 đoạn để tạo ra nonce đấy.</p>
<p>Cách đơn giản và linh động nhất là các bạn sử dụng <strong>hàm wp_create_nonce($action)</strong> để tạo nonce và dùng <strong>hàm wp_verify_nonce($nonce, $action)</strong> để xác nhận.</p>
<p>Giả sử với link xóa bài như bên trên, mình cần tạo ra 1 nonce, đưa nonce này vào query string, bên trang xử lý sẽ xác nhận nonce thông qua query string.</p>
<div>
<div id="highlighter_525902" class="syntaxhighlighter  php">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php variable">$nonce</code> <code class="php plain">= wp_create_nonce(</code><code class="php string">' delete_1'</code><code class="php plain">); </code><code class="php comments">//tạo nonce</code></div>
<div class="line number3 index2 alt2"><code class="php functions">echo</code> <code class="php string">'&lt;a href="http://domain/?action=delete&amp;id=1&amp;_nonce='</code><code class="php plain">.</code><code class="php variable">$nonce</code><code class="php plain">.</code><code class="php string">'"&gt;Xóa&lt;/a&gt;'</code><code class="php plain">; </code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Các bạn thấy đấy, mình đã tạo thêm 1 nonce với $action truyền vào là delete_1. Và mình đã đưa nó vào query string để verify bên trang xử lý. Tên _nonce các bạn có thể đổi lại tùy ý. Lưu ý: với $action các bạn đặt như thế nào thì trang xử lý các bạn phải verify lại như thế đó.</p>
<p>Bên trang xử lý các bạn chỉ cần xác nhận cái nonce này trước khi xử lý. Không hợp lệ thì ngưng xử lý:</p>
<div>
<div id="highlighter_142595" class="syntaxhighlighter  php">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php keyword">if</code><code class="php plain">(!wp_verify_nonce(</code><code class="php variable">$_GET</code><code class="php plain">[</code><code class="php string">'_nonce'</code><code class="php plain">], </code><code class="php string">'delete_'</code><code class="php plain">.</code><code class="php variable">$_GET</code><code class="php plain">[</code><code class="php string">'id'</code><code class="php plain">])) </code><code class="php functions">exit</code><code class="php plain">();</code></div>
<div class="line number3 index2 alt2"><code class="php comments">//code xử lý ở đây ....</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Bên trang xử lý chúng ta verify nonce thông qua <strong>hàm wp_verify_nonce</strong>. Với <strong>$nonce</strong> là $_GET[&#8216;_nonce&#8217;], và <strong>$action</strong> là ‘delete_’.$_GET[&#8216;id&#8217;]. Vì ở trên nonce được tạo từ ‘delete_1′ nên trang xử lý cần verify đúng $action đó theo dạng delete_{id}.</p>
<p>Thực chất hàm này sẽ tạo ra 1 nonce khác theo $action và so sánh nó với nonce get được.</p>
<p>Đơn giản thế thôi, ngoài cách sử dụng trên wordpress còn cung cấp các hàm để tạo và kiểm tra nonce khác để áp dụng vào từng trường hợp cụ thể.</p>
<p>Để tạo nonce cho url, ngoài cách trên ra chúng ta có thể sử dụng thêm <strong>hàm wp_nonce_url( $actionurl, $action, $name )</strong> hàm này sẽ trả về cho chúng ta 1 đường dẫn đầy đủ chứa nonce.</p>
<p>Tham số:</p>
<ul>
<li>$actionurl chính là đường dẫn để add nonce vào.</li>
<li>$action tương tự trên.</li>
<li>$name là tên của nonce, mặc định là _wpnonce</li>
</ul>
<p>Cũng với ví dụ trên mình sử dùng hàm <strong>wp_nonce_url()</strong> để tạo:</p>
<div>
<div id="highlighter_457962" class="syntaxhighlighter  php">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php variable">$nonce_url</code> <code class="php plain">= wp_nonce_url(</code><code class="php string">'http://domain/?action=delete&amp;id=1'</code><code class="php plain">, </code><code class="php string">'delete_1'</code><code class="php plain">);</code></div>
<div class="line number3 index2 alt2"><code class="php functions">echo</code> <code class="php string">'&lt;a href="'</code><code class="php plain">. </code><code class="php variable">$nonce_url</code><code class="php plain">.</code><code class="php string">' "&gt;Xóa&lt;/a&gt;'</code><code class="php plain">;</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Khi xem thì các bạn thấy đường dẫn nó cũng tương tự cách trên, _wpnonce chính là tham số $name mặc định, các bạn muốn đổi thành tên khác thì tùy chỉnh tại tham số này.</p>
<div>
<div id="highlighter_884845" class="syntaxhighlighter  xml">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="xml plain">http://domain/?action=delete&amp;id=1&amp;_wpnonce=e73a52569c</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Tiếp tục để tạo nonce cho 1 form các bạn có thể dùng thêm <strong>hàm wp_nonce_field( $action, $name, $referer, $echo )</strong>. Các tham số như $action và $name thì tương tự trên. Tham số <strong>$referer</strong> sẽ nhận giá trị true hoặc false, quyết định hiển thị thêm 1 field ẩn chứa đường dẫn hiện tại. <strong>$echo</strong> cũng nhận giá trị true hoặc false, quyết định hàm <strong>wp_nonce_field()</strong> sẽ hiển thị hay trả về kết quả.</p>
<p>Khi sử dụng hàm này với <strong>$referer </strong>là true các bạn sẽ thấy trong form có thêm 2 trường dạng như thế này:</p>
<div>
<div id="highlighter_366965" class="syntaxhighlighter  xml">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="xml plain">&lt;</code><code class="xml keyword">input</code> <code class="xml color1">type</code><code class="xml plain">=</code><code class="xml string">"hidden"</code> <code class="xml color1">id</code><code class="xml plain">=</code><code class="xml string">"_nonce"</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"_nonce"</code> <code class="xml color1">value</code><code class="xml plain">=</code><code class="xml string">"43bc8cf75e"</code> <code class="xml plain">/&gt;</code></div>
<div class="line number3 index2 alt2"><code class="xml plain">&lt;</code><code class="xml keyword">input</code> <code class="xml color1">type</code><code class="xml plain">=</code><code class="xml string">"hidden"</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"_wp_http_referer"</code> <code class="xml color1">value</code><code class="xml plain">=</code><code class="xml string">"/phanmem/ "</code> <code class="xml plain">/&gt;</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Về cách verify nonce thì WordPress cung cấp thêm cho chúng ta 2 hàm nữa khác là <strong>check_admin_referer()</strong> và <strong>check_ajax_referer()</strong>.</p>
<p>Hàm <strong>check_admin_referer() </strong>sẽ dùng để verify nonce trong trang quản lý. Hàm này nhận 2 tham số là <strong>$action</strong> và <strong>$query_arg</strong> với <strong>$action</strong> thì tương tự, <strong>$query_arg</strong> chính là <strong>$name</strong> của nonce. Hàm này sẽ trả về true nếu nonce hợp lệ và ngược lại là false.</p>
<p>Giả sử có 1 form dạng như thế này:</p>
<div>
<div id="highlighter_707774" class="syntaxhighlighter  xml">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
<div class="line number6 index5 alt1">6</div>
<div class="line number7 index6 alt2">7</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="xml plain">&lt;</code><code class="xml keyword">form</code> <code class="xml color1">method</code><code class="xml plain">=</code><code class="xml string">"post"</code><code class="xml plain">&gt;</code></div>
<div class="line number3 index2 alt2"><code class="xml spaces">    </code><code class="xml plain">//.......</code></div>
<div class="line number5 index4 alt2"><code class="xml spaces">   </code><code class="xml plain">&lt;?</code><code class="xml keyword">php</code> <code class="xml plain">wp_nonce_field( 'myaction','mynonce' ); ?&gt;</code></div>
<div class="line number7 index6 alt2"><code class="xml plain">&lt;/</code><code class="xml keyword">form</code><code class="xml plain">&gt;</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Thì khi verify các bạn làm như sau:</p>
<div>
<div id="highlighter_878405" class="syntaxhighlighter  php">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php keyword">if</code><code class="php plain">(!check_admin_referer(</code><code class="php string">'myaction'</code><code class="php plain">, </code><code class="php string">'mynonce'</code><code class="php plain">)) </code><code class="php keyword">die</code><code class="php plain">();</code></div>
<div class="line number3 index2 alt2"><code class="php spaces"> </code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Hàm <strong>check_ajax_referer()</strong> sẽ verify nonce thông qua <strong>Ajax</strong>. Hàm này nhận 3 tham số, 2 tham số đầu là <strong>$action</strong> và <strong>$query_arg</strong> tương tự hàm trên, với tham số thứ 3 là <strong>$die,</strong> nếu nonce không hợp lệ, và tham số này set true thì sẽ ngưng xử lý, nếu false thì bỏ qua. Mặc định là true.</p>
<p>Thời gian sống mặc định của 1 nonce là 24 giờ. Các bạn vẫn có thể chỉnh sửa lại thông qua hook<strong> nonce_life</strong>.</p>
<div>
<div id="highlighter_133949" class="syntaxhighlighter  php">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php plain">add_filter( </code><code class="php string">'nonce_life'</code><code class="php plain">, </code><code class="php keyword">function</code> <code class="php plain">() { </code><code class="php keyword">return</code> <code class="php plain">4 * HOUR_IN_SECONDS; } );</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Hằng <strong>HOUR_IN_SECONDS </strong>sẽ trả về số giây trên giờ (3600).</p>
<h3>3. Lời kết</h3>
<p>Mình đã hướng dẫn xong cho các bạn cách bảo mật WordPress với <strong>Nonce</strong> để hạn chế việc tấn công theo kỹ thuật <strong>CSRF</strong> trong khi phát triển theme và plugin. Hy vọng với những kiến thức này có thể giúp các bạn bảo vệ website của mình tốt hơn.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hobaohuy.com/tang-cuong-toi-da-tinh-bao-mat-trong-wordpress-bang-nonce.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
