-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from SolskGaer/pcapd
commiuncate with pcapd to do packet sniffering, replace rvictl
- Loading branch information
Showing
7 changed files
with
244 additions
and
13 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
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,60 @@ | ||
package giDevice | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/electricbubble/gidevice/pkg/libimobiledevice" | ||
) | ||
|
||
type pcapdClient struct { | ||
stop chan struct{} | ||
c *libimobiledevice.PcapdClient | ||
} | ||
|
||
func newPcapdClient(c *libimobiledevice.PcapdClient) *pcapdClient { | ||
return &pcapdClient{ | ||
stop: make(chan struct{}), | ||
c: c, | ||
} | ||
} | ||
|
||
func (c *pcapdClient) Packet() <-chan []byte { | ||
packetCh := make(chan []byte, 10) | ||
go func() { | ||
for { | ||
select { | ||
case <-c.stop: | ||
return | ||
default: | ||
pkt, err := c.c.ReceivePacket() | ||
if err != nil { | ||
close(packetCh) | ||
return | ||
} | ||
var payload []byte | ||
_ = pkt.Unmarshal(&payload) | ||
raw, err := c.c.GetPacket(payload) | ||
if err != nil { | ||
close(packetCh) | ||
return | ||
} | ||
res, err := c.c.CreatePacket(raw) | ||
if err != nil { | ||
log.Println("failed to create packet") | ||
return | ||
} | ||
packetCh <- res | ||
} | ||
} | ||
}() | ||
return packetCh | ||
} | ||
|
||
func (c *pcapdClient) Stop() { | ||
select { | ||
case <-c.stop: | ||
default: | ||
close(c.stop) | ||
} | ||
c.c.Close() | ||
} |
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,119 @@ | ||
package libimobiledevice | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"fmt" | ||
"io/ioutil" | ||
"time" | ||
|
||
"github.com/lunixbochs/struc" | ||
) | ||
|
||
const PcapdServiceName = "com.apple.pcapd" | ||
|
||
func NewPcapdClient(innerConn InnerConn) *PcapdClient { | ||
return &PcapdClient{ | ||
client: newServicePacketClient(innerConn), | ||
} | ||
} | ||
|
||
type PcapdClient struct { | ||
filter func(*IOSPacketHeader) bool | ||
client *servicePacketClient | ||
} | ||
|
||
func (c *PcapdClient) ReceivePacket() (respPkt Packet, err error) { | ||
var bufLen []byte | ||
if bufLen, err = c.client.innerConn.Read(4); err != nil { | ||
return nil, fmt.Errorf("lockdown(Pcapd) receive: %w", err) | ||
} | ||
lenPkg := binary.BigEndian.Uint32(bufLen) | ||
|
||
buffer := bytes.NewBuffer([]byte{}) | ||
buffer.Write(bufLen) | ||
|
||
var buf []byte | ||
if buf, err = c.client.innerConn.Read(int(lenPkg)); err != nil { | ||
return nil, fmt.Errorf("lockdown(Pcapd) receive: %w", err) | ||
} | ||
buffer.Write(buf) | ||
|
||
if respPkt, err = new(servicePacket).Unpack(buffer); err != nil { | ||
return nil, fmt.Errorf("lockdown(Pcapd) receive: %w", err) | ||
} | ||
|
||
debugLog(fmt.Sprintf("<-- %s\n", respPkt)) | ||
|
||
return | ||
} | ||
|
||
type IOSPacketHeader struct { | ||
HdrSize uint32 `struc:"uint32,big"` | ||
Version uint8 `struc:"uint8,big"` | ||
PacketSize uint32 `struc:"uint32,big"` | ||
Type uint8 `struc:"uint8,big"` | ||
Unit uint16 `struc:"uint16,big"` | ||
IO uint8 `struc:"uint8,big"` | ||
ProtocolFamily uint32 `struc:"uint32,big"` | ||
FramePreLength uint32 `struc:"uint32,big"` | ||
FramePstLength uint32 `struc:"uint32,big"` | ||
IFName string `struc:"[16]byte"` | ||
Pid int32 `struc:"int32,little"` | ||
ProcName string `struc:"[17]byte"` | ||
Unknown uint32 `struc:"uint32,little"` | ||
Pid2 int32 `struc:"int32,little"` | ||
ProcName2 string `struc:"[17]byte"` | ||
Unknown2 [8]byte `struc:"[8]byte"` | ||
} | ||
|
||
func (c *PcapdClient) GetPacket(buf []byte) ([]byte, error) { | ||
iph := IOSPacketHeader{} | ||
preader := bytes.NewReader(buf) | ||
_ = struc.Unpack(preader, &iph) | ||
|
||
if c.filter != nil { | ||
if !c.filter(&iph) { | ||
return nil, nil | ||
} | ||
} | ||
|
||
packet, err := ioutil.ReadAll(preader) | ||
if err != nil { | ||
return packet, err | ||
} | ||
if iph.FramePreLength == 0 { | ||
ext := []byte{0xbe, 0xfe, 0xbe, 0xfe, 0xbe, 0xfe, 0xbe, 0xfe, 0xbe, 0xfe, 0xbe, 0xfe, 0x08, 0x00} | ||
return append(ext, packet...), nil | ||
} | ||
return packet, nil | ||
} | ||
|
||
type PcaprecHdrS struct { | ||
TsSec int `struc:"uint32,little"` /* timestamp seconds */ | ||
TsUsec int `struc:"uint32,little"` /* timestamp microseconds */ | ||
InclLen int `struc:"uint32,little"` /* number of octets of packet saved in file */ | ||
OrigLen int `struc:"uint32,little"` /* actual length of packet */ | ||
} | ||
|
||
func (c *PcapdClient) CreatePacket(packet []byte) ([]byte, error) { | ||
now := time.Now() | ||
phs := &PcaprecHdrS{ | ||
int(now.Unix()), | ||
int(now.UnixNano() / 1e6), | ||
len(packet), | ||
len(packet), | ||
} | ||
var buf bytes.Buffer | ||
err := struc.Pack(&buf, phs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
buf.Write(packet) | ||
return buf.Bytes(), nil | ||
} | ||
|
||
func (c *PcapdClient) Close() { | ||
c.client.innerConn.Close() | ||
} |