星期四, 12月 23, 2010

Do things in Dir by C

C語言中如何處理目錄中每筆記錄

在freeBSD裡面的man中說明:
Sample code which searches a directory for entry ``name'' is:
DIR* dirp;
struct direct* dp;
len = strlen(name);
dirp = opendir(".");
while ((dp = readdir(dirp)) != NULL)
if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
(void)closedir(dirp);
return FOUND;
}
(void)closedir(dirp);
return NOT_FOUND;


十分簡單卻有用。

星期三, 11月 10, 2010

修改已經存在的table -- django-evolution

在manage.py syncdb中,他並不會修改已經存在的table。

所幸有位救星出來了:django-evolution

他可以在修改model後,新增或減少欄位,都直接去alter table。

安裝方法如下:
easy_install -U django_evolution

使用步驟如後:
1. Add django_evolution to the INSTALLED_APPS for your project
2. Run ./manage.py syncdb
3. Make modifications to the model files in your project
4. Run ./manage.py evolve --hint --execute

記得,新增的欄位要給default值,否則要設定null=True,
否則會出現initial value沒有指定的錯誤。

參考:django-evolution

星期五, 10月 22, 2010

Django Short names for ENGINE DeprecationWarning

Django在更新到某版後,跑shell會出現如下訊息:

/usr/lib/python2.5/site-packages/django/db/__init__.py:59: DeprecationWarning: Short names for ENGINE in database configurations are deprecated. Prepend default.ENGINE with 'django.db.backends.'
DeprecationWarning

為了解決它,只要在setting.py裡面更改Engine的名稱即可。

'ENGINE': 'django.db.backends.sqlite3'

之後Short name格式都會取消支援,所以就記得在前面加上
django.db.backends.

星期三, 10月 20, 2010

利用ssh在local跑遠端上的GUI軟體

今天看了hak5,才發現原來ssh有這麼好用的參數。

$ssh -v -X name@host
$firefox &

執行完後,就會發現在本機端可以跑起ff來了!

重點是即使沒有安裝,仍然沒問題。

因為其實ff本身還是在ssh主機上面跑的。

當然,附註一下,要在windows下這樣跑也是可以的。

只要安裝xming和下載pietty,

在pietty連線時候enable x11-forwarding即可。

星期二, 10月 12, 2010

Catalyst 2960-G 開機順序

Pre-Work
準備一台PC及網路線。

開始吧。

1.確認所有網路線都已經被拔除
2.在開機期間因為Switch會以DHCP server的方式運作,所以如果原本PC有static IP的話要調整成DHCP。
在此記得先把static IP抄下來,之後可能會用到。
3.插上Switch電源,大約30秒後會開始自我測試。經過五分鐘後,應該只剩SYSTEM LED是亮著穩定的綠燈,其他都要是關的了。(唯一的例外是STACKMASTER LED,如果它也亮著穩定的綠燈,表示switch 正在用stack mode運作,而且本身是stack master。)
4.按下mode鍵,直到所有LED燈穩定亮著綠色為止。(有些swtich可能RPS LED會不亮。)
5.把網路線插上PC及switch的任一port,switch上該port的LED會閃綠燈,表示已經成功連接。
6.打開瀏覽器,在網址列輸入http://10.0.0.1,可以進入設定網頁。(如果沒有Enable javascript的話會無法順利進入。)
7.下列欄位是必要輸入的:
VLAN ID:預設是1,建議在設定期間先不要改,因為設定期間VLAN是唯一的。之後如果要改的話,改的範圍從1~1001皆可。
IP address:就是IP位置。
Subnet Mask:子網路遮罩。
Default Gateway
Switch password:1~25個字元,大小寫有差,除開始結束外其他地方可有空白字元。(原本預設是cisco)
8.按submit後即可完成設定

記得在此之後,就需要改成用剛才設定的IP位置才能http到switch的設定介面。

星期五, 10月 08, 2010

error while loading shared libraries: ...so : cannot open shared object file

當新增so到lib之後,

系統會需要被通知更新一下,否則可能仍會有安裝完新的lib so後仍出現

error while loading shared libraries: ...so : cannot open shared object file

這樣的訊息。

只要跑

#ldconfig

讓系統重新去確認一下即可。

星期四, 9月 16, 2010

execl vs excvp

These two will search for prog on current PATH
execlp(prog, arg0, arg1, arg2, ...)
execvp(prog, argList)

簡單說
execvp可以傳argument list,而exclp卻需要把參數一個一個傳進去。

Mac terminal cursor hot key

