Code Monkey home page Code Monkey logo

ocaml-usbmux's Introduction

ocaml-usbmux + gandalf

This is a library and command line tool to control port forwarding to jailbroken iOS devices.

Basically, it lets you do:

$ ssh root@localhost -p 2000

and you get a shell to an iDevice connected over a USB wire.

The command line tool is called gandalf and it requires that usbmuxd be running. If on OS X then you don't have to do anything, if on Linux then you need to have the open source version of usbmuxd. I recommend compiling from source, versions on most package managers are old.

Installation

You need to have opam installed, it is OCaml's package manager.

On OS X you can do:

$ brew install opam

(If on Linux, then get opam via your package manager, aka apt-get or whatever). NOTE if you are on Ubuntu, then you need to do:

$ add-apt-repository ppa:avsm/ppa
$ apt-get update
$ apt-get install ocaml opam

It is important that your compiler is up to date, you can check with opam switch, make sure its at least >= 4.02.0. Currently gandalf works with versions between ["4.02.0", "4.03.0"). Once ppx_core updates then I can update as well to let you use "4.03.0".

then

$ opam install usbmux

This will install the command line tool gandalf and an OCaml library.

gandalf usage.

The following are a series of usages of gandalf, all short form arguments have long-forms as well and -v can be added at any time.

  1. See with realtime updates what devices are connected

    $ gandalf

    This will start up gandalf in listen mode, that is it will print out whenever a device connects or disconnects.

  2. Start with a mapping file such that # start comments and consists of an array of json objects with these fields, note that name can be null and is just a nickname for this tunnel, other fields are required.

    # This is a comment
    [{"udid":"9cdfac9f74c5e18a6eff3611c0927df5cf4f2eca",
      "name":"i11", "forwarding": [{"local_port":2000, "device_port":22},
                                   {"local_port":3000, "device_port":1122}]
    							   }]
    
    $ gandalf -m mapping

2.1) You can also daemonize gandalf with the -d flag. NOTE: You might need to end up doing that under sudo as gandalf needs to make a pid file under /var/run.

  1. To see a pretty JSON representation of devices and their ports that are currently connected, do:

    $ gandalf -s
  2. To reload the gandalf with a new set of mappings, do:

    $ gandalf -r

    This will cancel all running threads and reload from the original mappings file, so make your changes there.

  3. To cleanly exit gandalf, do: NOTE This might require super user permissions.

    $ gandalf -e

Check out the man page, accessible with:

$ gandalf --help

or

$ man gandalf

Simple invocation:

$ sudo `which gandalf` --mappings etc/mapping --daemonize --verbose

Important Notes and Catches

  1. If you are running this on Linux, then you might get issues with usbmuxd having issues when more than around 7 devices are plugged in. This is because multiple threads are trying to call various libxml2 freeing functions. I have a forked version of libplist that usbmuxd uses, sans the memory freeing calls. Its available here. Compile and install that, then compile and install usbmuxd from source. This will leak memory but its not that much at all and I believe it to be a fixed amount.

  2. Another issue you might have is USB3.0. The Linux kernel might crap out on you after 13 devices. This is a combination of the kernel not giving enough resources and the host controller on your motherboard being crappy. The solution to this problem is to disable USB3.0 in your BIOS. To verify that USB3.0 isn't working check with lsusb

For reference, this project is currently in use given these two issues and works fine with > 20 iPhones.

Acknowledgements

  1. Much appreciation to MixRank for green lighting this project
  2. Ivg, Edwin and others on #OCaml
  3. Starting point ideas from irelay, an npm package.

Future plans

Future plans include writing C bindings to libusbmuxd itself, thereby eliminating the dependency on usbmuxd.

ocaml-usbmux's People

Contributors

chris00 avatar fxfactorial avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ocaml-usbmux's Issues

Disconnect and re-connect device

When I unplug a device and plug it back in the tunnel does not open and I have to restart gandalf. Is this the normal behavior?

Wrong expression type

Hi,

While compiling on OSX I get the following:

