mirror of
https://github.com/bettercap/bettercap
synced 2025-08-20 21:43:18 -07:00
yeah i should have done this before, i know
This commit is contained in:
commit
0091ffdbb3
33 changed files with 25678 additions and 0 deletions
40
packets/arp.go
Normal file
40
packets/arp.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package packets
|
||||
|
||||
import (
|
||||
"github.com/google/gopacket/layers"
|
||||
"net"
|
||||
)
|
||||
|
||||
func NewARPTo(from net.IP, from_hw net.HardwareAddr, to net.IP, to_hw net.HardwareAddr, req uint16) (layers.Ethernet, layers.ARP) {
|
||||
eth := layers.Ethernet{
|
||||
SrcMAC: from_hw,
|
||||
DstMAC: net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
||||
EthernetType: layers.EthernetTypeARP,
|
||||
}
|
||||
arp := layers.ARP{
|
||||
AddrType: layers.LinkTypeEthernet,
|
||||
Protocol: layers.EthernetTypeIPv4,
|
||||
HwAddressSize: 6,
|
||||
ProtAddressSize: 4,
|
||||
Operation: req,
|
||||
SourceHwAddress: from_hw,
|
||||
SourceProtAddress: from.To4(),
|
||||
DstHwAddress: to_hw,
|
||||
DstProtAddress: to.To4(),
|
||||
}
|
||||
|
||||
return eth, arp
|
||||
}
|
||||
func NewARP(from net.IP, from_hw net.HardwareAddr, to net.IP, req uint16) (layers.Ethernet, layers.ARP) {
|
||||
return NewARPTo(from, from_hw, to, []byte{0, 0, 0, 0, 0, 0}, req)
|
||||
}
|
||||
|
||||
func NewARPRequest(from net.IP, from_hw net.HardwareAddr, to net.IP) (error, []byte) {
|
||||
eth, arp := NewARP(from, from_hw, to, layers.ARPRequest)
|
||||
return Serialize(ð, &arp)
|
||||
}
|
||||
|
||||
func NewARPReply(from net.IP, from_hw net.HardwareAddr, to net.IP, to_hw net.HardwareAddr) (error, []byte) {
|
||||
eth, arp := NewARPTo(from, from_hw, to, to_hw, layers.ARPReply)
|
||||
return Serialize(ð, &arp)
|
||||
}
|
58
packets/queue.go
Normal file
58
packets/queue.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
package packets
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/google/gopacket/pcap"
|
||||
"github.com/op/go-logging"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var log = logging.MustGetLogger("mitm")
|
||||
|
||||
type Queue struct {
|
||||
iface string
|
||||
handle *pcap.Handle
|
||||
lock *sync.Mutex
|
||||
active bool
|
||||
}
|
||||
|
||||
func NewQueue(iface string) (*Queue, error) {
|
||||
log.Debugf("Creating packet queue for interface %s.\n", iface)
|
||||
var err error
|
||||
|
||||
q := &Queue{
|
||||
iface: iface,
|
||||
handle: nil,
|
||||
lock: &sync.Mutex{},
|
||||
active: true,
|
||||
}
|
||||
|
||||
q.handle, err = pcap.OpenLive(iface, 65536, true, pcap.BlockForever)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (q *Queue) Send(raw []byte) error {
|
||||
q.lock.Lock()
|
||||
defer q.lock.Unlock()
|
||||
|
||||
log.Debugf("Sending %d bytes to packet queue.\n", len(raw))
|
||||
if q.active {
|
||||
return q.handle.WritePacketData(raw)
|
||||
} else {
|
||||
return fmt.Errorf("Packet queue is not active.")
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Queue) Stop() {
|
||||
q.lock.Lock()
|
||||
defer q.lock.Unlock()
|
||||
|
||||
log.Debugf("Stopping packet queue.\n")
|
||||
|
||||
q.handle.Close()
|
||||
q.active = false
|
||||
}
|
20
packets/serialize.go
Normal file
20
packets/serialize.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package packets
|
||||
|
||||
import (
|
||||
"github.com/google/gopacket"
|
||||
)
|
||||
|
||||
func Serialize(layers ...gopacket.SerializableLayer) (error, []byte) {
|
||||
// Set up buffer and options for serialization.
|
||||
buf := gopacket.NewSerializeBuffer()
|
||||
opts := gopacket.SerializeOptions{
|
||||
FixLengths: true,
|
||||
ComputeChecksums: true,
|
||||
}
|
||||
|
||||
if err := gopacket.SerializeLayers(buf, opts, layers...); err != nil {
|
||||
return err, nil
|
||||
}
|
||||
|
||||
return nil, buf.Bytes()
|
||||
}
|
32
packets/udp.go
Normal file
32
packets/udp.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package packets
|
||||
|
||||
import (
|
||||
"github.com/google/gopacket/layers"
|
||||
"net"
|
||||
)
|
||||
|
||||
func NewUDPProbe(from net.IP, from_hw net.HardwareAddr, to net.IP, port int) (error, []byte) {
|
||||
eth := layers.Ethernet{
|
||||
SrcMAC: from_hw,
|
||||
DstMAC: net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
||||
EthernetType: layers.EthernetTypeIPv4,
|
||||
}
|
||||
|
||||
ip4 := layers.IPv4{
|
||||
Protocol: layers.IPProtocolUDP,
|
||||
Version: 4,
|
||||
TTL: 64,
|
||||
SrcIP: from,
|
||||
DstIP: to,
|
||||
}
|
||||
|
||||
udp := layers.UDP{
|
||||
SrcPort: layers.UDPPort(12345),
|
||||
DstPort: layers.UDPPort(port),
|
||||
}
|
||||
udp.Payload = []byte{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef}
|
||||
|
||||
udp.SetNetworkLayerForChecksum(&ip4)
|
||||
|
||||
return Serialize(ð, &ip4, &udp)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue