Skip to content
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
14 changes: 2 additions & 12 deletions doc/scapy/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ Interpreting results can help users that don't know what a port scan is, but it
Quick demo
==========

First, we play a bit and create four IP packets at once. Let's see how it works. We first instantiate the IP class. Then, we instantiate it again and we provide a destination that is worth four IP addresses (/30 gives the netmask). Using a Python idiom, we develop this implicit packet in a set of explicit packets. Then, we quit the interpreter. As we provided a session file, the variables we were working on are saved, then reloaded::
First, we play a bit and create four IP packets at once. Let's see how it works. We first instantiate the IP class. Then, we instantiate it again and we provide a destination that is worth four IP addresses (/30 gives the netmask). Using a Python idiom, we develop this implicit packet in a set of explicit packets::

# ./run_scapy -s mysession
New session [mysession]
# ./run_scapy
Welcome to Scapy (2.4.0)
>>> IP()
<IP |>
Expand All @@ -76,15 +75,6 @@ First, we play a bit and create four IP packets at once. Let's see how it works.
>>> [p for p in ip]
[<IP dst=207.171.175.28 |>, <IP dst=207.171.175.29 |>,
<IP dst=207.171.175.30 |>, <IP dst=207.171.175.31 |>]
>>> ^D

::

# ./run_scapy -s mysession
Using session [mysession]
Welcome to Scapy (2.4.0)
>>> ip
<IP dst=<Net www.target.com/30> |>

Now, let's manipulate some packets::

Expand Down
7 changes: 2 additions & 5 deletions scapy/autorun.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def autorun_commands(_cmds, my_globals=None, verb=None):
my_globals = _scapy_builtins()
interp = ScapyAutorunInterpreter(locals=my_globals)
try:
del builtins.__dict__["scapy_session"]["_"]
del builtins.__dict__["_"]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the important change that fixes UTScapy.

except KeyError:
pass
if verb is not None:
Expand Down Expand Up @@ -113,10 +113,7 @@ def autorun_commands(_cmds, my_globals=None, verb=None):
pass
finally:
conf.verb = sv
try:
return builtins.__dict__["scapy_session"]["_"]
except KeyError:
return builtins.__dict__.get("_", None)
return builtins.__dict__.get("_", None)


def autorun_commands_timeout(cmds, timeout=None, **kwargs):
Expand Down
1 change: 0 additions & 1 deletion scapy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,6 @@ class Conf(ConfClass):
This object contains the configuration of Scapy.
"""
version: str = ReadOnlyAttribute("version", VERSION)
session: str = "" #: filename where the session will be saved
interactive = False
#: can be "ipython", "bpython", "ptpython", "ptipython", "python" or "auto".
#: Default: Auto
Expand Down
23 changes: 1 addition & 22 deletions scapy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,23 +445,6 @@ def list_contrib(name=None, # type: Optional[str]
return None


##############################
# Session saving/restoring #
##############################

def update_ipython_session(session):
# type: (Dict[str, Any]) -> None
"""Updates IPython session with a custom one"""
if "_oh" not in session:
session["_oh"] = session["Out"] = {}
session["In"] = {}
try:
from IPython import get_ipython
get_ipython().user_ns.update(session)
except Exception:
pass


def _scapy_prestart_builtins():
# type: () -> Dict[str, Any]
"""Load Scapy prestart and return all builtins"""
Expand Down Expand Up @@ -527,14 +510,10 @@ def init_session(mydict=None, # type: Optional[Union[Dict[str, Any], None]]
scapy_builtins.update(_scapy_exts())

SESSION = {"conf": conf} # type: Dict[str, Any]

SESSION.update(scapy_builtins)
SESSION["_scpybuiltins"] = scapy_builtins.keys()
builtins.__dict__["scapy_session"] = SESSION

if mydict is not None:
builtins.__dict__["scapy_session"].update(mydict)
update_ipython_session(mydict)
SESSION.update(mydict)
if ret:
return SESSION
return None
Expand Down
12 changes: 12 additions & 0 deletions test/regression.uts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,18 @@ assert ret == ("\\textcolor{blue}{{\\tt\\char62}{\\tt\\char62}{\\tt\\char62} }IP
ret = autorun_get_text_interactive_session("scapy_undefined")
assert "NameError" in ret[0]

= Test autorun does not reuse a previous False result
~ autorun

# The last non-None expression is the test result. A following test that only
# asserts produces no new value; it must not inherit the previous False.

out, res = autorun_get_text_interactive_session("False\n")
assert res is False

out, res = autorun_get_text_interactive_session("assert 1 == 1\n")
assert res is None

= Test autorun with logging

cmds = """log_runtime.info(hex_bytes("446166742050756e6b"))\n"""
Expand Down
Loading