Skip to content

Add Spawn Gating #25

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
⚡ Using device iPad (8802dab560c608734ce59b2305ae5f857e89109e)
⚡ Enable spawn gating
70 changes: 60 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/frida/frida-go/frida"
"github.com/spf13/cobra"
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"strings"
"sync"
"syscall"

"github.com/frida/frida-go/frida"
"github.com/spf13/cobra"
)

//go:embed script.js
Expand Down Expand Up @@ -71,6 +73,11 @@ var rootCmd = &cobra.Command{
return err
}

spawnGate, err := cmd.Flags().GetString("spawn")
if err != nil {
return err
}

if output != "" {
if err := logger.SetOutput(output); err != nil {
return err
Expand Down Expand Up @@ -114,12 +121,33 @@ var rootCmd = &cobra.Command{
}
}

/*
Because the signal handler is broken, we deactivate spawn gating when not used
*/

if dev == nil {
return errors.New("could not obtain specified device")
}
defer dev.Clean()
logger.Infof("Using device %s (%s)", dev.Name(), dev.ID())

file, err := cmd.Flags().GetString("file")
if err != nil {
return err
}

if spawnGate != "" {
if err := dev.EnableSpawnGating(); err != nil {
return err
}
logger.Infof("Enable spawn gating")
} else {
if err := dev.DisableSpawnGating(); err != nil {
return err
}
logger.Infof("Disable spawn gating")
}

procPid := pid

if pid == -1 && procName != "" {
Expand All @@ -136,12 +164,7 @@ var rootCmd = &cobra.Command{
}
}

file, err := cmd.Flags().GetString("file")
if err != nil {
return err
}

if procPid == -1 && file == "" {
if procPid == -1 && file == "" && spawnGate == "" {
return errors.New("missing pid, name or file to spawn")
}

Expand All @@ -152,6 +175,32 @@ var rootCmd = &cobra.Command{
if err != nil {
return err
}
} else if spawnGate != "" {
lock := sync.Mutex{}
lock.Lock()

dev.On("spawn_added", func(spawn *frida.Spawn) {
logger.Infof("%d", spawn.PID())
if spawn.Identifier() == spawnGate {
procPid = spawn.PID()
lock.Unlock()
} else {
logger.Infof("Ignore Spawn(pid=%d, identifier=%s)", spawn.PID(), spawn.Identifier())
}
spawn.Clean()
})
lock.Lock()

session, err = dev.Attach(procPid, nil)

if err != nil {
return err
}

if err := dev.DisableSpawnGating(); err != nil {
return err
}

} else {
opts := frida.NewSpawnOptions()
argv := make([]string, len(args)+1)
Expand All @@ -175,7 +224,7 @@ var rootCmd = &cobra.Command{

logger.Infof("Attached to the process with PID => %d", procPid)

detached := make(chan struct{})
detached := make(chan struct{}, 1)

session.On("detached", func(reason frida.SessionDetachReason, crash *frida.Crash) {
logger.Errorf("Session detached: %s: %v", reason.String(), crash)
Expand Down Expand Up @@ -301,7 +350,7 @@ var rootCmd = &cobra.Command{
_ = script.ExportsCall("setup", offsets)
}

c := make(chan os.Signal)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)

select {
Expand Down Expand Up @@ -366,6 +415,7 @@ func setupFlags() {
rootCmd.Flags().StringP("name", "n", "", "process name")
rootCmd.Flags().StringP("file", "f", "", "spawn the file")
rootCmd.Flags().StringP("output", "o", "", "save output to this file")
rootCmd.Flags().StringP("spawn", "W", "", "spawn gate")

rootCmd.Flags().StringP("config", "c", "", "path to gxpc.conf file; default user home directory")

Expand Down