Skip to content

Instantly share code, notes, and snippets.

@rikatz
Created April 11, 2023 00:47
Show Gist options
  • Save rikatz/1228c74a46d24f44d3419e394f864463 to your computer and use it in GitHub Desktop.
Save rikatz/1228c74a46d24f44d3419e394f864463 to your computer and use it in GitHub Desktop.
DHCP Client with fake Mac Address
package main
import (
"flag"
"log"
"net"
"github.com/davecgh/go-spew/spew"
"github.com/insomniacslk/dhcp/dhcpv4"
client4 "github.com/insomniacslk/dhcp/dhcpv4/client4"
"github.com/insomniacslk/dhcp/netboot"
)
var (
ifname = flag.String("i", "eth0", "Interface name")
macAddr = flag.String("m", "ba:ba:ca:ba:ba:ca", "Fake Mac Address")
)
func main() {
flag.Parse()
var (
err error
bootconf *netboot.BootConf
)
bootconf, err = dhclient4(*ifname, 3, true)
if err != nil {
log.Fatal(err)
}
spew.Dump(bootconf)
}
func dhclient4(ifname string, attempts int, verbose bool) (*netboot.BootConf, error) {
if attempts < 1 {
attempts = 1
}
client := client4.NewClient()
var (
conv []*dhcpv4.DHCPv4
err error
)
newMac, err := net.ParseMAC(*macAddr)
if err != nil {
return nil, err
}
for attempt := 0; attempt < attempts; attempt++ {
log.Printf("Attempt %d of %d", attempt+1, attempts)
conv, err = client.Exchange(ifname, dhcpv4.WithHwAddr(newMac), dhcpv4.WithBroadcast(true))
if err != nil && attempt < attempts {
log.Printf("Error: %v", err)
continue
}
break
}
if verbose {
for _, m := range conv {
log.Print(m.Summary())
}
}
if err != nil {
return nil, err
}
// extract the network configuration
netconf, err := netboot.ConversationToNetconfv4(conv)
return netconf, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment