3

Im working on a project in PyCharm, and I need to debug certain part of the code.

When I tried to debug, the debugger just "skipped" the breakpoints without stopping at them. After a lot of non-helpful tries in the web, I found that when I import the Scapy module, the debugger doesn't work, and when Scapy isn't imported, everything works just FINE.

Btw - Im working on Ubuntu OS.

Any ideas??

Idan Ofek
  • 87
  • 1
  • 2
  • 13

2 Answers2

5

Came across this problem myself. It is very annoying. After much debugging, got to an answer.

The cause of the problem seems to be the way scapy imports everything into the global namespace and this seems to break PyCharm (name clash, perhaps?).

By the way, this all applies to v2.3.3 of scapy from 18th October, 2016.

As scapy is loading, it eventually hits a line in scapy/all.py:

from scapy.layers.all import *

This loads scapy/layers/all.py which loads scapy/config.py. This last file initialises Conf.load_layers[] to a list of modules (in scapy/layers).

scapy/layers/all.py then loops through this list, calling _import_star() on each module.

After it loads scapy/layers/x509.py, all breakpoints in PyCharm stop working.

I've FOUR solutions for you, pick the one you like best ...

(1) If you don't use anything to do with X509, you could simply remove this module from the list assigned to Conf.load_layers[] in scapy/config.py (line 383 in my copy of config.py). WARNING: THIS IS A REAL HACK - please avoid doing it unless there is no other way forward for you.

If you need to temporally debug, you can also use this code sample:

from scapy import config
config.Conf.load_layers.remove("x509")
from scapy.all import *

(2) The problem is with symbols being imported into the global namespace. This is fine for classes, bad for constants. There is code in _import_star() that checks the name of the symbol and does NOT load it into the global namespace if it begins with a _ (i.e. a "private" name). You could modify this function to treat the x509 module specially by ignoring names that do not begin X509_. Hopefully this will import the classes defined in x509 and not the constants. Here is a sample patch:

*** layers/all.py   2017-03-31 12:44:00.673248054 +0100
--- layers/all.py   2017-03-31 12:44:00.673248054 +0100
***************
*** 21,26 ****
--- 21,32 ----
          for name in mod.__dict__['__all__']:
              __all__.append(name)
              globals()[name] = mod.__dict__[name]
+     elif m == "x509":
+         # import but rename as we go ...
+         for name, sym in mod.__dict__.iteritems():
+             if name[0] != '_' and name[:5] != "X509_":
+                 __all__.append("_x509_" + name)
+                 globals()["_x509_" + name] = sym
      else:
          # import all the non-private symbols
          for name, sym in mod.__dict__.iteritems():

WARNING: THIS IS A REAL HACK - please avoid doing it unless there is no other way forward for you.

(3) This is a variation on solution (2), so also A REAL HACK (etc. etc.). You could edit scapy/layers/x509.py and prepend a _ to all constants. For example, all instances of default_directoryName should be changed to _default_directoryName. I found the following constants that needed changing: default_directoryName, reasons_mapping, cRL_reasons, ext_mapping, default_issuer, default_subject, attrName_mapping and attrName_specials. This is nice as it matches a fix applied to x509.py that I found in the scapy git repo ...

(4) You could just update to the next version of scapy. I don't know if this will be v2.3.4 or v2.4, as there is (at the time of writing) no next version released yet. So, while this (lack of a new release) continues, you could update to the latest development version (where they have already fixed this problem on Feb 8th 2017). I use scapy installed under my home directory (rather than in the system python packages location), so I did the following:

pip uninstall scapy
git clone https://github.com/secdev/scapy /tmp/scapy
cd /tmp/scapy
python setup.py install --user
cd -
rm -rf /tmp/scapy

Good luck !

Spiceisland
  • 61
  • 1
  • 4
1

I can not comment on Spiceisland's response because of lack of reputation points but with current version of scapy 2.3.3.dev532 I can see the same issues with tls layer as pointed out by Spiceisland with x509. Therefore all workarounds and fixes have to be applied accordingly for tls module.

So simplest quick and dirty fix (and you won't be able to use TLS after that):

In scapy/config.py remove "tls" element from load_layers list (that's line 434 in the 2.3.3.dev532 version of scapy)

I have also filed a bug for this issue https://github.com/secdev/scapy/issues/746

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/16895402) – mbelsky Aug 01 '17 at 11:15
  • Thank you for your comment, I have updated my answer with straightforward solution that should not require clarification. – fakej Gazeta.pl Aug 01 '17 at 12:13