forked from danielpaulus/go-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try get ip from pcap on the fly (danielpaulus#110)
an experimental feature to use pcap to grab the local network IPv4 and IPv6 of a device: using the WiFi MAC address waiting for the mac to appear as source combined with a ipv4 layer dump ipv4, ipv6 and mac source address Example: {"Mac":"b4:85:e1:7a:61:1c","IPv4":"192.168.2.106","IPv6":"2003:c2:6f05:cc01:f575:9abd:9c1e:7927"} This waits for network traffic, so it can take a while if nothing happens. To speed it up start apple maps f.ex. like: ios launch com.apple.Maps NOTE: Automatic Wifi Must be disabled! Disabling the private or random MAC address feature on your iOS 14 device Go to the Settings app on your iOS device. Tap on Wi-Fi. Tap the information button. ... Tap the toggle switch next to Private Address to disable it. Turn OFF your device's Wi-Fi and then ON again. Addressed danielpaulus#106 Co-authored-by: Daniel Paulus <[email protected]>
- Loading branch information
1 parent
84b1332
commit ccaee45
Showing
6 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package pcap | ||
|
||
import ( | ||
"github.com/danielpaulus/go-ios/ios" | ||
"github.com/google/gopacket" | ||
"github.com/google/gopacket/layers" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type NetworkInfo struct { | ||
Mac string | ||
IPv4 string | ||
IPv6 string | ||
} | ||
|
||
func (n NetworkInfo) complete() bool { | ||
return n.IPv6 != "" && n.Mac != "" && n.IPv4 != "" | ||
} | ||
|
||
//FindIp reads pcap packets until one is found that matches the given MAC | ||
//and contains an IP address. This won't work if the iOS device "automatic Wifi address" privacy | ||
//feature is enabled. The MAC needs to be static. | ||
func FindIp(device ios.DeviceEntry) (NetworkInfo, error) { | ||
mac, err := ios.GetWifiMac(device) | ||
if err != nil { | ||
return NetworkInfo{}, err | ||
} | ||
return findIp(device, mac) | ||
|
||
} | ||
|
||
|
||
func findIp(device ios.DeviceEntry, mac string) (NetworkInfo, error) { | ||
intf, err := ios.ConnectToService(device, "com.apple.pcapd") | ||
if err != nil { | ||
return NetworkInfo{}, err | ||
} | ||
plistCodec := ios.NewPlistCodec() | ||
info := NetworkInfo{} | ||
info.Mac = mac | ||
for { | ||
b, err := plistCodec.Decode(intf.Reader()) | ||
if err != nil { | ||
return NetworkInfo{}, err | ||
} | ||
decodedBytes, err := fromBytes(b) | ||
if err != nil { | ||
return NetworkInfo{}, err | ||
} | ||
packet, err := getPacket(decodedBytes) | ||
if err != nil { | ||
return NetworkInfo{}, err | ||
} | ||
if len(packet) > 0 { | ||
err:=findIP(packet, &info) | ||
if err != nil { | ||
return NetworkInfo{}, err | ||
} | ||
if info.complete() { | ||
return info, nil | ||
} | ||
} | ||
} | ||
} | ||
|
||
func findIP(p []byte, info *NetworkInfo) error { | ||
packet := gopacket.NewPacket(p, layers.LayerTypeEthernet, gopacket.Default) | ||
// Get the TCP layer from this packet | ||
if tcpLayer := packet.Layer(layers.LayerTypeEthernet); tcpLayer != nil { | ||
tcp, _ := tcpLayer.(*layers.Ethernet) | ||
if tcp.SrcMAC.String() == info.Mac { | ||
if log.IsLevelEnabled(log.DebugLevel) { | ||
log.Debugf("found packet for %s", info.Mac) | ||
for _, layer := range packet.Layers() { | ||
log.Debugf("layer:%s", layer.LayerType().String()) | ||
} | ||
} | ||
if ipv4Layer := packet.Layer(layers.LayerTypeIPv4); ipv4Layer != nil { | ||
ipv4, ok := ipv4Layer.(*layers.IPv4) | ||
if ok { | ||
info.IPv4 = ipv4.SrcIP.String() | ||
log.Debugf("ip4 found:%s", info.IPv4) | ||
} | ||
} | ||
if ipv6Layer := packet.Layer(layers.LayerTypeIPv6); ipv6Layer != nil { | ||
ipv6, ok := ipv6Layer.(*layers.IPv6) | ||
if ok { | ||
info.IPv6 = ipv6.SrcIP.String() | ||
log.Debugf("ip6 found:%s", info.IPv6) | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters