Use raw sockets in Go

試著忘記壹切 提交于 2019-11-30 10:01:27

There are a number of hurdles to jump with what you propose:

Security

In general, being able to set the source IP address for a packet could be a very dangerous thing security wise. Under linux, in order to forge your own raw DHCP packets with custom headers, you will need to run your application as root or from an application with the CAP_NET_RAW capability (see setcap).

Raw Sockets in Go

The standard net library does not provide raw socket capability because it is very specialized and the API may be subject to change as people begin to use it in anger.

The go.net subrepository provides an ipv4 and an ipv6 package, the former of which should suit your needs:

http://godoc.org/code.google.com/p/go.net/ipv4#NewRawConn

Header Spoofing

You will need to use ipv4.RawConn's ReadFrom method to read your source packet. You should then be able to use most of those fields, along with your GIADDR logic, to set up the headers for the WriteTo call. It will probably look something like:

for {
  hdr, payload, _, err := conn.ReadFrom(buf)
  if err != nil { ... }
  hdr.ID = 0
  hdr.Checksum = 0
  hdr.Src = ...
  hdr.Dst = ...
  if err := conn.WriteTo(hdr, payload, nil); err != nil { ... }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!