2010年4月10日 星期六

FTP server + PHP 新檔通知

先作一個遞迴列回資料夾和檔案名稱的程式:list.php
$today=system("date +%Y-%m-%d");
$yesterday=("date --date=\"1 day ago\" +%Y-%m-%d");


//getDirectory( "." );
getDirectory( "/var/lib/mldonkey/incoming/files" );



function getDirectory( $path = '.', $level = 0 ){

$ignore = array( 'cgi-bin', '.', '..' );
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.

$dh = @opendir( $path );
// Open the directory to the handle $dh

while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory

if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored

$spaces = str_repeat( '', ( $level * 4 ) );
// Just to add spacing to the list, to better
// show the directory tree.
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...

//echo "Directory:$spaces $file\n";
getDirectory( "$path/$file", ($level+1) );
// Re-call this same function but on a new directory.
// this is what makes function recursive.

} else {
echo "$spaces$file\n";
// Just print out the filename

}

}

}
closedir( $dh );
// Close the directory handle

}

?>

再來,做一個CheckNewFile.sh
#!/bin/sh
today=`date +%Y-%m-%d`
yesterday=`date --date="1 day ago" +%Y-%m-%d`
/usr/bin/php /root/list.php > /tmp/$today.log
/usr/bin/find /var/lib/mldonkey/incoming/files/ -name `diff -u /tmp/$yesteray.log \ /tmp/$today.log | grep +File: | awk -F: '{print $2}' ` | awk -F/files/ '{print $2}' > \ /tmp/$today-check.log
/usr/bin/test -s /tmp/$today-check.log && /usr/bin/php /root/MailCheckFile.php

如果今天的check.log是空的話,就不通知了。如果不是空的話,
再執行MailCheckFile.php(用PHPMailer改的程式),寄出通知信。

最後再做一件事,編輯crontab

0 8 * * * /bin/sh /root/CheckNewFile.sh

呼,這樣就好了。PHP+shell script是很強大的!

2010年1月18日 星期一

2010 一月份的 SA@Tainan –雙連發駭客終極解碼心得

在ptt上看到有人推文這個課程,剛好又在台南。就馬上拉了max和ken參加。
沒有想到,報名時需要有domain的人才能參加(據max說因為是twnic協辦的關係)
於是跟ram借了帳號密碼(在此感謝ram的幫忙)

早上是blueT的課程,blueT是Ubuntu的推廣者(也是Perl 愛好者),相當讓人佩服的人。
我對於願意貢獻出自己的時間和精力的人,總是相當敬佩的。

同課程的kevin和楊姓少年(現在應該是楊姓青年了,抱歉,真的忘了怎麼稱呼)
也大大方方貢獻出自己的心得,在職場久了就會發現,願意和別人分享自己所學所得的,真的是很了不起的人。我後來發現,我參加類似的課程都是在抄別人用什麼軟體的,我果然是個script middle-ager)

現場當然是有一些示範。不過也許是我比較容易操心,總覺得許多年輕人會不會就這樣一種栽入黑的世界…但願是我多慮。

blueT也用 HIT2009的幾題來講解一些手法。例如game3的解法,我其實不知道kill -11 的用法,真的是上了一課。不過呢....我也沒有解過那一題。(我大概也解不出來,所以我後來跑去問newbug)
newbug說:
"ulimit -c unlimited
再kill -9 吧"
try it

應該去要個試題的說 XD

下午場次是CIH的課,這是我第二次聽CIH的課程,上一次是在HIT2009,聽他講P2P Botnet的概念。
CIH是讓人很感動的人,連第一次聽他上課的ken,都感覺的出 CIH的熱情和執著。
唯偏執者得以倖存,不曉得這句話用在這裡適不適當。

CIH的邏輯真的很強,聽他上完課,我才發現我以前考的LPI原來有很多觀念是可以用在這些地方的。另外他的學習曲線和一般人明顯的不一樣,要模仿和學習可能都很困難。不過聽邏輯好的人講話真是舒服,希望我自己也能一直向著這個目標前進。

CIH介紹的是在linux環境下,如何修正程式的檢查。用的是ptrace函數。
用的方法在我腦中浮現的是:
1、用ptrace,找出ls這個程式每一行程式碼的位址(base address + library address + program address),然後把它存下來。這邊的library指的是ld-linux,其它程式不見得是它。
2、再比較兩種不同的輸入得到的不同輸出結果,用diff找出其分歧點。
3、用gdb,把分歧點設成中斷點。再設定的eip,讓他跳過的那一段程式碼,直接執行的程式碼。。
4、最後計算出要修正的碼(從je或jne變成jmp,然後把位址塞到剛好,避免因長度不一樣而無法執行,據說和-o這個參數有關?)
5、用mc或vi (%xddi)來改hex code

這其中學到一個方法很有趣,就是 CIH用來查每個CPU的ASM CODE的方法。
用一個while(1),在上下都asm("nop");,然後用objdump看他的結果。

另外,CIH用hello world(事實上是輸入數字作判別)來取代比較有問題的ls(原因是ls會讀,這一個應變手法,也讓我學到一課。

晚上和ken和max去吃義大利麵,感謝ken大的招待(別人請你吃飯或借你東西才會加個大字,我果然是個現實的人 ^_^)

晚上回家時運氣很好,自強號剛好誤點,被我們坐到,在八點前就到高雄了,和max一起行動總有好事發生。

記一個手法:
readelf -a /bin/ls | grep less
再/text,可以看執行時的address
也可以看 Entry point address

記一個觀念:
cat /proc/1946/maps
可以看到1946這個process讀了哪些so,和他們怎麼配置在記憶體中。