drxt70655 2017-12-04 12:34
浏览 409
已采纳

如何在Golang中发送伪造的udp软件包

Im trying to send a fake udp (a random mac address, let's say 01:ff:ff:ff:ff:ff) package to be handled by the ServeDHCP on the server side, Im running the following dhcpv4 github repository github.com/krolaw/dhcp4.

The goal of send a discover package, is to check if the dhcp is alive.

In fact I created a new func called check

func (h *DHCPHandler) check () {
    con, err = net.Dial("udp", "127.0.0.1:67")
    for {
            //fake udp package???
            time.Sleep(10 * time.Minute)
    }

}

in the main of the function, I have the following call go handler.check()

And in the ServeDHCP I should pass these parameters:
func (h *DHCPHandler) ServeDHCP(p dhcp.Packet, msgType dhcp.MessageType, options dhcp.Options)

How could I send a fake upd package from the func check?

  • 写回答

1条回答 默认 最新

  • doutun1362 2017-12-05 13:45
    关注

    At the end I managed to make works this check to send in periods of 10 minuts an upd package with an mac address I know is gonna never be reached as following.

    func (h *DHCPHandler) check() {
        //Fetch parameters from config file
        config := getConfig() // here is a mac saved on a json file 01:ff:ff:ff:ff:ff
        macFake, err := net.ParseMAC(config.MacFake)
    
        if err != nil {
                fmt.Println("error with the fake mac provided on the json file", err)
        }
    
        // create connection
        conn, err := net.Dial("udp4", "127.0.0.1:67")
        if err != nil {
                fmt.Println("error with the connection", err)
        }
    
        //stay alive
        for {
                fmt.Fprintf(conn, "GET / HTTP/1.0
    
    ")
                conn.Write(dhcp.RequestPacket(dhcp.Discover, macFake, h.ip, []byte{0, 1, 2, 3}, true, nil))
                time.Sleep(10 * time.Minute)
        }
    

    }

    On the main function, I just need to call this check goroutine and add on the server side (ServeDHCP) this code:

        mac := p.CHAddr().String()
        //Fetch parameters from config file
        config := getConfig()
        if mac == config.MacFake { //udp package received and a mac saved on a json file 01:ff:ff:ff:ff:ff
                // send notification to restart if failure on a systemd
                daemon.SdNotify(false, "WATCHDOG=1")
                return
        }
    

    The last part needed is to add a systemd check, in my case I send the watchdog every 10 min, so, the systemd check will be setted it to 11min

    [Service]
    ExecStartPre=//something 
    WatchdogSec=11min 
    Restart=on-failure
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 求华为P30PRO手机硬盘数据恢复
  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题