Below test in the bash shell.

back to the begin of current command: ctrl + a
go to the end of current command: ctrl + e
back one word : esc + b
forward one word : esc + f

Format usb under mac os x in command line

This is the command:

$ diskutil eraseVolume MS-DOS COOKIES /Volumes/COOKIES

eraseVolume filesystem name device

會讓usb變成fat32的fs

星期五, 9月 10, 2010

Kismac -- Mac OS X 上的無線探測工具

除了Wireshark以外,MAC上還有這套頗好用的工具。

雖然此工具和Kismet沒有關係,但此工具提供不少不錯的功能:

1. 簡單明瞭的GUI
2. 支援GPS
3. Crack of some protocol
在選單下,收集足夠的packet以後就可以進行crack WEP, LEAP。
4. de-authentication attack
5. 可以在MAC下進行rfmon mode (不用和任何AP associate即可sniffing traffic)

星期二, 7月 20, 2010

MAC 下的三種套件管理

目前比較多人使用的分別為homebrew, fink, macport

來記錄一下。

homebrew:
個人覺得最好的,因為會盡量使用系統中的程式,而非像其他兩種都各自用自己的,
減少了許多重複安裝的麻煩。

fink:
通常安裝好後的位置都放在/sw中,如果之後要移除直接把/sw刪除就好了。

macport:
安裝好的會在/opt/local裡面。

想要的軟體通常這三個裡面就會有收集了!

MAN page FixME

在用MAN page時出現了該訊息,而且很多內容看起來都怪怪如下

NMAP(1) [FIXME: manual] NMAP(1)


因此用來看看是為什麼,出現如下
cacaegg$ man -d nmap
Reading config file /private/etc/man.conf
Looked whether there exists a message catalog man, but there is none
(and for English messages none is needed)

found man directory /usr/share/man
found man directory /usr/local/share/man
found man directory /usr/X11/man
found manpath map /bin --> /usr/share/man
found manpath map /sbin --> /usr/share/man
found manpath map /usr/bin --> /usr/share/man
found manpath map /usr/sbin --> /usr/share/man
found manpath map /usr/local/bin --> /usr/local/share/man
found manpath map /usr/local/sbin --> /usr/local/share/man
found manpath map /usr/X11/bin --> /usr/X11/man
found manpath map /usr/bin/X11 --> /usr/X11/man
found manpath map /usr/bin/mh --> /usr/share/man

using /usr/bin/less -is as pager

using /usr/bin/less -is as browser

using /bin/cat to dump HTML pages as text
path directory /usr/bin is in the config file
adding /usr/share/man to manpath

path directory /bin is in the config file

path directory /usr/sbin is in the config file

path directory /sbin is in the config file

path directory /usr/local/bin is in the config file
adding /usr/local/share/man/zh to manpath
adding /usr/local/share/man to manpath

path directory /usr/X11/bin is in the config file
adding /usr/X11/man to manpath

adding mandatory man directories

no cat page stored because of nonstandard line length
parsing directive from file /usr/local/share/man/zh/man1/nmap.1.old
found tbl(1) directive

not executing command:
(cd '/usr/local/share/man/zh' && (echo ".ll 18.8i"; echo ".nr LL 18.8i"; /bin/cat '/usr/local/share/man/zh/man1/nmap.1.old') | /usr/bin/tbl | /usr/bin/groff -Wall -mtty-char -Tascii -mandoc -c | (/usr/bin/less -is || true))

原來是zh中有nmap.1的檔,把它移除就可以了。

星期日, 5月 23, 2010

星期日, 5月 16, 2010

Ubuntu One on Aspire One can't scroll mouse

as title, 所以找了方法。

紀錄一下日後可用。

sudo modprobe -r psmouse
sudo modprobe psmouse proto=imps

如果可以的話,就把
options psmouse proto=imps
加到/etc/modprobe.d/options裡面,done.

ref:Link

星期日, 5月 02, 2010

HTTPS and SSL sniffing

延續上篇

可以spoofing dns以後,把有https的web site改到somehost

在somehost上,跑webmitm

webmitm會自行產生Certificate(沒有CA簽證,當然。)

webmitm會扮演好http & https proxy的角色

最後,在用ssldump -k webmitm.crt -d | grep pattern

就會過濾出....

這類攻擊的防線在於client要沒有認證的憑證網站就不要相信,

也就是瀏覽器在出現憑證沒有可信任的CA簽署時候,就不要繼續做傻事了

星期六, 5月 01, 2010

DNS Spoofing

