Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Sunday, October 20, 2013

BASH: Rogue DHCP Detector

不久前在公司里有人的电脑中毒了,然后还会自行分发 DHCP 给其他电脑,搞得不上不下。 结果用 Wireshark 找到凶手把它干掉了。
可是不可能要自己24小时开着 Wireshark 吧? 而且我又要离开公司了,有点放不下心。 上网找了一些 rogue dhcp detection 的资料,只是要简单的 rogue dhcp 侦测。结果都没有我要的,Windows 的是有 (按我参考)。
只好写个自己的版本,script 的流程是:
  1. 自造一个 .pcap 文件 (text2pcap)
  2. 网络嗅探 with filter (tcpdump)
  3. 从播之前制造的 .pcap 文件 (tcpreply)
  4. 把嗅探到的 网络封包 (packet)  处理+检测
以下的 script,基本上你只有几个变数(variable)要更改:
  1. tmp1file     <-- 网络嗅探后第一个文件
  2. tmp2file     <-- 加工处理,撤除一些没用的纵行(column)
  3. tmp3file     <-- 加工处理,撤除重复的封包 (也就是来自一样的 DHCP 服务器的封包)
  4. thePHfile   <-- packet 的 HEX 文件
  5. thePfile       <-- 从HEX转换成.pcap的文件
  6. authoriseIP <-- 你 DHCP 服务器的 IP
  7. IF                  <-- 你 DHCP 服务器的网卡

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash

tmp1file='/tmp/dhcp-raw.tmp'    # The initial sniff result
tmp2file='/tmp/dhcp-result.tmp' # The column removed result
tmp3file='/tmp/dhcp-uniq.tmp'   # The result without duplicate packet
thePHfile='/tmp/DHCP-Request-6.txt'     # The packet HEX file
thePfile='/tmp/DHCP-Request-6.pcap'     # The packet .PCAP file
authoriseIP='192.168.0.1'         # Your authorised DHCP server's IP
IF='eth0'                       # Your server's network interface

/bin/rm $tmp1file $tmp2file $tmp3file $thePHfile $thePfile 2&>/dev/null

function pcktRepyCapt { # Packet Replay & Capture
        /usr/sbin/tcpdump -e -i eth0 "udp src port 67 && udp dst port 68" -nnq > $tmp1file 2>/dev/null &        # Sniff UDP packet, we want source port is 67 & destination port is 68, which is a DHCP offer behavior, and also the task to background
        i=5     # The packet replay interval
        while [ $i -ge 1 ]; do
                /bin/ping -c 2 127.0.0.1 >/dev/null     # delay for 2 second before proceed to packet replay, just in case the packet replay too fast and those DHCP servers are not able to receive your packet
                /usr/bin/tcpreplay --intf1=$IF $thePfile 2&>/dev/null   # Replay packet
                i=$(($i-1))
        done
        #/bin/kill `jobs -p` 2&>/dev/null       # Terminate the previous background task
        /bin/kill `ps -A|grep tcpdump|awk '{print $1}'` 2&>/dev/null    # Terminate the previous background task
}

function processRaw {   # Remove unwanted column
        while read -r myArray; do
                echo ${myArray:15}
        done < $tmp1file
}

function queryUniq {    # Sort the packet and remove duplicate line
        while IFS=$',' read -r -a myVar; do     # Read each line into array form and use the COMMA symbol as seperator
                if [ -z "${myVar[2]}" ]; then
                        #IF EMPTY THEN STOP
                        break
                fi
                echo ${myVar[0]}, ${myVar[1]}, ${myVar[2]}, ${myVar[3]}
        done < $tmp2file
}

function pcktAnalyse {  # Check IP
        while IFS=$',' read -r -a myVarr; do    # Reach each line into array form and use the COMMA symbol as seperator
                theIP=`echo ${myVarr[2]} | awk '{print $3}'`    # Basically the IP is located at the third column of the third column in each line
                theMAC=`echo ${myVarr[0]} | awk '{print $1}'`   # And the ethernet address is located at the first column of the first column in each line
                len2sub=`expr ${#theIP} - 3`
                thesub=${theIP:0:$len2sub}      # The extracted the IP come with the source port, remove for better display
                if [ $thesub != "$authoriseIP" ]; then  # IP comparison, if the IP is not the authorised IP, it will show the line below with ethernet & IP address
                        echo -e "There is non-authorised DHCP server in the network, MAC=$theMAC and IP=$thesub"
                        break
                fi
        done < $tmp3file
}