File "src/lib/usbmux.ml", line 260, characters 9-336:
Error: This expression has type
(Yojson.Safe.json Lazy.t * ([< Error of string |Ok of 'b ] as 'a))
list -> 'b list
but an expression was expected of type
(Yojson.Safe.json Lazy.t *
tunnel Ppx_deriving_yojson_runtime.error_or)
list -> 'c
Type 'a is not compatible with type
tunnel Ppx_deriving_yojson_runtime.error_or =
(tunnel, string) Result.result`

getting broken pipe error and exiting

I'm running gandalf -m {mappingFileLocation} --log_async_exceptions --log_connections --log_everything_else --log_plugged_action and this is what I get:

gandalf: plugged_inout: Device 45 with serial number: <id> connected
gandalf: connections: Tunneling. Udid: <id> Local Port: 2000 Device Port: 22 Device_id: 45
gandalf: connections: Tunneling. Udid: <id> Local Port: 2000 Device Port: 22 Device_id: 45
gandalf: connections: Tunneling. Udid: <id> Local Port: 2003 Device Port: 5900 Device_id: 45
gandalf: everything_else: Unix based error: Broken pipe
Exited with 1 still running; this is a bug.

I know there's a workaround: ignoring unix errors

Installation error on opam install usbmux

This is on High Sierra

=-=- Processing actions -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=  ๐Ÿซ 
[ERROR] The compilation of usbmux failed at "ocaml setup.ml -build".
Processing  1/1: [usbmux: ocaml]
#=== ERROR while installing usbmux.1.3.2 ======================================#
# opam-version 1.2.2
# os           darwin
# command      ocaml setup.ml -build
# path         /Users/macbook/.opam/system/build/usbmux.1.3.2
# compiler     system (4.06.0)
# exit-code    1
# env-file     /Users/macbook/.opam/system/build/usbmux.1.3.2/usbmux-52213-c61e7b.env
# stdout-file  /Users/macbook/.opam/system/build/usbmux.1.3.2/usbmux-52213-c61e7b.out
# stderr-file  /Users/macbook/.opam/system/build/usbmux.1.3.2/usbmux-52213-c61e7b.err
### stdout ###
# [...]
# ocamlfind ocamlopt unix.cmxa -I /Users/macbook/.opam/system/lib/ocamlbuild /Users/macbook/.opam/system/lib/ocamlbuild/ocamlbuildlib.cmxa -linkpkg myocamlbuild.ml /Users/macbook/.opam/system/lib/ocamlbuild/ocamlbuild.cmx -o myocamlbuild
# /Users/macbook/.opam/system/bin/ocamlfind ocamldep -package yojson -package threads -package ppx_deriving_yojson -package ppx_deriving -package plist -package lwt.unix -package lwt.preemptive -package lwt.ppx -package cohttp.lwt -package cohttp -modules src/lib/usbmux.mli > src/lib/usbmux.mli.depends
# /Users/macbook/.opam/system/bin/ocamlfind ocamlc -c -g -annot -bin-annot -g -w +a-4-40..42-44-45-48 -thread -package yojson -package threads -package ppx_deriving_yojson -package ppx_deriving -package plist -package lwt.unix -package lwt.preemptive -package lwt.ppx -package cohttp.lwt -package cohttp -I src/lib -o src/lib/usbmux.cmi src/lib/usbmux.mli
# /Users/macbook/.opam/system/bin/ocamlfind ocamldep -package yojson -package threads -package ppx_deriving_yojson -package ppx_deriving -package plist -package lwt.unix -package lwt.preemptive -package lwt.ppx -package cohttp.lwt -package cohttp -modules src/lib/usbmux.ml > src/lib/usbmux.ml.depends
# /Users/macbook/.opam/system/bin/ocamlfind ocamlc -c -g -annot -bin-annot -g -w +a-4-40..42-44-45-48 -thread -package yojson -package threads -package ppx_deriving_yojson -package ppx_deriving -package plist -package lwt.unix -package lwt.preemptive -package lwt.ppx -package cohttp.lwt -package cohttp -I src/lib -o src/lib/usbmux.cmo src/lib/usbmux.ml
# + /Users/macbook/.opam/system/bin/ocamlfind ocamlc -c -g -annot -bin-annot -g -w +a-4-40..42-44-45-48 -thread -package yojson -package threads -package ppx_deriving_yojson -package ppx_deriving -package plist -package lwt.unix -package lwt.preemptive -package lwt.ppx -package cohttp.lwt -package cohttp -I src/lib -o src/lib/usbmux.cmo src/lib/usbmux.ml
# File "src/lib/usbmux.ml", line 158, characters 35-41:
# Error: This expression has type bytes but an expression was expected of type
#          string
# Command exited with code 2.
### stderr ###
# E: Failure("Command ''/Users/macbook/.opam/system/bin/ocamlbuild' src/lib/usbmux.cma src/lib/usbmux.cmxa src/lib/usbmux.a src/lib/usbmux.cmxs src/app/main.native -use-ocamlfind -tag debug' terminated with error code 10")

gandalf: option `--mappings': no `etc/mapping' file

After I install gandalf, I can't find mapping file under ~/etc/, but I create a new mapping file under ~/etc,which system still tell me no mapping file.

Where I should create the mapping file? Or gandalf install failed?

Can't build ocaml-usbmux from source

Hello!
What I doing wrong?

~/gandalf_src/ocaml-usbmux # make
ocaml setup.ml -build
W: File src/lib/META has changed, doing a backup in src/lib/META.bak
Finished, 1 target (0 cached) in 00:00:00.
+ /root/.opam/system/bin/ocamlfind ocamlc -c -g -annot -bin-annot -g -w +a-4-40..42-44-45-48 -thread -package yojson -package threads -package ppx_deriving_yojson -package plist -package lwt.unix -package lwt.preemptive -package lwt.ppx -package cohttp.lwt -I src/lib -o src/lib/usbmux.cmo src/lib/usbmux.ml
File "src/lib/usbmux.ml", line 260, characters 9-336:
Error: This expression has type
         (Yojson.Safe.json Lazy.t * ([< `Error of string | `Ok of 'b ] as 'a))
         list -> 'b list
       but an expression was expected of type
         (Yojson.Safe.json Lazy.t *
          tunnel Ppx_deriving_yojson_runtime.error_or)
         list -> 'c
       Type 'a is not compatible with type
         tunnel Ppx_deriving_yojson_runtime.error_or =
           (tunnel, string) Result.result
Command exited with code 2.
Compilation unsuccessful after building 4 targets (0 cached) in 00:00:00.
E: Failure("Command ''/usr/bin/ocamlbuild' src/lib/usbmux.cma src/lib/usbmux.cmxa src/lib/usbmux.a src/lib/usbmux.cmxs src/app/main.native -use-ocamlfind -tag debug' terminated with error code 10")
Makefile:7: recipe for target 'build' failed
make: *** [build] Error 1

Brew install usbmux

The following dependencies couldn't be met:

  • usbmux -> ppx_deriving_yojson >= 3.0
    Your request can't be satisfied:
  • ppx_deriving_yojson>=3.0 is not available because your system doesn't comply with ocaml-version < "4.05.0".

No solution found, exiting

Any ideas ?

gandalf not found problem

After following the command, gandalf is not found.
It seems that the opam does not insert .opam/system/bin/ into PATH environment variable. Maybe add this to the readme?

I'm getting connection refused when I try to connect to gandalf with multiple mapping for a device

this is my configuration.

[                                                      
  {                                                    
    "udid": "device_id",
    "name": "iPad jail 10.2 (32GB)",                   
    "forwarding": [                                    
      {                                                
        "device_port": 22,                             
        "local_port": 2000                             
      },                                               
      {                                                
        "device_port": 5900,                           
        "local_port": 2001                             
      }                                                
    ]                                                  
  }                                                    
]

when I try telnet 127.0.0.1 2001 I'm getting

Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host

but without the first mapping everything's fine.

Opam fails to install base64(and usbmux)

Here's what's printed out.

Canors-Mac-mini:~ Canor$ opam install usbmux
The following actions will be performed:
  โˆ—  install ocamlmod        0.0.8            [required by oasis]
  โˆ—  install biniou          1.0.12           [required by yojson]
  โˆ—  install stringext       1.4.2            [required by usbmux]
  โˆ—  install lwt             2.5.2            [required by usbmux]
  โˆ—  install base64          2.0.0            [required by cohttp]
  โˆ—  install magic-mime      1.0.0            [required by cohttp]
  โˆ—  install ppx_deriving    3.3              [required by ppx_type_conv]
  โˆ—  install ocplib-endian   1.0              [required by cstruct]
  โˆ—  install re              1.6.1            [required by cohttp]
  โˆ—  install ppx_core        113.33.01+4.03   [required by ppx_fields_conv]
  โˆ—  install sexplib         113.33.00+4.03   [required by cohttp]
  โˆ—  install fieldslib       113.24.00        [required by cohttp]
  โˆ—  install ocamlify        0.0.1            [required by oasis]
  โˆ—  install yojson          1.3.2            [required by usbmux]
  โˆ—  install markup          0.7.2            [required by lambdasoup]
  โˆ—  install ppx_optcomp     113.33.00+4.03   [required by conduit]
  โˆ—  install cstruct         2.3.0            [required by conduit]
  โˆ—  install oasis           0.4.7            [required by usbmux]
  โˆ—  install lambdasoup      0.6              [required by plist]
  โˆ—  install ppx_driver      113.33.01+4.03   [required by conduit]
  โˆ—  install plist           0.1              [required by usbmux]
  โˆ—  install ppx_type_conv   113.33.01+4.03   [required by ppx_fields_conv]
  โˆ—  install ppx_sexp_conv   113.33.01+4.03   [required by cohttp]
  โˆ—  install ppx_fields_conv 113.33.00+4.03   [required by cohttp]
  โˆ—  install uri             1.9.2            [required by cohttp]
  โˆ—  install ipaddr          2.7.0            [required by conduit]
  โˆ—  install conduit         0.12.0           [required by cohttp]
  โˆ—  install cohttp          0.21.0           [required by usbmux]
  โˆ—  install usbmux          1.1.1         
===== โˆ—  29 =====
Do you want to continue ? [Y/n] y

=-=- Gathering sources =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[base64] Archive in cache
[biniou] Archive in cache
[cohttp] Archive in cache
[conduit] Archive in cache
[cstruct] Archive in cache
[fieldslib] Archive in cache
[ipaddr] Archive in cache
[lambdasoup] Archive in cache
[lwt] Archive in cache
[magic-mime] Archive in cache
[markup] Archive in cache
[oasis] Archive in cache
[ocamlify] Archive in cache
[ocamlmod] Archive in cache
[ocplib-endian] Archive in cache
[plist] Archive in cache
[ppx_core] Archive in cache
[ppx_deriving] Archive in cache
[ppx_driver] Archive in cache
[ppx_fields_conv] Archive in cache
[ppx_optcomp] Archive in cache
[ppx_sexp_conv] Archive in cache
[ppx_type_conv] Archive in cache
[re] Archive in cache
[sexplib] Archive in cache
[stringext] Archive in cache
[uri] Archive in cache
[usbmux] Archive in cache
[yojson] Archive in cache

=-=- Processing actions -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#=== ERROR while installing base64.2.0.0 ======================================#
# opam-version    1.2.2
# os              darwin
Extracting the archive will overwrite /Users/Canor 1/.opam/system/build/base64.2.0.0.


=-=- Error report -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
The following actions were aborted
  โˆ—  install biniou          1.0.12        
  โˆ—  install cohttp          0.21.0        
  โˆ—  install conduit         0.12.0        
  โˆ—  install cstruct         2.3.0         
  โˆ—  install fieldslib       113.24.00     
  โˆ—  install ipaddr          2.7.0         
  โˆ—  install lambdasoup      0.6           
  โˆ—  install lwt             2.5.2         
  โˆ—  install magic-mime      1.0.0         
  โˆ—  install markup          0.7.2         
  โˆ—  install oasis           0.4.7         
  โˆ—  install ocamlify        0.0.1         
  โˆ—  install ocamlmod        0.0.8         
  โˆ—  install ocplib-endian   1.0           
  โˆ—  install plist           0.1           
  โˆ—  install ppx_core        113.33.01+4.03
  โˆ—  install ppx_deriving    3.3           
  โˆ—  install ppx_driver      113.33.01+4.03
  โˆ—  install ppx_fields_conv 113.33.00+4.03
  โˆ—  install ppx_optcomp     113.33.00+4.03
  โˆ—  install ppx_sexp_conv   113.33.01+4.03
  โˆ—  install ppx_type_conv   113.33.01+4.03
  โˆ—  install re              1.6.1         
  โˆ—  install sexplib         113.33.00+4.03
  โˆ—  install stringext       1.4.2         
  โˆ—  install uri             1.9.2         
  โˆ—  install usbmux          1.1.1         
  โˆ—  install yojson          1.3.2         
The following actions failed
  โˆ—  install base64 2.0.0
No changes have been performed

it's OSX 10.11.6, opam compiler 4.03.0 installed via homebrew, however Terminal prints

The environment is not in sync with the current switch.
          You should run: eval `opam config env`

when I type opam switch.

Lwt lazy value exploding and causing early exit

Mar 23 22:12:38 conquest gandalf[21356]: main: Exited with 19 still running; this is a bug.
Mar 23 22:12:38 conquest gandalf[21356]: async_exceptions: Called from file "src/core/lwt.ml", line 781, characters 66-71
Mar 23 22:12:38 conquest gandalf[21356]: async_exceptions: Called from file "src/core/lwt.ml", line 1150, characters 14-18
Mar 23 22:12:38 conquest gandalf[21356]: async_exceptions: Called from file "src/unix/lwt_io.ml", line 1387, characters 28-36
Mar 23 22:12:38 conquest gandalf[21356]: async_exceptions: Re-raised at file "camlinternalLazy.ml", line 32, characters 10-11
Mar 23 22:12:38 conquest gandalf[21356]: async_exceptions: Called from file "camlinternalLazy.ml", line 25, characters 17-27
Mar 23 22:12:38 conquest gandalf[21356]: async_exceptions: Raised at file "camlinternalLazy.ml", line 18, characters 48-57
Mar 23 22:12:38 conquest gandalf[21356]: async_exceptions: backtrace:
Mar 23 22:12:38 conquest gandalf[21356]: async_exceptions: Please report, this is an unhandled async exception (A bug): CamlinternalLazy.Undefined

I think its an issue of

        (* Finished tunneling, now cleanly close the chans *)
        >>= close_chans (tcp_ic, tcp_oc)
        |> Lwt.ignore_result

in usbmux.ml because lwt's establish_server has its own logic on closing the server's connections and uses lazy in its implementation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.