2014/10/31

正規表示式 Regexper 及測試使用的小工具


講到 RE 首先推薦這個網站,由 osteele 作者開發的小工具,可以方便的針對程式中使用的正規表示式先行測試,最後還附上使用語法的頁面。

以檢測是否為網卡卡號的表示式為範例:

[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}

http://osteele.com/tools/rework/



另外一各式把正規表示式畫成一個圖,看起來比較容易理解。
http://www.regexper.com/

底下為使用 REGERPER 網站顯示的圖形說明



在 Linux 下可以使用 grep -E 來使用進階的正規表示試驗正,如果使用 grep -e 的話只是用一般型態的正規表示式。地下為 ifconfig 指令找到該指令中 MAC address 位置的正規表示式。




常見的正規表示式
1. 驗證是否為一個合法的日期字串 ex: 2014-10-31

/^(\d{2}(([02468][048])|([13579][26]))\-((((0[13578])|(1[02]))\-((0[1-9])|([1-2][0-9])|(3[01])))|(((0[469])|(11))\-((0[1-9])|([1-2][0-9])|(30)))|(02\-((0[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))\-((((0[13578])|(1[02]))\-((0[1-9])|([1-2][0-9])|(3[01])))|(((0[469])|(11))\-((0[1-9])|([1-2][0-9])|(30)))|(02\-((0[1-9])|(1[0-9])|(2[0-8])))))$/

2. Email 簡單的正規表示式
/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/

3. ftp http https 網址
/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/

4. 信用卡
credit_card : /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/

5. RFC 完整的 email 檢查程式,但這已經不只是正規表示式了。
http://dzone.com/snippets/rfc-compliant-email-address





2014/10/30

webmaster 小工具

這個網站,提供一些網頁的小工具
http://www.phpjunkyard.com/

查詢某個 ip 的 whois 資訊
http://www.phpjunkyard.com/tools/ip-whois.php

將文字轉換成為 html ,避免掉奇怪的程式碼
http://www.phpjunkyard.com/tools/text-to-html.php 

將 html 去除成為純文字
http://www.phpjunkyard.com/tools/html-to-text.php

觀看 html 的 head 資訊
http://www.phpjunkyard.com/tools/view-http-header.php

亂碼產生器可以產生自己也會往記的密碼
http://www.phpjunkyard.com/tools/random-string.php




phpmailer 發信程式範例(smtp 不用認證)


PHPmailer 是個很好用的發信工具, https://github.com/PHPMailer/PHPMailer (原始碼) 目前在 linux 的套件也都有收錄這套。

如果要安裝直接安裝   libphp-phpmailer  就可以(ubuntu , linux mint 都有收錄)
mtchang@mt ~ $ sudo apt-cache search phpMailer
libphp-phpmailer - full featured email transfer class for PHP
mtchang@mt ~ $ sudo apt-get install libphp-phpmailer

安裝好後他會放在 /usr/share/php/libphp-phpmailer 目錄內
如果程式中需要引用的話,可以透過
require_once "/usr/share/php/libphp-phpmailer/class.phpmailer.php";
的方式引用

底下為 phpmailer 引用的程式碼範例,並針對中文部份有使用 mb_internal_encoding('UTF-8'); 及 mb_encode_mimeheader("$name","UTF-8")); 避免中文的亂碼產生。

// 程式開始  ------------------------------------------------------------------------------------------------------
<?php
// phpmailer 發信函式庫
require_once "/usr/share/php/libphp-phpmailer/class.phpmailer.php";

// ------------------------------------------------------------------------------------
// 接收上一個網頁傳來的變數
// ------------------------------------------------------------------------------------
$name =$_POST['name'];
$email =$_POST['email'];
$comments =$_POST['comments'];

// ------------------------------------------------------------------------------------
// php mailer 發信
// ------------------------------------------------------------------------------------
$system_email='root@jangmt.com';
date_default_timezone_set('Asia/Taipei');
mb_internal_encoding('UTF-8');
$mail             = new PHPMailer(); // defaults to using php "mail()"
$mail->IsSendmail(); // telling the class to use SendMail transport
// 設定郵件的內文
$body             = eregi_replace("[\]",'',$comments);
// 寄信伺服器,請勿修改位置限制管院內部可以發信
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.jangmt.com"; // SMTP server
$mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = false;                  // enable SMTP authentication
// 信件的回信欄位
$mail->AddReplyTo($system_email,mb_encode_mimeheader("系統發信請勿回信","UTF-8"));
// 信件的送件人欄位
$mail->SetFrom($system_email, mb_encode_mimeheader("系統發信請勿回信","UTF-8"));
// 主要收件人
$mail->AddAddress($email, mb_encode_mimeheader("$name","UTF-8"));
// 標題
$mail->Subject    = "Message from $name, internet customer";
$mail->MsgHTML($body);
// 回報執行結果
if(!$mail->Send()) {
  echo "Email Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Email Message sent! <br>";
}
// ------------------------------------------------------------------------------------
// 網頁顯示提示訊息
// ------------------------------------------------------------------------------------
echo "<html> \n";
echo "<head> \n";
echo "<title> Message from $name, internet customer</title>
\n";
echo "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html;
charset=utf-8\"> \n";
echo "</head> \n\n";
echo "<body> \n";
echo " Thank you for the message, $name.<br> <br> \n";
echo " The information you provide is: <br> \n";
echo " Name: $name <br> \n";
echo " Email: $email <br> \n";
echo " Comments: $comments <br> \n";
echo "</body> </html> \n";


?>
// 程式結束  ------------------------------------------------------------------------------------------------------

底下為搭配上面發信程式的 form 表單,直接拉 CDN 上面的 bootstrap 3.2 來用。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
<title>顧客回饋單</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

  </head>

  <body>
<!-- REF:  http://getbootstrap.com/css/ -->
    <div class="container">
<div class="page-header">
        <h3>顧客回饋單</h3>
        </div>
<div class="col-lg-6">
<p class="text-info"><strong>親愛的顧客,感謝您本次的消費。請留下您寶貴的意見,促使我們能提供更好的服務,謝謝您~</strong></p>
<form method="post" action="sendpost.php">
<p class="text-info">
您的大名:
<input name="name" type="text" />&nbsp;</td>
</p>
<p class="text-info">
您的email:
<input name="email" type="text" />&nbsp;</td>
</p>
<p class="text-info">
您的意見:
<textarea name="comments" cols="20" rows="2"></textarea>
</p>
<input name="Submit1" type="submit" value="submit" />&nbsp;  <input name="Reset1" type="reset" value="reset" />
</form>
</div>

     <div  class="bg-info">
<p></p>
<p>Company 2014 by mtchang </p>
    </div>

    </div> <!-- /container -->

  </body>
</html>

2014/10/25

類似 bootstrap 前端 UI Frameworks


BootStrap 這個 UI Frameworks 很好用,可以很快速的做出品質不錯的使用者界面。 http://getbootstrap.com/  但用久了發現大家做的都一樣,看不出差異。

於是最近評估了幾個類似的 Fremework ,主要來自於底下兩篇的介紹

10 Best Responsive HTML5 Frameworks http://designinstruct.com/roundups/html5-frameworks/

20 Exceptional CSS Boilerplates and Frameworks http://mashable.com/2013/04/26/css-boilerplates-frameworks/

當然 wiki 上面這篇(http://en.wikipedia.org/wiki/CSS_frameworks) 比較了格點系統的細節比較可以參考看看

另外在這篇主要比較了 bootstrap 及 Foundation 及 Skeleton

我這次選了 Foundation  這個 css frameworks 來使用,因為從他的文件說明( http://foundation.zurb.com/docs/ )看得出 他的 UI 走扁平化的設計,符合時尚的潮流 ios7 Mac OS X 10.10 Yosemite 都用 。 

Foundation button
Foundation button


但是總是感覺怪怪的,這個 button 和 10 多年前 M$ frontpage 的內建 button 很像的感覺。所以應該說 M$ 十多年前就走在設計的前端 .....,只是他的名子不叫 APPLE 。

FrontPage Sample


Linux Mint – LAMP 的安裝 (3) – 讓使用者可以用 public_html 當個人網頁目錄

Linux Mint – LAMP 的安裝 (3) – 讓使用者可以用 public_html 當個人網頁目錄

* 設定讓使用者可以用 public_html 當個人網頁目錄

mtchang@mtchang-virtual-machine /etc/apache2/mods-available $ sudo a2enmod userdir
Enabling module userdir.
To activate the new configuration, you need to run:
  service apache2 restart
mtchang@mtchang-virtual-machine /etc/apache2/mods-available $ sudo service apache2 restart
 * Restarting web server apache2


 * 測試 public_html 驗證否正常工作?

mtchang@mtchang-virtual-machine /etc/apache2/mods-available $ cd ~
mtchang@mtchang-virtual-machine ~ $ ls
vmware-tools-distrib  下載  公共  圖片  影片  文件  桌面  模板  音樂
mtchang@mtchang-virtual-machine ~ $ mkdir public_html
mtchang@mtchang-virtual-machine ~ $ echo 'hello my person home'  > /home/mtchang/public_html/index.html
mtchang@mtchang-virtual-machine ~ $ echo '<?php phpinfo();  ?>'  > /home/mtchang/puublic_html/index.php 

* 請直接使用 http://localhost/~mtchang/index.html 或是 http://localhost/~mtchang/index.php 觀看驗證。

* php 程式會當成文字顯示

mtchang@mtchang-virtual-machine ~ $ cd /etc/apache2/mods-enabled/
mtchang@mtchang-virtual-machine /etc/apache2/mods-enabled $ sudo nano php5.conf 
mtchang@mtchang-virtual-machine /etc/apache2/mods-enabled $ sudo service apache2 restart
# 修改底下最後的部份,將它註解
# Running PHP scripts in user directories is disabled by default
#
# To re-enable PHP in user directories comment the following lines
# (from <ifmodule ...=""> to </ifmodule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
#<ifmodule mod_userdir.c="">
#    <directory home="" *="" public_html="">
#        php_admin_flag engine Off
#    </directory>
#</ifmodule>

mtchang@mtchang-virtual-machine /etc/apache2/mods-enabled $ sudo service apache2 restart
 * Restarting web server apache2

Linux Mint – LAMP 的安裝 (2) -MySQL and phpMyAdmin 的安裝與設定

Linux Mint – LAMP 的安裝 (2) -MySQL and phpMyAdmin 的安裝與設定
MySQL and phpMyAdmin 的安裝與設定

mtchang@mtchang-virtual-machine ~ $ sudo apt-get install mysql-server
正在讀取套件清單... 完成
正在重建相依關係        
正在讀取狀態資料... 完成
下列的額外套件將被安裝:
  libaio1 libdbd-mysql-perl libdbi-perl libmysqlclient18 libterm-readkey-perl
  mysql-client-5.5 mysql-client-core-5.5 mysql-common mysql-server-5.5
  mysql-server-core-5.5
建議套件:
  libmldbm-perl libnet-daemon-perl libplrpc-perl libsql-statement-perl tinyca
  mailx
推薦套件:
  libhtml-template-perl
下列【新】套件將會被安裝:
  libaio1 libdbd-mysql-perl libdbi-perl libmysqlclient18 libterm-readkey-perl
  mysql-client-5.5 mysql-client-core-5.5 mysql-common mysql-server
  mysql-server-5.5 mysql-server-core-5.5
升級 0 個,新安裝 11 個,移除 0 個,有 89 個未被升級。
需要下載 8,945 kB 的套件檔。
此操作完成之後,會多佔用 96.3 MB 的磁碟空間。
Do you want to continue? [Y/n] y
...
... (省略了約 1000 行....)
...
設定 libaio1:amd64 (0.3.109-4) ...
設定 libmysqlclient18:amd64 (5.5.38-0ubuntu0.14.04.1) ...
設定 libdbi-perl (1.630-1) ...
設定 libdbd-mysql-perl (4.025-1) ...
設定 libterm-readkey-perl (2.31-1) ...
設定 mysql-client-core-5.5 (5.5.38-0ubuntu0.14.04.1) ...
設定 mysql-client-5.5 (5.5.38-0ubuntu0.14.04.1) ...
設定 mysql-server-core-5.5 (5.5.38-0ubuntu0.14.04.1) ...
設定 mysql-server-5.5 (5.5.38-0ubuntu0.14.04.1) ...
140913 23:10:51 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead.
mysql start/running, process 15507
Processing triggers for ureadahead (0.100.0-16) ...
設定 mysql-server (5.5.38-0ubuntu0.14.04.1) ...
Processing triggers for libc-bin (2.19-0ubuntu6.1) ...

* 安裝過程會詢問 MySQL 的 root 預設密碼 ,請給予一個預設密碼。

* 重新啟動 mysql server 服務

mtchang@mtchang-virtual-machine ~ $ sudo /etc/init.d/mysql restart
 * Stopping MySQL database server mysqld                                                   [ OK ]
 * Starting MySQL database server mysqld                                                   [ OK ]
 * Checking for tables which need an upgrade, are corrupt or were
not closed cleanly.

* 用 mysql 客戶端工具測試看看, 說明請翻開 mysql –help 的內容說明

mtchang@mtchang-virtual-machine /etc/apache2/conf-available $ mysql -u root -h 127.0.0.1 -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 93
Server version: 5.5.38-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select Host,User,Password  from mysql.user;
+-----------------+------------------+-------------------------------------------+
| Host            | User             | Password                                  |
+-----------------+------------------+-------------------------------------------+
| localhost       | root             | *A7A9FADD822492649C01057FB6ADBBC9D2FD02DE |
| mint.jangmt.com | root             | *A7A9FADD822492649C01057FB6ADBBC9D2FD02DE |
| 127.0.0.1       | root             | *A7A9FADD822492649C01057FB6ADBBC9D2FD02DE |
| ::1             | root             | *A7A9FADD822492649C01057FB6ADBBC9D2FD02DE |
| localhost       | debian-sys-maint | *21703857A0643E7417551DE439160F71ADB5701F |
| mint.jangmt.com | phpmyadmin       | *A7A9FADD822492649C01057FB6ADBBC9D2FD02DE |
+-----------------+------------------+-------------------------------------------+
6 rows in set (0.00 sec)

mysql> \q
Bye

* 安裝 phpmyadmin 網頁版本的管理工具
mtchang@mtchang-virtual-machine ~ $ sudo apt-get install phpmyadmin
正在讀取套件清單... 完成
正在重建相依關係        
正在讀取狀態資料... 完成
下列的額外套件將被安裝:
  dbconfig-common libapache2-mod-php5 libjs-codemirror libjs-jquery
  libjs-jquery-cookie libjs-jquery-event-drag libjs-jquery-metadata
  libjs-jquery-mousewheel libjs-jquery-tablesorter libjs-jquery-ui
  libjs-underscore libmcrypt4 php-gettext php5-common php5-mcrypt php5-mysql
建議套件:
  php-pear libjs-jquery-ui-docs libmcrypt-dev mcrypt php5-user-cache
推薦套件:
  php5-cli javascript-common php5-gd
下列【新】套件將會被安裝:
  dbconfig-common libjs-codemirror libjs-jquery libjs-jquery-cookie
  libjs-jquery-event-drag libjs-jquery-metadata libjs-jquery-mousewheel
  libjs-jquery-tablesorter libjs-jquery-ui libjs-underscore libmcrypt4
  php-gettext php5-mcrypt php5-mysql phpmyadmin
下列套件將會被升級:
  libapache2-mod-php5 php5-common
升級 2 個,新安裝 15 個,移除 0 個,有 87 個未被升級。
需要下載 8,379 kB 的套件檔。
此操作完成之後,會多佔用 29.0 MB 的磁碟空間。

* 過程中會問題這個工具和那個 web service 搭配,有兩種選項 apache2 及 lighttpd 兩個。我們當然要選 apache2 這個服務。

* 再來會問 phpmyadmin 和資料庫的設定問題 「Configure database for phpmyadmin with dbconfig-common?」,請一定要回答這些問題,否則無法啟動使用 phpmyadmin 。

* 裝好後你可以使用 http://localhost/phpmyadmin 登入系統也可以用 ip 遠端登入,但是在 ubuntu 及 centos 等系統則沒法從其他機器登入。

* 使用 http://localhost/phpmyadmin/ 登入系統測試

Linux Mint – LAMP 的安裝 (1)



* Apache的管理與設定更是成了初學 Linux 的使用者必須一定要學會的技術。本文說明的主要環境以 Linux Mint,配合 Apache2 Server 網頁服務、PHP 程式語言、MySQL資料庫(更改為 MariaDB) 及 PHPMyAdmin資料庫管理工具,架設出完整的LAMP服務的架設流程。


* 需求套件:再進行LAMP服務的安裝時,我們需要以下開放原始碼的程式。他的官方網站如下:

  • Apache(http://httpd.apache.org/)
  • PHP(http://www.php.net/)
  • MySQL(http://www.mysql.com/) --> 轉 https://mariadb.org/
  • PHPMyAdmin(http://www.phpmyadmin.net)

這些程式是他的官方網站資料,在 Linux Mint OS 中已經有提供LAMP已經編譯好的套件程式,只需要透過 apt-get 的軟體安裝即可方便的安裝完成。

安裝之前
1. 設定好你的網路及網路名稱
sudo /etc/init.d/networking restart  可以重新啟動你的網卡
ifconfig  可以查詢你的網卡資訊
mtchang@machine ~ $ sudo nano /etc/hosts
127.0.0.1 localhost
127.0.0.1 mtchang-virtual-machine
127.0.0.1 mini.jangmt.com
192.168.189.129 mint.jangmt.com

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

* 查詢網卡資訊 ifconfig
mtchang@machine ~ $ ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0c:29:a4:23:2d  
          inet addr:192.168.189.129  Bcast:192.168.189.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fea4:232d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:990 errors:0 dropped:0 overruns:0 frame:0
          TX packets:936 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:93782 (93.7 KB)  TX bytes:122551 (122.5 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:207 errors:0 dropped:0 overruns:0 frame:0
          TX packets:207 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:29195 (29.1 KB)  TX bytes:29195 (29.1 KB)

* 啟動 apache 的服務
mtchang@machine ~ $ sudo /etc/init.d/apache2 restart
 * Restarting web server apache2                            [ OK ] 
* 接下來用瀏覽器測試
http://localhost/index.php 應該會有 linux 的測試畫面出現。(localhost 只能在本機用,如果試用別機器測試請輸入他的 ip )


* 再 建立 php 的測試檔案, /var/www/html 目錄為 apache2 的家目錄
mtchang@machine /var/www/html $ sudo nano /var/www/html/index.php
<?php 
echo "hello linux";
phpinfo(); 
?>

到這裡 apache2 及 php 可算是可以工作了。

電腦端立院 IVOD GUI 下載器 (win portable)

電腦端立院 IVOD GUI 下載器 (win portable)


我依據這篇 https://g0v.hackpad.com/IVOD-KdTs5gZb3yw 的程式碼及概念

用 PHP 改寫了一個 protable 版本可以在 windows 7 64bit 運作的

圖形界面下載器,他是用 php web 服務做的本機端下載程式。


---- 2014.10.4 更新 ----

找到一個 phpdesktop 的工具,把php 打包起來,看起來更像是一個桌面程式了。http://code.google.com/p/phpdesktop/

另外修正一些小 bug ,重新打包釋出 ivod v0.3 版本。請解開後執行 run.exe 就可以開始抓了。

另外推薦最近熱門影片:範例網址(2):http://ivod.ly.gov.tw/Play/VOD/76446/300K (韓國已發展8G,台灣現在才推動4G是否太落伍?04:21秒起) 看新聞不清楚拉,直接看最了解為何 IT 人員會崩潰的原因,且可以作為資訊教育的案例宣導。

--------


使用方式:

(1) 下載這個檔案,完成後在你的電腦解開這個程式。


* 0.3版下載(2014.10.5 update) --> 解開後執行 run.exe 就可以了。

* 0.2 版下載 (2014.9.27 update)


這一版加入了中文檔名命名、改善啟動流程、跑起來更順了.....


(2) 找到目錄中的 run.bat 檔案執行它,它會啟動 php web 服務。並且帶出瀏覽器引導你進入 http://localhost:8000/ 。






(3) 去立法院的 IVOD 選各想要下載的網址






(4) 丟到你本機的下載器內,它會先檢查一下內容,並引導你下載它。





我的環境是 win7 64bit eng. 目前測試用 chrome 和 firefox 最新版下載沒有問題。如有問題,請留言告訴我巴。












謝謝。

2014/10/14

Vulnerability Scanning Tools(漏洞掃描工具)

https://www.owasp.org/index.php/Category:Vulnerability_Scanning_Tools
寫得很清楚

但 OpenSource 就只有幾個


  1. http://rgaucher.info/beta/grabber/
  2. http://sourceforge.net/p/grendel/code/ci/c59780bfd41bdf34cc13b27bc3ce694fd3cb7456/tree/
  3. http://www.cirt.net/nikto2
  4. https://subgraph.com/vega/
  5. http://wapiti.sourceforge.net/
  6. http://www.sensepost.com/research/wikto/
  7. https://www.owasp.org/index.php/OWASP_Xenotix_XSS_Exploit_Framework
  8. https://www.owasp.org/index.php/OWASP_Zed_Attack_Proxy_Project


先推薦兩個:
1. vega
https://subgraph.com/vega/ 跨平台,用 java 寫的,圖形化界面。



補充一個,無線網路偵測的
https://www.fern-pro.com/download.php