function pcktGen {      # Create packet (.PCAP) file
        srcMAC='aa bb cc dd ee ff'      # Source Ethernet Address
        dstMAC='ff ff ff ff ff ff'      # Destination Address (broadcast address)
        echo -e "0000  $dstMAC $srcMAC 08 00 45 00   .......PV..F..E.
0010  01 48 00 00 40 00 40 11 39 a6 00 00 00 00 ff ff   .H..@.@.9.......
0020  ff ff 00 44 00 43 01 34 9c bb 01 01 06 00 16 6d   ...D.C.4.......m
0030  44 66 00 04 00 00 00 00 00 00 00 00 00 00 00 00   Df..............
0040  00 00 00 00 00 00 00 50 56 97 00 46 00 00 00 00   .......PV..F....
0050  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
0060  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
0070  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
0080  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
0090  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
00a0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
00b0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
00c0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
00d0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
00e0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
00f0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
0100  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
0110  00 00 00 00 00 00 63 82 53 63 35 01 01 3d 07 01   ......c.Sc5..=..
0120  00 50 56 97 00 46 39 02 05 dc 3c 0d 64 68 63 70   .PV..F9...<.dhcp
0130  63 64 20 34 2e 30 2e 31 35 37 0b 01 79 21 03 06   cd 4.0.157..y!..
0140  0f 1c 33 3a 3b 77 ff 00 00 00 00 00 00 00 00 00   ..3:;w..........
0150  00 00 00 00 00 00                                 ......"> $thePHfile
        /usr/bin/text2pcap $thePHfile $thePfile 2&>/dev/null    # This command convert the HEX file into PCAP file
}

pcktGen         # Create packet (.PCAP) file
pcktRepyCapt    # Packet Replay and Capture
processRaw > $tmp2file 2>/dev/null      # Remove unwanted column
queryUniq | /usr/bin/sort -u > $tmp3file 2>/dev/null    # Sort the packet and remove duplicate line
pcktAnalyse     # Check IP
如果你任何疑问/有更好的方法,请email到我的电邮 nick_khor@hotmail.com 谢谢

Sunday, July 8, 2012

TC (Traffic Control) command, 限制网络速度

几年前为宿舍的网速问题而烦恼的时候,师傅说了一句
"怎么你自己不做一个 server 来玩?"
"怎样?什么 server?"
"做自己的 gateway, firewall, dns server 啦!"
(心想。。。反正没有做过,何不一试?)
经过了几个礼拜的琢磨,终于有了一点着落,但是限制网速的办法还是没有,只好用最牛的方法 (完全 command line) 来限制网速。仅仅用一个 TC command 来搞定。
对于这个 TC command,我到现在都还没完全了解,只是上网找了一找,然后一直 try n error 将近两个月才调到到比较稳定的变量;很 efficient,不会有人拿多速度。

可是问题是这个限制一次只给一整个 IP range / subnet,也就是说当限制 300kbits 的时候,整个IP range/subnet 共享一个速度 (300 kbits)。 这可不行,我的目标是每一个 IP 都有自己的速度,当他怎么下载,下载的几快都好,都不会影响别人的速度。

在不断的尝试下,以我有限的知识,我只能想到一个 IP 一个 rule 的办法 (最多 255 个 rules)。
就写了以下的 shell script 来省时省力。

let's say 我们的目标,network interface 是 eth1,然后 interface speed 是 100 mbits,只限制 192.168.1.21 到 192.168.1.50 的 IP,再给每一个受限制 IP 都享有最高 768 kbits 的速度。

以下红色部分是可更改的 (based on 我们的目标),其余的部分当然也可更改(个人建议维持这个变量)。

oK! 新手,自己消化!有问题请 email 到 nick_khor@hotmail.com

