佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1551|回复: 2

BASH: Rogue DHCP Detector

[复制链接]
发表于 23-10-2013 07:16 PM | 显示全部楼层 |阅读模式
最近写了个简单的bash script, 想分享一下。
script 的流程是:
  • 自造一个 .pcap 文件 (text2pcap)
  • Sniff packet with filter (tcpdump)
  • Replay 之前制造的 .pcap 文件 (tcpreply)
  • 把 sniffed 到的 packet 处理+检测

以下的 script,基本上只有几个variable要更改:
  • tmp1file     <-- sniffed 的后第一个文件
  • tmp2file     <-- 加工处理,撤除一些没用的 column
  • tmp3file     <-- 加工处理,撤除重复的 packet (也就是来自一样的 DHCP server's packet)
  • thePHfile    <-- packet 的 HEX 文件
  • thePfile       <-- 从 HEX 转换成 .pcap 的文件
  • authoriseIP  <-- DHCP server 的 IP
  • IF               <-- Server network interface
  1. #!/bin/bash

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

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

  10. function pcktRepyCapt { # Packet Replay & Capture
  11.         /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
  12.         i=5     # The packet replay interval
  13.         while [ $i -ge 1 ]; do
  14.                 /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
  15.                 /usr/bin/tcpreplay --intf1=$IF $thePfile 2&>/dev/null   # Replay packet
  16.                 i=$(($i-1))
  17.         done
  18.         #/bin/kill `jobs -p` 2&>/dev/null       # Terminate the previous background task
  19.         /bin/kill `ps -A|grep tcpdump|awk '{print $1}'` 2&>/dev/null    # Terminate the previous background task
  20. }

  21. function processRaw {   # Remove unwanted column
  22.         while read -r myArray; do
  23. echo ${myArray:15}
  24.         done < $tmp1file
  25. }

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

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

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

  74. pcktGen         # Create packet (.PCAP) file
  75. pcktRepyCapt    # Packet Replay and Capture
  76. processRaw > $tmp2file 2>/dev/null      # Remove unwanted column
  77. queryUniq | /usr/bin/sort -u > $tmp3file 2>/dev/null    # Sort the packet and remove duplicate line
  78. pcktAnalyse     # Check IP
复制代码
希望可以帮到有需要的人,如果你有更好的意见或疑问,欢迎.
tq 本帖最后由 nick_khor 于 23-10-2013 07:16 PM 编辑

回复

使用道具 举报


ADVERTISEMENT

发表于 24-10-2013 11:43 AM | 显示全部楼层
tcpdump -w /tmp/DHCP-Request-6.pcap就可以省略pcap generation部分。要Hex的话,就用-X option。
回复

使用道具 举报

 楼主| 发表于 24-10-2013 05:05 PM | 显示全部楼层
chfl4gs_ 发表于 24-10-2013 11:43 AM
tcpdump -w /tmp/DHCP-Request-6.pcap就可以省略pcap generation部分。要Hex的话,就用-X option。

了解,可是为什么 tcpdump 可以省略 pcap generation 部分咧?
因为我的 pcap generation 是把 packet Hex 转换成 pcap, 然后再replay pcap + sniff。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 18-6-2024 04:30 PM , Processed in 0.059026 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表