ipyida icon indicating copy to clipboard operation
ipyida copied to clipboard

Wrap sys.displayhook and support notebook kernel

Open NyaMisty opened this issue 3 years ago • 4 comments

  1. Actually ida_ipython can receive jupyter notebook's connection file by JUPYTER_CONNECTION environment variable. In this way IDA can be spawned by Jupyter Notebook, and being like a normal IPython kernel. image

  2. sys.displayhook are currently being used by IDAPython to display REPR's result, so we have to wrap it as well

  3. Placing ipyida_plugin_stub.py under ipyida package is confusing, as the stub file will never able to import the ipyida package when being called directly from the source tree. So we can simply move it out, and everything will be working fine

NyaMisty avatar Aug 09 '21 23:08 NyaMisty

Hi! 👋

I like how minimal the changes are to support notebook! And it doesn't change any behavior under "normal"/qtconsole operation. I can certainly merge this to release it the next version.

Now, I also want to provide proper instructions on how to use this feature. I was reading ida_ipython instructions (https://github.com/james91b/ida_ipython#using-the-notebook) but can't really figure out how to launch this properly. Could it work on non-Windows OSes? Should IDA be started before the notebook or the other way around? What should JUPYTER_CONNECTION contain? A path to some JSON file right?

Thanks! M-E

marc-etienne avatar Aug 23 '21 14:08 marc-etienne

Well I've tried to document it, but it's pretty hard to describe.

What's the JUPYTER_CONNECTION

The JUPYTER_CONNECTION is simply the kernel connection file, which is a path to json, like this;

[ipyida] IPython Kernel initialized! You can connect using: 
        kernel-31584.json

Without jupyter notebook, the connection file is generated by IPython kernel in IDA itself, and with jupyter, the connection file will be generated by jupyter's kernel manager instead, so that kernel will listen to that preconfigured address, and then got connected by jupyter. But when passed to IDA, the json path should be absolute path. ;)

How to spawn the notebook

Jupyter actually manages kernel by spawning a process, passing the connection file to it, and then monitor the process, reporting process's death and restarting the dead kernels. The command line of process that jupyter spawn is defined in the kernel spec file. So, as you can see in the https://github.com/tmr232/ida_ipython/blob/master/generate_kernels.py, ida_ipython is wrapping IDA using a script that launches the IDA with JUPYTER_CONNECTION env set up: generate_kernels.py generated the kernel spec file, installing it using jupyter-kernelspec install, and then in jupyter, when you select IDA32/IDA64, jupyter will spawn the wrapper script launch_ida.py, and then IDA being spawned. In theory this can be achieved on all platform as long as they correctly handle the environment variable. But I don'w own Mac and Linux IDA. And I'm setting up the environment using WSL instead (and it's another long journal), so I can't really give a example. But it should be really easy to configure given that you have found tmr232's repo instead of james91b's one ;)

NyaMisty avatar Aug 23 '21 14:08 NyaMisty

also note: I moved the script ipyida/ipyida_plugin_stub.py 's location, as I'm using an old way that clone the repo to local and loading it using a plugin loader, so I need a plugin entry that can be import from everywhere (not only from the folder). It'll be pretty happy for users like me, but it will also break setup.py, which relies on pkg_resource to install that py script to target plugin folder :(

NyaMisty avatar Aug 23 '21 14:08 NyaMisty

Great! I'll test it out and see how I can perhaps automate some of the process maybe. I guess it should work on other OSes given the path to the executable in the connection file it set accordingly.

marc-etienne avatar Aug 23 '21 16:08 marc-etienne

Hey @NyaMisty! After so much time IPyIDA 2.0 finally support Jupyter Notebook, but I took a drastically different approach to be able to start the notebook from IDA. Thanks for your contribution nonetheless :) Meanwhile perhaps others were using your fork and documentation.

Cheers!

marc-etienne avatar Jan 09 '23 16:01 marc-etienne