Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: DTX utun sniffing #317

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ios/debugproxy/utun/decoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"

dtx "github.com/danielpaulus/go-ios/ios/dtx_codec"
Expand Down Expand Up @@ -114,6 +115,7 @@ func decodeRemoteXpc(w io.Writer, r io.Reader) error {
log.Info("file transfer started, skipping remaining data ")
return nil
}
fmt.Fprintf(w, "\n")
}
}

Expand All @@ -127,9 +129,11 @@ func decodeRemoteDtx(w io.Writer, r io.Reader) error {
return err
}

buf := bytes.NewBufferString(m.StringDebug() + "\n")
buf := bytes.NewBufferString(m.StringDebug())
if _, err := io.Copy(w, buf); err != nil {
return err
}

fmt.Fprintf(w, "\n\n")
}
}
7 changes: 4 additions & 3 deletions ios/debugproxy/utun/sniff.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package utun
import (
"context"
"fmt"
"io"
"net"

"github.com/danielpaulus/go-ios/ios"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
log "github.com/sirupsen/logrus"
"io"
"net"
)

type direction uint8
Expand All @@ -32,7 +33,7 @@ func Live(ctx context.Context, iface string, provider ios.RsdPortProvider, dumpD
return err
}
log.Infof("Capture traffice for iface %s with address %s", iface, addr)
if handle, err := pcap.OpenLive(iface, 1600, true, pcap.BlockForever); err != nil {
if handle, err := pcap.OpenLive(iface, 65536, true, pcap.BlockForever); err != nil {
return fmt.Errorf("failed to connect to iface %s. %w", iface, err)
} else {
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
Expand Down
150 changes: 91 additions & 59 deletions ios/dtx_codec/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dtx
import (
"bytes"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"io"

Expand All @@ -23,73 +25,98 @@ func ReadMessage(reader io.Reader) (Message, error) {
}
result := readHeader(header)

if result.IsFragment() {
messageLength := result.MessageLength
// if messageLength == 468 {
// messageLength -= 72
// }

// the first part of a fragmented message is only a header indicating the total length of
// the defragmented message
if result.IsFirstFragment() {
// put in the header as bytes here
result.fragmentBytes = header
return result, nil
}
// 32 offset is correct, the binary starts with a payload header
messageBytes := make([]byte, result.MessageLength)
_, err := io.ReadFull(reader, messageBytes)
if err != nil {
return Message{}, err
}
result.fragmentBytes = messageBytes
return result, nil
}

payloadHeaderBytes := make([]byte, 16)
_, err = io.ReadFull(reader, payloadHeaderBytes)
remainingBytes := make([]byte, messageLength)
_, err = io.ReadFull(reader, remainingBytes)
if err != nil {
d, _ := json.Marshal(result)
log.Printf("%s", string(d))

return Message{}, err
}

ph, err := parsePayloadHeader(payloadHeaderBytes)
m, _, err := DecodeNonBlocking(append(header, remainingBytes[:]...))
if err != nil {
return Message{}, err
}
result.PayloadHeader = ph

if result.HasAuxiliary() {
auxHeaderBytes := make([]byte, 16)
_, err = io.ReadFull(reader, auxHeaderBytes)
if err != nil {
return Message{}, err
}

header, err := parseAuxiliaryHeader(auxHeaderBytes)
if err != nil {
return Message{}, err
}
result.AuxiliaryHeader = header
auxBytes := make([]byte, result.AuxiliaryHeader.AuxiliarySize)
_, err = io.ReadFull(reader, auxBytes)
if err != nil {
return Message{}, err
}
result.Auxiliary = DecodeAuxiliary(auxBytes)
}

result.RawBytes = make([]byte, 0)
if result.HasPayload() {
payloadBytes := make([]byte, result.PayloadLength())
_, err := io.ReadFull(reader, payloadBytes)
if err != nil {
return Message{}, err
}

payload, err := nskeyedarchiver.Unarchive(payloadBytes)
if err != nil {
return Message{}, err
}
result.Payload = payload
}

return result, nil
return m, nil

// if result.IsFragment() {

// // the first part of a fragmented message is only a header indicating the total length of
// // the defragmented message
// if result.IsFirstFragment() {
// // put in the header as bytes here
// result.fragmentBytes = header
// return result, nil
// }
// // 32 offset is correct, the binary starts with a payload header
// messageBytes := make([]byte, result.MessageLength)
// _, err := io.ReadFull(reader, messageBytes)
// if err != nil {
// return Message{}, err
// }
// result.fragmentBytes = messageBytes
// return result, nil
// }

// payloadHeaderBytes := make([]byte, 16)
// _, err = io.ReadFull(reader, payloadHeaderBytes)
// if err != nil {
// return Message{}, err
// }

// ph, err := parsePayloadHeader(payloadHeaderBytes)
// if err != nil {
// return Message{}, err
// }
// result.PayloadHeader = ph

// if result.HasAuxiliary() {
// auxHeaderBytes := make([]byte, 16)
// _, err = io.ReadFull(reader, auxHeaderBytes)
// if err != nil {
// return Message{}, err
// }

// header, err := parseAuxiliaryHeader(auxHeaderBytes)
// if err != nil {
// return Message{}, err
// }
// result.AuxiliaryHeader = header
// auxBytes := make([]byte, result.AuxiliaryHeader.AuxiliarySize)
// _, err = io.ReadFull(reader, auxBytes)
// if err != nil {
// return Message{}, err
// }
// result.Auxiliary = DecodeAuxiliary(auxBytes)
// }

// result.RawBytes = make([]byte, 0)
// if result.HasPayload() {
// payloadLength := result.PayloadLength()
// if payloadLength == 161 {
// payloadLength = 300
// }
// payloadBytes := make([]byte, payloadLength)
// _, err := io.ReadFull(reader, payloadBytes)
// if err != nil {
// return Message{}, err
// }

// log.Printf("\nBYTES:::::::::::::::::::::: %s", string(payloadBytes[:]))
// payload, err := nskeyedarchiver.Unarchive(payloadBytes)
// if err != nil {
// return Message{}, err
// }
// result.Payload = payload
// }

// return result, nil
}

// DecodeNonBlocking should only be used for the debug proxy to on the fly decode DtxMessages.
Expand Down Expand Up @@ -158,9 +185,14 @@ func DecodeNonBlocking(messageBytes []byte) (Message, []byte, error) {
}
result.RawBytes = messageBytes[:totalMessageLength]

// d, _ := json.Marshal(result)
// log.Printf("%s", string(d))

if result.HasPayload() {
// log.Printf("BYTESSSSSSSSS: %s", result.RawBytes)
payload, err := result.parsePayloadBytes(result.RawBytes)
if err != nil {
log.Printf("RAW BYTES: %s", hex.EncodeToString(result.RawBytes))
return Message{}, make([]byte, 0), err
}
result.Payload = payload
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/danielpaulus/go-ios/ios/debugproxy/usbmuxd"
"github.com/danielpaulus/go-ios/ios/debugproxy/utun"
"io/ioutil"
"os"
"os/signal"
Expand All @@ -19,6 +17,9 @@ import (
"syscall"
"time"

"github.com/danielpaulus/go-ios/ios/debugproxy/usbmuxd"
"github.com/danielpaulus/go-ios/ios/debugproxy/utun"

"github.com/danielpaulus/go-ios/ios/mobileactivation"

"github.com/danielpaulus/go-ios/ios/afc"
Expand Down
Loading