2010年11月26日 星期五

SSH穿越多台主機的連線方法

ssh的連線我相信很多人都很熟悉,尤其是有管理unix like的主機或是伺服器,不過我最近才知道有很方便的穿透連線方式,在這裡記錄一下也與大家分享!

 

假設條件:
  1. 公司內部環境(Client)無法直接連線到DMZ(Server B)與暴露在網路上的伺服器(Server C)。
  2. Client連線到DMZ主機需透過Server A才能到Server B。
  3. Clinet連線到外網須透過Server A到Server B才能到Server C。
我 假設的條件是一個非常普通的例子,不管大家面對到的架構如何,都有可能遇到要到某一台Server必須先連到某一台Server,而以往最一般的做法,就 是從Client連線到Server A,之後在Server A內敲打指令再連線到Server B,若是需要再連到Server C的話也要在Server B內敲打指令,這樣的情形不知道大家是否遇到過?也對於這樣跳來跳去的動作感到很繁瑣,其實ssh本身就有快速穿透連線模式。
  • Client -> Server A
    ssh user@ServerA
  • Client -> Server A -> Server B
    ssh -t user@ServerA ssh user@ServerB
  • Client -> Server A -> Server B -> Server C
    ssh -t user@ServerA ssh -t user@ServerB ssh user@ServerC
  • Client -> Server A(port:1234)-> Server B(port:3456)-> Server C(port:5678)
    ssh -t user@ServerA -p 1234 ssh -t user@ServerB -p 3456 ssh user@ServerC -p 5678
主要就是利用-t的參數來達成穿透式的連線,當然還是需要敲密碼的!若是要更快速的連線,或許可以加入ssh的認證,這樣穿透可以更快速,讓三行指令縮成一行指令就可以,而且就算非預設的22 port也都可以!有覺得比較快嗎?雖然說敲指令時效思考一下,不過習慣就好囉!

p.s.通常若是只有兩層,只需要連線到DMZ的主機,我會建立Cient與Server A之間的ssh tunnel,之後到DMZ的多台主機就直接透過tunnel去連線,也是相當快速!



ssh-copy-id with port

$ cp /usr/bin/ssh-copy-id /usr/bin/ssh-copy-id.orig
$ vi /usr/bin/ssh-copy-id

#!/bin/sh

# Shell script to install your identity.pub on a remote machine
# Takes the remote machine name as an argument.
# Obviously, the remote machine must accept password authentication,
# or one of the other keys in your ssh-agent, for this to work.

ID_FILE="${HOME}/.ssh/identity.pub"

while getopts ':i:p:P:h' OPTION
do
    case $OPTION in
        i)
        if [ -n "$OPTARG" ]; then
            if expr "$OPTARG" : ".*.pub" > /dev/null ; then
                ID_FILE="$OPTARG"
            else
                ID_FILE="$OPTARG.pub"
            fi
        fi
        ;;
        P|p)
            PORT=$OPTARG;
        ;;
        h)
            echo "Usage: $0 [-i [identity_file]] [user@]machine" >&2
            exit 1
        ;;
    esac;
done;

shift $(($OPTIND - 1))

