Exemple #1
0
func handlePacket(handle *pcap.Handle, packet gopacket.Packet) {
	ethLayer := packet.LinkLayer()
	if ethLayer == nil {
		return
	}
	eth, ok := ethLayer.(*layers.Ethernet)
	if !ok {
		return
	}

	ipLayer := packet.NetworkLayer()
	if ipLayer == nil {
		return
	}

	ip, ok := ipLayer.(*layers.IPv4)
	if !ok {
		return
	}

	tcpLayer := packet.Layer(layers.LayerTypeTCP)
	if tcpLayer == nil {
		return
	}
	tcp, ok := tcpLayer.(*layers.TCP)
	if !ok {
		return
	}

	// 服务器回应 SYN 请求
	if tcp.SYN == true && tcp.ACK == true {
		ttl := uint8(0)

		switch {
		case ip.TTL > 128:
			//Solaris/AIX
			ttl = 254 - ip.TTL
		case ip.TTL > 64:
			// windows
			ttl = 128 - ip.TTL
		default:
			// linux
			ttl = 64 - ip.TTL
		}

		switch {
		case ttl > 10:
			ttl -= 4
		case ttl > 5:
			ttl -= 2
		case ttl > 2:
			ttl -= 1
		default:
			return
		}

		ack := tcp.Seq + uint32(len(tcp.Payload)) + 1
		data := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
		seq := tcp.Ack
		sendPacket(handle, eth.DstMAC, eth.SrcMAC, ip.DstIP, ip.SrcIP, tcp.DstPort, tcp.SrcPort, ip.Id+123, ttl, seq, ack, 258, data)
		seq += 2048
		sendPacket(handle, eth.DstMAC, eth.SrcMAC, ip.DstIP, ip.SrcIP, tcp.DstPort, tcp.SrcPort, ip.Id+123, ttl, seq, ack, 258, nil)
		//go fmt.Printf("伪重置 %v:%v 的 tcp 连接。\r\n", ip.SrcIP, tcp.SrcPort)
	}
}