用了幾項工具來完成。
1.dnsspoof
2.arpspoof
3.fragrouter

過程如下
1.設定好IP forwarding
#fragrouter -i eth0 -B1

2.接收LAN的封包
#arpspoof [spoof ip]
通常spoof的ip為LAN的router

3.將要改的dns放到dnsspoof.host檔案以後
#dnsspoof -f ./dnsspoof.host

OK.

星期六, 4月 10, 2010

Smash Stack Disable

-fstack-protector Ubuntu下的GCC compile時預設啟動了此參數,如果想要測試一些stack overflow的程式會出現問題如下:

*** stack smashing detected ***: ./a.out terminated
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x48)[0xb7f5b6d8]
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x0)[0xb7f5b690]
./a.out[0x80484cf]
[0x78787878]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:01 5620003    /home/cacaegg/src/a.out
08049000-0804a000 r--p 00000000 08:01 5620003    /home/cacaegg/src/a.out
0804a000-0804b000 rw-p 00001000 08:01 5620003    /home/cacaegg/src/a.out

解決方法就是在compile時加上-fno-stack-protector就可以了。

星期日, 3月 28, 2010

Download File and Check MD5

每次都要重複下載,使用md5sum來確認等重複的動作,乾脆寫個script來直接作吧!
#!/usr/bin/python
import os, sys, re, subprocess
#argument checking
if len(sys.argv) <= 1:
        print "Usage:%s URL [md5]" % (sys.argv[0])
        sys.exit(0)
]
print "Downloading..."
args = ["/usr/bin/wget", sys.argv[1]
p = subprocess.Popen(args)
p.wait()

print "Checking MD5..."
filename = re.split("/", sys.argv[1])[-1]
if len(sys.argv) == 3:
        md5file = open("tmp.md5", "w+")
        md5file.write("%s  %s\n" % (sys.argv[2], filename))
        md5file.close()
        p = subprocess.Popen(["/usr/bin/md5sum", "-c", "tmp.md5"])
        p.wait()
        os.remove("tmp.md5")
整個script做的動作是
1.下載檔案,並解析該檔名
2.如果有提供MD5就進行MD5 check

另外subprocess是2.4版後用來執行系統中程式用的
class subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
可以只給arg,用sequence的形式,好處是都可以直接自行指定stdin, stdout, stderr。

星期六, 3月 27, 2010

找出系統中所開之port

這是一件常遇到的事,以下分別記載兩種作業系統尋找的方式。

1.Ubuntu(Unix)
$netstat -nap
-n 使用數字表示
-a 所有正在listening and non-lietening的port
-p 顯示使用該socket之program的pid & program name

也可以
$lsof -i
-i 表示列出所有正在使用的TCP & UDP port
$lsof -p pid
-p 會列出更多關於該pid程式之詳細訊息,如執行檔、lib、設定檔等

2.M$ Windows
>netstat -nao | find "LISTENING"
-na不再贅述
-o 類似UNIX中-p的功能

星期日, 3月 21, 2010

使用tor & SASL上XChat

為了能在FreeNode上發言,又不想透露自己IP,當然要結合一下Tor。

步驟如下
1.在/etc/tor/torrc中
最後一行加上mapaddress 10.40.40.40 p4fsi4ockecnea7l.onion

2./etc/init.d/tor restart

3.打開xchat,新增一個Network,暫時叫做TorifiedFreeNode

4.點Edit進行編輯
在Serverlist中加上 10.40.40.40

5.因為要用Tor連到FreeNode還需要SASL驗證,所以要下載plugin
http://adipose.attenuate.org/~stephen/ircd-seven/sasl/cap_sasl.py

cd ~/.xchat2
wget http://adipose.attenuate.org/~stephen/ircd-seven/sasl/cap_sasl.py


6.重新開啟xchat

7.連上TorifiedFreeNode,在Command裡打上
/SASL -set TorifiedFreeNode Nick Password

就可以在irc上隱藏自己的位置囉!
PS.步驟七會出現Command Unknown是正常情況,小bug,不影響使用

參考:
http://freenode.net/irc_servers.shtml#tor
http://74.125.153.132/search?q=cache:xXSITmoGGEwJ:sleepyirc.net/wiki/SASL+sasl+xchat&cd=1&hl=zh-TW&ct=clnk&gl=tw

星期六, 3月 20, 2010

Backtrack環境設定

1. Vim
當然要先設定好Vim,預設似乎是安裝vin-tiny,所以

#apt-get install vim

來把Vim完整裝上去。然後在Home目錄下放.vimrc
關於設定參考了http://plog.longwin.com.tw/post/1/369

2. 中文字型
下載http://wiki.0x209.org/Ubuntu-Chinese_Font_Installer

之後執行此script就可以了。

3. FF最新版
因為預設只有裝3.0,所以需要一些小步驟。
Step 1.下載Ubuntuzilla然後進行安裝並用apt-get insstall -f去確認相依性。

# wget http://downloads.sourceforge.net/sourceforge/ubuntuzilla/ubuntuzilla-4.6.1-0ubuntu1-i386.deb?use_mirror=surfnet
# dpkg -i ubuntuzilla-4.6.1-0ubuntu1-i386.deb
# apt-get install -f


Step 2.執行script

# ubuntuzilla.py -a install -p firefox

會被安裝在/opt/firefox裡面。

最後如果要復原的話

# ubuntuzilla.py -a remove -p firefox

就可以了。

參考自http://forums.remote-exploit.org/backtrack-4-howto/24741-install-firefox-3-5-bt4-pre-final.html

星期日, 3月 14, 2010

Set MAC address of NIC

Linux中使用下列指令可以直接設定MAC Address


#ifconfig [interface_name] hw ether [desired_MAC_Address]


interface_name:是指定哪個網路介面

hw ether:此介面為ethernet

最後就是想要的MAC Address。

有些AP如果是用MAC作為連線阻擋的話,先sniff一陣子,再加上這個還是可以連的上的。

Wireless Crack Tool - aircrack-ng

在Ubuntu中使用Kismap,來找出現在身邊有誰。

接著安裝aircrack-ng系列,用apt會安裝上一系列程式。

使用airmon-ng, airodump-ng


/tmp$ sudo airmon-ng start wlan0 //開始monitor wlan0介面
Interface Chipset Driver

wlan0 Intel 3945ABG iwl3945 - [phy0]
(monitor mode enabled on mon5)
mon0 Intel 3945ABG iwl3945 - [phy0]
mon1 Intel 3945ABG iwl3945 - [phy0]
mon2 Intel 3945ABG iwl3945 - [phy0]
mon3 Intel 3945ABG iwl3945 - [phy0]
mon4 Intel 3945ABG iwl3945 - [phy0]


/tmp$ sudo airodump-ng -i -w ivs mon5 //可以得知會在mon5上, -i表示只抓ivs, -w 是log的prefix
/tmp$ sudo aircrack-ng -a 1 -b 00:16:01:F4:B0:2D ivs-04.ivs //-a 1表示只crack wep, -b是針對bsid, 最後就是目標log檔


如果抓取packet數量夠多的話,就可以破解無線網路了。

星期日, 3月 07, 2010

隱藏自己的IP(Tor + Polipo + Torbutton on Ubuntu)

可以讓我們隱藏自己的IP位置不被別人知道,當然缺點是因為位置都在國外就慢了些。

邏輯如下:

browser <=> Polipo <=> Tor <=> Proxy <=> Web Site

Tor安裝只要照下列步驟即可

先把

deb http://deb.torproject.org/torproject.org main

加入 /etc/apt/source.list中
再執行

gpg --keyserver keys.gnupg.net --recv 886DDD89
gpg --export A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89 | sudo apt-key add -
apt-get update
apt-get install tor tor-geoipdb vidalia

然後就可以經由vidalia來看看tor是否運作正常囉!
接著安裝Polipo,然後修改config檔

apt-get install polipo
cd /etc/polipo/
mv config config.example
wget https://svn.torproject.org/svn/torbrowser/trunk/build-scripts/config/polipo.conf
mv polipo.conf config
/etc/init.d/polipo restart


最後安裝Firefox的torbutton就可以隱藏自己的IP了!

Client Reconnaissance Tool - Spade

用來做recon的工具。

簡單介紹一下功能:

whois:列舉了許多whois db可直接提供查詢。

IP Block Whois:經由問whois找出誰有某特定群組之IP

NSLOOKUP:查詢domain name

DNS Zone Transfer:取得該domain所有資訊,要看該domain name server有沒有提供

Traceroute:提供中途經過的router

等等...

星期日, 2月 21, 2010

MBSA - Microsoft的弱點掃描

由Microsoft提供的系統掃描工具Microsoft Baseline Security Analyzer(MBSA),可以針對一台或多台掃描(可用Domain去指派)。

有這些掃描選項

Check for Windows administrative vulnerabilities:
檢查Guest帳號、檔案系統、檔案分享或是系統群組等設定

Check for weak passwords:
檢查空白或弱密碼,時間可能會依系統帳號多寡而定。直得注意的地方是如果有打開logon/logoff audit,此檢查會產生系統log。

Check for IIS administrative vulnerabilities
檢查IIS弱點。

Check for SQL administrative vulnerabilities
檢查SQL Server弱點,如authentication mode, sa account password status, and service account memberships.

Check for security updates
檢查有沒有安全更新還沒做的。

自訂Python的import path

在import時,python會去搜尋sys.path中的每個entry。
而sys.path又是從下述所串接起來的
1.執行程式的目錄
2.PYTHONPATH環境變數
3.標準程式庫目錄(安裝時候就已經決定了)

所以若要自行新增,就需增加PYTHONPATH這環境變數。

cacaegg@cacabook:~$ export PYTHONPATH="/home/cacaegg/lib"
cacaegg@cacabook:~$ echo $PYTHONPATH
/home/cacaegg/lib
cacaegg@cacabook:~$ python
Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['', '/home/cacaegg/lib', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/Numeric', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.6-gtk2-unicode', '/usr/local/lib/python2.6/dist-packages', '/usr/local/lib/python2.6/dist-packages/pygoogle']
>>> import python.lang
>>> quit()
cacaegg@cacabook:~$ ls lib/python/
__init__.py __init__.pyc lang.py lang.pyc


此外需注意,由於從python.lang這樣方式去import的,所以python目錄底下需要__init__.py檔案才行。(空白的就好)

星期五, 2月 19, 2010

用Vim開發Python

找了一下網路上文件,一共會有些plugin需要使用。
1.minibufexpl.vim
可以開多個檔案的時候會有tab出現,需要切換的話control+ww然後移到要切換的tab上即可,
或是用滑鼠點要切換的tab也可以。

2.taglist.vim
可以出現檔案中的函式,member等的樹狀結構。
安裝此plugin需要ctags,所以就先

$http://vim.sourceforge.net/scripts/script.php?script_id=273

再下載下來安裝。解壓縮後會有兩個檔案,一個是doc,另外一個是plugin。
plugin就放在$HOME/.vim/plugin/裏面,再把doc放到$HOME/.vim/doc裏面,
在此目錄下執行vim以後,輸入

:helptags .

如此以後才可以有help查。
接著就可以使用taglist了!要使用只要輸入:TlistToggle就可以切換出視窗了。

3.VimPdb
可以直接在Vim裏面debug囉。抓下來後把VimPdb.vim & VimPdb.py直接放到.vim/plugin資料夾中即可。
如果不行
a.先檢查Vim是否支援Python
:python print "hellp world"
看是否有錯誤訊息
b.如果出現如下錯誤訊息

Error message:

Error detected while processing /home/user/.vim/plugin/VimPdb.vim:
line 19:
E492: Not an editor command: import sys
line 20:
E319: Sorry, the command is not available in this version: python
sys.path.insert(0, r"/home/user/.vim/plugin")
line 21:
E492: Not an editor command: import VimPdb
Error detected while processing function PdbInitialize:
line 11:
E319: Sorry, the command is not available in this version: ^Ipython import sys
line 12:
E319: Sorry, the command is not available in this version: python
sys.path.insert(0, r"")
line 13:
E319: Sorry, the command is not available in this version: ^Ipython import
VimPdb
line 15:
.....省略

應該是vimpdb.vim的檔案格式有問題,用vim打開它,然後輸入

:set fileformat=unix
:w

就可以正常運作了。

4.pythoncomplete
怎麼可以少了autocomplete呢!
下載下來放在plugin資料夾後,需要autocomplete時,再按Ctrl+x Control+o就可以了,還有文件說明,好厲害!

5.自動縮排
vim能自動偵測*.py後,就可以.vimrc中加上
filetype indent on
就可以自動縮排了!

避免使用LM Representation

Windows 在密碼儲存的DB (SAM DB)中,一筆帳戶Entry會有兩個密碼欄位。
分別為LM Representation、NT Hash。
LM用來存14字元以下的密碼,在儲存時,
會先把不足的地方用空白去padding,
然後再拆成一半(7,7)分別加上parity bit去做DES。
可想而知,排列組合數大幅減少了。(因為只有7個字元而已)
加上LM會先轉成UPPERCASE,又減弱了密碼強度
因此除非有需要和Windows 98以前的系統溝通,否則建議把LM Representation拿掉的好。

Reg Key是HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa 裡NoLmHash的登錄機碼

設定為1即可。

PS:Windows 7 預設是 1