if [ $# -lt 1 ] && [ x$SSH_AUTH_SOCK != x ] ; then
   GET_ID="$GET_ID ssh-add -L"
fi

if [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then
  GET_ID="cat ${ID_FILE}"
fi

if [ -z "`eval $GET_ID`" ]; then
  echo "$0: ERROR: No identities found" >&2
  exit 1
fi

if [ -z $PORT ]; then
    PORTOPTION=""
else
    PORTOPTION="-p $PORT "
fi;

{ eval "$GET_ID" ; } | ssh $PORTOPTION $1 "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys" || exit 1

cat <
Now try logging into the machine, with "ssh $PORTOPTION'$1'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

EOF

2010年11月25日 星期四

Management Data Input/Output

轉錄於Nano雞排's Blog

Management Data Input/Output簡稱MDIO,MDIO提供MAC(Media Access Control)如何去存取PHY的標準,制定於802.3裡面。

這張圖我想表達MAC/PHY之間的關係,MAC屬於data link層,PHY屬於Physcial層。


MDIO有兩組訊號線,MDC(Management Data Clock),由圖就可以理解是由STA(Station Management)提供的,而且和TX_CLK/RX_CLK無關。MDIO是一個bidirectional signal,用於PHY和STA之間傳送控制訊號。


frame format

PRE (preamble)STA的每一個指令都會先送出連續32bit的1和PHY取得同步。
ST (start of frame)算是一個pattern,告知PHY這個後面就是MDIO frame了。
OP (operation code)10為read,01為write。
PHYAD (PHY Address)這個MDIO bus上面每個PHY都要有獨一無二的address,讓STA辨識,5個bit,所以最多可有32個PHY在這一組MDIO bus上面。
REGAD (Register Address)5個bit,允許每個PHY可以有32組register。
TA (turnaround)當PHY執行wtite時,TA就是單純的送出10,而當PHY是要read時,STA和PHY會在第一個bit會輸出高阻抗(high-impedance),PHY接著會在第二個bit送出0,後面PHY就會吐出data給STA了,看下面的時序圖就會清楚了。


圖檔來源:http://www.tech-regal.com/post/Ethernet-MII.html


參考資料:
  1. IEEE 802.3, http://standards.ieee.org/getieee802/download/802.3-2008_section2.pdf
  2. http://www.tech-regal.com/post/Ethernet-MII.html
  3. http://en.wikipedia.org/wiki/Management_Data_Input/Output

2010年11月16日 星期二

Colasoft packet builder

Colasoft packet builder是十分實用的軟體,透過它可以讓你快速的測試網路協定,基本上,你可以把它看做是packet generator,你只需要把wireshark的capture log餵給它,你就可以任意修改在log中的封包並傳送
打開軟體後先import一個packet cap檔


隨便選擇一個封包然後在下方的editor中編輯欄位如ip,mac…


在封包上點選右鍵選擇傳送,就可以把封包丟到受測平台


這套軟體可以節省protocol驗證的時間,不用特別去寫packet generator,更重要的是,它是free tool.

From http://daydreamer.idv.tw/rewrite.php/read-61.html

2010年11月11日 星期四

iptables with l7-filter

簡介
Linux netfilter的外掛模組,可讓iptables對應用層過濾分析,像是p2p,im,..等
(http://l7-filter.sourceforge.net/)

安裝l7-filter需要以下檔案
Linux kernel source(http://www.kernel.org)
iptables source(http://netfilter.org)
netfilter-layer7(http://sourceforge.net/project/showfiles.php?group_id=80085)
l7-protocols definitions(http://sourceforge.net/project/showfiles.php?group_id=80085)

ps:Kernel compatibility
http://l7-filter.sourceforge.net/kernelcompat

..................................................................................................
安裝步驟

1
先下載所需要的檔案

2
kernel部份
ps:需已安裝過kernel source,並讓/usr/src/linux可指向核心
2.1修正核心patch
用netfilter-layer7-vX.Y/kernel-2.6.*-layer7-X.Y.patch修正核心patch
ps:patch版本必須與目前核心相同,可看netfilter-layer7-vX.Y/readme
2.2重新編譯核心,並增加Layer 7 match support
2.3開始編譯核心
2.4確認開機檔是用新的核心,並reboot

3
iptables部份
3.1修正iptables的patch
用netfilter-layer7-vX.Y/iptables-layer7-X.Y.patch修正iptables source,並安裝修正過的iptables
3.2將iptables指令,指向修正過的iptables
3.3安裝l7-protocols definitions
預設會裝在/etc/l7-protocols/

.......

安裝過程大致如下
1
下載以下檔案後解壓縮到/usr/local/src
netfilter-layer7-v2.0
iptables-1.4.0
l7-protocols-2005-12-16

2.1
cd /usr/src/linux
patch -p1 < /usr/local/src/netfilter-layer7-v2.0/kernel-2.6.13-2.6.14-layer7-2.0.patch
2.2
make mrproper
make oldconfig
會出現 Layer 7 match support (EXPERIMENTAL)... [N/m/?] (NEW) -> 可選m或按enter
make menuconfig
確定是否為 Layer 7 match support
2.3
make clean
make bzImage
make modules
make modules_install
make install
2.4
reboot

3.1
cd /usr/local/src/iptables-1.4.0
patch -p1 < ../netfilter-layer7-v2.0/iptables-layer7-2.0.patch
chmod +x ./extensions/.layer7-test
make clean
make KERNEL_DIR=/usr/src/linux
make install KERNEL_DIR=/usr/src/linux
3.2
cd /sbin
mv iptables iptables.old
ln -s /usr/local/sbin/iptables iptables
3.3
cd /usr/local/src/l7-protocols-2005-12-16
make install

.......................................................................................
用法
-m layer7 --l7proto < protocol> -j < target>
簡介
Linux netfilter的外掛模組,可讓iptables對應用層過濾分析,像是p2p,im,..等
(http://l7-filter.sourceforge.net/)

安裝l7-filter需要以下檔案
Linux kernel source(http://www.kernel.org)
iptables source(http://netfilter.org)
netfilter-layer7(http://sourceforge.net/project/showfiles.php?group_id=80085)
l7-protocols definitions(http://sourceforge.net/project/showfiles.php?group_id=80085)

ps:Kernel compatibility
http://l7-filter.sourceforge.net/kernelcompat

..................................................................................................
安裝步驟

1
先下載所需要的檔案

2
kernel部份
ps:需已安裝過kernel source,並讓/usr/src/linux可指向核心
2.1修正核心patch
用netfilter-layer7-vX.Y/kernel-2.6.*-layer7-X.Y.patch修正核心patch
ps:patch版本必須與目前核心相同,可看netfilter-layer7-vX.Y/readme
2.2重新編譯核心,並增加Layer 7 match support
2.3開始編譯核心
2.4確認開機檔是用新的核心,並reboot

3
iptables部份
3.1修正iptables的patch
用netfilter-layer7-vX.Y/iptables-layer7-X.Y.patch修正iptables source,並安裝修正過的iptables
3.2將iptables指令,指向修正過的iptables
3.3安裝l7-protocols definitions
預設會裝在/etc/l7-protocols/

.......

安裝過程大致如下
1
下載以下檔案後解壓縮到/usr/local/src
netfilter-layer7-v2.0
iptables-1.4.0
l7-protocols-2005-12-16

2.1
cd /usr/src/linux
patch -p1 < /usr/local/src/netfilter-layer7-v2.0/kernel-2.6.13-2.6.14-layer7-2.0.patch
2.2
make mrproper
make oldconfig
會出現 Layer 7 match support (EXPERIMENTAL)... [N/m/?] (NEW) -> 可選m或按enter
make menuconfig
確定是否為 Layer 7 match support
2.3
make clean
make bzImage
make modules
make modules_install
make install
2.4
reboot

3.1
cd /usr/local/src/iptables-1.4.0
patch -p1 < ../netfilter-layer7-v2.0/iptables-layer7-2.0.patch
chmod +x ./extensions/.layer7-test
make clean
make KERNEL_DIR=/usr/src/linux
make install KERNEL_DIR=/usr/src/linux
3.2
cd /sbin
mv iptables iptables.old
ln -s /usr/local/sbin/iptables iptables
3.3
cd /usr/local/src/l7-protocols-2005-12-16
make install

.......................................................................................
用法
-m layer7 --l7proto < protocol> -j < target>
可過濾的protocol可參考http://l7-filter.sourceforge.net/protocols
常見p2p protocol如下
gnutella 可阻擋foxy
bittorrent 可阻擋bittorrent 通訊協定
fasttrack 可阻擋kazaa
edonkey 可阻擋eDonkey,eMule
xunlei 可阻擋迅雷
ex:
在prerouting封鎖即時通
iptables -t mangle -A PREROUTING -m layer7 --l7proto yahoo -j DROP
不讓edonkey能進入本機
iptables -I INPUT -m layer7 --l7proto edonkey -j DROP
封鎖foxy
iptables -t mangle -A PREROUTING -m layer7 --l7proto gnutella -j DROP
iptables -t mangle -A POSTROUTING -m layer7 --l7proto gnutella -j DROP




其他參考
http://l7-filter.sourceforge.net/L7-HOWTO-Netfilter



可過濾的protocol可參考http://l7-filter.sourceforge.net/protocols
常見p2p protocol如下
gnutella 可阻擋foxy
bittorrent 可阻擋bittorrent 通訊協定
fasttrack 可阻擋kazaa
edonkey 可阻擋eDonkey,eMule
xunlei 可阻擋迅雷
ex:
在prerouting封鎖即時通
iptables -t mangle -A PREROUTING -m layer7 --l7proto yahoo -j DROP
不讓edonkey能進入本機
iptables -I INPUT -m layer7 --l7proto edonkey -j DROP
封鎖foxy
iptables -t mangle -A PREROUTING -m layer7 --l7proto gnutella -j DROP
iptables -t mangle -A POSTROUTING -m layer7 --l7proto gnutella -j DROP




其他參考
http://l7-filter.sourceforge.net/L7-HOWTO-Netfilter







from http://systw.net/note/af/sblog/more.php?id=124

2010年11月10日 星期三

Read ext3/ext4 Partition from Windows

Ext2Read is an explorer like utility to explore ext2/ext3/ext4 files. It now supports LVM2 and EXT4 extents. It can be used to view and copy files and folders. It can recursively copy entire folders. It can also be used to view and copy disk and file


How to repair and clone disk with ddrescue

  ddrescue  is a tool that can be used to repair and clone disks on a  Linux system . This includes hard drives, partitions, DVD discs, flas...