1:  #!/bin/sh  
2:  TC=/sbin/tc  
3:  startNum=21  
4:  DEV=eth1  
5:  IP=192.168.1.  
6:  endNum=50  
7:    
8:  LINERATE=100mbit  
9:  THROTTLERATE=768kbit  
10:  sfqMTU=1500  
11:  sfqInterval=10  
12:  avpktSize=1000  
13:  classPrio=5  
14:  filterPrio=16  
15:  allotByte=1500  
16:  weightSize=1000kbit  
17:    
18:  $TC qdisc del dev $DEV root >/dev/null  
19:  $TC qdisc add dev $DEV root handle 1: cbq avpkt $avpktSize bandwidth $LINERATE   
20:    
21:  while [ $startNum != $endNum ]; do  
22:       $TC class add dev $DEV parent 1: classid 1:$startNum cbq rate $THROTTLERATE avpkt $avpktSize cell 8 weight $weightSize allot $allotByte prio $classPrio bounded isolated  
23:       $TC filter add dev $DEV parent 1: protocol ip prio $filterPrio u32 match ip dst $IP$startNum flowid 1:$startNum  
24:       $TC qdisc add dev $DEV parent 1:$startNum sfq quantum $sfqMTU perturb $sfqInterval  
25:       echo "$IP$startNum is added"  
26:        startNum=$(($startNum+1))  
27:  done  

Thursday, April 15, 2010

pfsense 之 安装

我第一篇系统安装教程,多多包含!

简介:
pfsense 是一个以FreeBSD为基础的防火墙适合用作防火墙和路由器(router). pfsense project始于2004年的m0n0wall project的其中一个项目,但若注重于全面的个人电脑来说pfsense会胜过注重于嵌入式硬件(embeded hardware)的m0n0wall

pfsense功能:
-Firewall 防火墙
-State Table
-NAT(Network Address Traslation) 网络地址转换
-Load Balancing
-VPN (Visual Private Network)
-PPPoE Server
-RRD Graph Reporting
-Real Time Information - Using AJAX
-DDNS (Dynamic Domain Name Server)
-Captive Portal
-DHCP server (Dynamic Host Configuration Protocol)
/*有些不懂要怎样翻译,太难了*/

系统最低需求:
CPU - 100 MHz Pentium
RAM - 128 MB (有 load balancing 最好256MB)
Harddisk - 1.00 GB
CD-ROM - 1 unit
Network port - 2 unit (1 LAN, 1 WAN)
/*要求很低吧?你们家里随便一个旧电脑都可以拿来做pfsense,所以不要浪费~*/

本人用利用虚拟软件(virtual box)来安装pfsense,配置如下:
RAM - 512MB
Network port - 3 unit (1 LAN,2 WAN)
HARDDISK - 2.00GB
/*本人极力推荐用双核的cpu用虚拟,不然后悔莫及啊~*/

开始咯~
下载ISO然后烧录到CD,然后放入你要用的电脑boot
Photobucket

STEP 1选择 N,不设置Vlan
 Photobucket


STEP 2
LAN 和 WAN 一定要设置,通常我把第一个放LAN,第二个放WAN,其他的可以做DMZ(Demilitarized Zone),不过我今次要作load balancing,所以其他的一律做WAN.
LAN - em0
WAN - pcn0
OPT1 - pcn1
ENTER! 选择 Y
Photobucket



STEP 3选择99,正式安装
/*大部分设置都是optimize了的,所以我们只要跟着原厂设置就行了*/

Photobucket



STEP 4选择 < Accept there Settings >
STEP 5
选择 < Install pfSense >

STEP 6这是硬盘选择,绝大多都选第一个
STEP 7
选择 < Format this Disk >

STEP 8选择 < Use this Geometry >
STEP 9
选择 < Format ad0 >

STEP 10
选择 < Skip this Step > /*不用分区硬盘,除非另有用途*/

STEP 11
分区选择,只有一个可以选

STEP 12选择 < Accept and Create >
STEP 13
系统会问内核设置(kernel configuration setting)
如果你的CPU是单核的就选第一项 < Uniprocessor kernel >
如果你的CPU是双核的就选第二项 < Symmetric multiprocessing kernel >
其他的不懂
不过大多数都是第一个,因为很少人会用一个双核的电脑来安装pfsense,freeBSD的资源处理效率是最好的,远远超过microsoft和apple,能做到 low resources high efficiency 非freeBSB莫属。除非你真的是有钱啦,买个好的电脑来安装pfsense。
Photobucket


STEP 14
选择 <Accept and Install Bootblocks>


最后选择 < REBOOT > !
大功告成!
Photobucket


 

-待续 - 设置-

很简单吧?