Skip to content

Commit d673090

Browse files
added ipv4 and udp checksum calclulation, fixed sime arpspoof related functions
1 parent 6b01342 commit d673090

File tree

12 files changed

+300
-81
lines changed

12 files changed

+300
-81
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ sudo setcap cap_net_raw+ep ~/go/bin/mshark
3131

3232
```shell
3333
mshark -h
34-
3534
______ __ __
3635
/ \ | \ | \
3736
______ ____ | $$$$$$\| $$____ ______ ______ | $$ __
@@ -49,22 +48,23 @@ GitHub: https://github.com/shadowy-pycoder/mshark
4948
Usage: mshark [OPTIONS]
5049
Options:
5150
-h Show this help message and exit.
52-
-D Display list of interfaces and exit.
51+
-D Display list of interfaces and exit.
52+
-V Show version and build information
5353
-b int
54-
The maximum size of packet queue. (default 8192)
54+
The maximum size of packet queue. (default 8192)
5555
-c int
56-
The maximum number of packets to capture.
56+
The maximum number of packets to capture.
5757
-e string
58-
BPF filter expression. Example: "ip proto tcp"
58+
BPF filter expression. Example: "ip proto tcp".
5959
-f value
60-
File extension(s) to write captured data. Supported formats: stdout, txt, pcap, pcapng
60+
File extension(s) to write captured data. Supported formats: stdout, txt, pcap, pcapng
6161
-i string
62-
The name of the network interface. Example: eth0 (default "any")
63-
-p Promiscuous mode. This setting is ignored for "any" interface. Defaults to false.
62+
The name of the network interface. Example: eth0 (default "any")
63+
-p Promiscuous mode. This setting is ignored for "any" interface. Defaults to false.
6464
-s int
65-
The maximum length of each packet snapshot. Defaults to 65535.
65+
The maximum length of each packet snapshot. Defaults to 65535.
6666
-t duration
67-
The maximum duration of the packet capture process. Example: 5s
67+
The maximum duration of the packet capture process. Example: 5s
6868
-v Display full packet info when capturing to stdout or txt.
6969
```
7070

arpspoof/arpspoof.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (at *ARPTable) Delete(ip netip.Addr) {
8787
func (at *ARPTable) Refresh() error {
8888
at.Lock()
8989
defer at.Unlock()
90-
cmd := exec.Command("sh", "-c", "ip -br neigh")
90+
cmd := exec.Command("sh", "-c", "ip -4 -br neigh")
9191
out, err := cmd.Output()
9292
if err != nil {
9393
return err
@@ -175,7 +175,7 @@ func NewARPSpoofer(conf *ARPSpoofConfig) (*ARPSpoofer, error) {
175175
arpspoofer.gwIP = gwIP
176176
}
177177
if gwMAC, ok := arpspoofer.arpTable.Get(arpspoofer.gwIP); !ok {
178-
probeIP(arpspoofer.gwIP)
178+
doPing(arpspoofer.gwIP)
179179
time.Sleep(probeThrottling)
180180
err = arpspoofer.arpTable.Refresh()
181181
if err != nil {
@@ -294,8 +294,7 @@ func (ar *ARPSpoofer) Stop() error {
294294
return err
295295
}
296296

297-
func doProbe(ip netip.Addr) error {
298-
// TODO: add manual packet crafting
297+
func doPing(ip netip.Addr) error {
299298
ping := exec.Command("sh", "-c", fmt.Sprintf("ping -c1 -t1 -w1 %s", ip))
300299
if err := ping.Start(); err != nil {
301300
return err
@@ -306,8 +305,14 @@ func doProbe(ip netip.Addr) error {
306305
return nil
307306
}
308307

309-
func probeIP(ip netip.Addr) error {
310-
return doProbe(ip)
308+
func (ar *ARPSpoofer) doProbe(ip netip.Addr) error {
309+
// TODO: add parsing ARP replies
310+
ap, err := ar.newARPRequest(ar.hostMAC, ar.hostIP, ip)
311+
if err != nil {
312+
return err
313+
}
314+
ar.packets <- ap
315+
return nil
311316
}
312317

313318
func (ar *ARPSpoofer) probeTargetsOnce() {
@@ -316,7 +321,7 @@ func (ar *ARPSpoofer) probeTargetsOnce() {
316321
wg.Add(1)
317322
go func(ip netip.Addr) {
318323
defer wg.Done()
319-
doProbe(ip)
324+
doPing(ip)
320325
}(ip)
321326
time.Sleep(probeThrottling)
322327
}
@@ -338,7 +343,7 @@ func (ar *ARPSpoofer) probeTargets() {
338343
wg.Add(1)
339344
go func(ip netip.Addr) {
340345
defer wg.Done()
341-
doProbe(ip)
346+
doPing(ip)
342347
}(ip)
343348
time.Sleep(probeThrottling)
344349
}
@@ -390,6 +395,20 @@ func (ar *ARPSpoofer) newARPReply(srcMAC, dstMAC net.HardwareAddr, srcIP, dstIP
390395
return &Packet{addr: dstMAC, data: eth.ToBytes()}, nil
391396
}
392397

398+
func (ar *ARPSpoofer) newARPRequest(srcMAC net.HardwareAddr, srcIP, dstIP netip.Addr) (*Packet, error) {
399+
arp, err := layers.NewARPPacket(layers.OperationRequest, srcMAC, srcIP, network.LoopbackMAC, dstIP)
400+
if err != nil {
401+
ar.logger.Debug().Msg(err.Error())
402+
return nil, err
403+
}
404+
eth, err := layers.NewEthernetFrame(network.BroadcastMAC, srcMAC, layers.EtherTypeARP, arp.ToBytes())
405+
if err != nil {
406+
ar.logger.Debug().Msg(err.Error())
407+
return nil, err
408+
}
409+
return &Packet{addr: network.BroadcastMAC, data: eth.ToBytes()}, nil
410+
}
411+
393412
func (ar *ARPSpoofer) spoofTargets() {
394413
for _, targetIP := range ar.targets {
395414
if targetMAC, ok := ar.arpTable.Get(targetIP); !ok {

cmd/mshark/cli.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"runtime"
89
"slices"
910
"strings"
1011
"time"
@@ -17,8 +18,7 @@ import (
1718

1819
const app string = "mshark"
1920

20-
const usagePrefix string = `
21-
______ __ __
21+
const usagePrefix string = ` ______ __ __
2222
/ \ | \ | \
2323
______ ____ | $$$$$$\| $$____ ______ ______ | $$ __
2424
| \ \ | $$___\$$| $$ \ | \ / \ | $$ / \
@@ -108,6 +108,11 @@ func root(args []string) error {
108108
fmt.Print(usagePrefix)
109109
flags.PrintDefaults()
110110
}
111+
flags.BoolFunc("V", "Show version and build information", func(flagValue string) error {
112+
fmt.Printf("%s (built for %s %s with %s)\n", ms.Version, runtime.GOOS, runtime.GOARCH, runtime.Version())
113+
os.Exit(0)
114+
return nil
115+
})
111116

112117
if err := flags.Parse(args); err != nil {
113118
return err

0 commit comments

Comments
 (0)