Skip to content
Snippets Groups Projects
Commit 4ad79777 authored by Pierre LALET's avatar Pierre LALET
Browse files

Fix imports and global variables

parent e6904fac
No related branches found
No related tags found
No related merge requests found
...@@ -11,21 +11,30 @@ from __future__ import absolute_import ...@@ -11,21 +11,30 @@ from __future__ import absolute_import
from __future__ import print_function from __future__ import print_function
import atexit
import code
import getopt
import glob import glob
import gzip import gzip
import importlib import importlib
import logging
import os import os
from random import choice from random import choice
import re
import sys import sys
import types import types
from scapy.config import conf
from scapy.error import log_interactive, log_loading, log_scapy, warning
import scapy.modules.six as six import scapy.modules.six as six
from scapy.error import * from scapy.themes import DefaultTheme
from scapy import utils
ignored = list(six.moves.builtins.__dict__.keys()) IGNORED = list(six.moves.builtins.__dict__)
GLOBKEYS = []
LAYER_ALIASES = { LAYER_ALIASES = {
"tls": "tls.all" "tls": "tls.all"
...@@ -62,12 +71,12 @@ def _validate_local(x): ...@@ -62,12 +71,12 @@ def _validate_local(x):
"""Returns whether or not a variable should be imported. """Returns whether or not a variable should be imported.
Will return False for any default modules (sys), or if Will return False for any default modules (sys), or if
they are detected as private vars (starting with a _)""" they are detected as private vars (starting with a _)"""
global ignored global IGNORED
return x[0] != "_" and not x in ignored return x[0] != "_" and not x in IGNORED
DEFAULT_PRESTART_FILE = _probe_config_file(".scapy_prestart.py") DEFAULT_PRESTART_FILE = _probe_config_file(".scapy_prestart.py")
DEFAULT_STARTUP_FILE = _probe_config_file(".scapy_startup.py") DEFAULT_STARTUP_FILE = _probe_config_file(".scapy_startup.py")
session = None SESSION = None
def _usage(): def _usage():
print("""Usage: scapy.py [-s sessionfile] [-c new_startup_file] [-p new_prestart_file] [-C] [-P] print("""Usage: scapy.py [-s sessionfile] [-c new_startup_file] [-p new_prestart_file] [-C] [-P]
...@@ -76,10 +85,6 @@ def _usage(): ...@@ -76,10 +85,6 @@ def _usage():
sys.exit(0) sys.exit(0)
from scapy.config import conf
from scapy.themes import DefaultTheme
###################### ######################
## Extension system ## ## Extension system ##
###################### ######################
...@@ -177,7 +182,6 @@ def list_contrib(name=None): ...@@ -177,7 +182,6 @@ def list_contrib(name=None):
def save_session(fname=None, session=None, pickleProto=-1): def save_session(fname=None, session=None, pickleProto=-1):
from scapy import utils
if fname is None: if fname is None:
fname = conf.session fname = conf.session
if not fname: if not fname:
...@@ -235,17 +239,17 @@ def update_session(fname=None): ...@@ -235,17 +239,17 @@ def update_session(fname=None):
scapy_session.update(s) scapy_session.update(s)
def init_session(session_name, mydict=None): def init_session(session_name, mydict=None):
global session global SESSION
global globkeys global GLOBKEYS
scapy_builtins = {k: v for k, v in six.iteritems(importlib.import_module(".all", "scapy").__dict__) if _validate_local(k)} scapy_builtins = {k: v for k, v in six.iteritems(importlib.import_module(".all", "scapy").__dict__) if _validate_local(k)}
six.moves.builtins.__dict__.update(scapy_builtins) six.moves.builtins.__dict__.update(scapy_builtins)
globkeys = list(scapy_builtins.keys()) GLOBKEYS.extend(scapy_builtins)
globkeys.append("scapy_session") GLOBKEYS.append("scapy_session")
scapy_builtins=None # XXX replace with "with" statement scapy_builtins=None # XXX replace with "with" statement
if mydict is not None: if mydict is not None:
six.moves.builtins.__dict__.update(mydict) six.moves.builtins.__dict__.update(mydict)
globkeys += list(mydict.keys()) GLOBKEYS.extend(mydict)
if session_name: if session_name:
try: try:
...@@ -255,26 +259,26 @@ def init_session(session_name, mydict=None): ...@@ -255,26 +259,26 @@ def init_session(session_name, mydict=None):
else: else:
try: try:
try: try:
session = six.moves.cPickle.load(gzip.open(session_name,"rb")) SESSION = six.moves.cPickle.load(gzip.open(session_name,"rb"))
except IOError: except IOError:
session = six.moves.cPickle.load(open(session_name,"rb")) SESSION = six.moves.cPickle.load(open(session_name,"rb"))
log_loading.info("Using session [%s]" % session_name) log_loading.info("Using session [%s]" % session_name)
except EOFError: except EOFError:
log_loading.error("Error opening session [%s]" % session_name) log_loading.error("Error opening session [%s]" % session_name)
except AttributeError: except AttributeError:
log_loading.error("Error opening session [%s]. Attribute missing" % session_name) log_loading.error("Error opening session [%s]. Attribute missing" % session_name)
if session: if SESSION:
if "conf" in session: if "conf" in SESSION:
conf.configure(session["conf"]) conf.configure(SESSION["conf"])
session["conf"] = conf SESSION["conf"] = conf
else: else:
conf.session = session_name conf.session = session_name
session={"conf":conf} SESSION = {"conf":conf}
else: else:
session={"conf": conf} SESSION = {"conf": conf}
six.moves.builtins.__dict__["scapy_session"] = session six.moves.builtins.__dict__["scapy_session"] = SESSION
################ ################
##### Main ##### ##### Main #####
...@@ -311,7 +315,6 @@ to be used in the fancy prompt. ...@@ -311,7 +315,6 @@ to be used in the fancy prompt.
return lines return lines
def scapy_write_history_file(readline): def scapy_write_history_file(readline):
from scapy import utils
if conf.histfile: if conf.histfile:
try: try:
readline.write_history_file(conf.histfile) readline.write_history_file(conf.histfile)
...@@ -326,10 +329,8 @@ def scapy_write_history_file(readline): ...@@ -326,10 +329,8 @@ def scapy_write_history_file(readline):
def interact(mydict=None,argv=None,mybanner=None,loglevel=20): def interact(mydict=None,argv=None,mybanner=None,loglevel=20):
global session global SESSION
global globkeys global GLOBKEYS
import code,sys,os,getopt,re
from scapy.config import conf
conf.interactive = True conf.interactive = True
if loglevel is not None: if loglevel is not None:
conf.logLevel=loglevel conf.logLevel=loglevel
...@@ -434,9 +435,8 @@ def interact(mydict=None,argv=None,mybanner=None,loglevel=20): ...@@ -434,9 +435,8 @@ def interact(mydict=None,argv=None,mybanner=None,loglevel=20):
the_banner += "\n" the_banner += "\n"
the_banner += mybanner the_banner += mybanner
import atexit
try: try:
import rlcompleter,readline import readline, rlcompleter
except ImportError: except ImportError:
log_loading.info("Can't load Python libreadline or completer") log_loading.info("Can't load Python libreadline or completer")
READLINE=0 READLINE=0
...@@ -446,7 +446,7 @@ def interact(mydict=None,argv=None,mybanner=None,loglevel=20): ...@@ -446,7 +446,7 @@ def interact(mydict=None,argv=None,mybanner=None,loglevel=20):
def global_matches(self, text): def global_matches(self, text):
matches = [] matches = []
n = len(text) n = len(text)
for lst in [dir(six.moves.builtins), session]: for lst in [dir(six.moves.builtins), SESSION]:
for word in lst: for word in lst:
if word[:n] == text and word != "__builtins__": if word[:n] == text and word != "__builtins__":
matches.append(word) matches.append(word)
...@@ -462,7 +462,7 @@ def interact(mydict=None,argv=None,mybanner=None,loglevel=20): ...@@ -462,7 +462,7 @@ def interact(mydict=None,argv=None,mybanner=None,loglevel=20):
object = eval(expr) object = eval(expr)
except: except:
try: try:
object = eval(expr, session) object = eval(expr, SESSION)
except (NameError, AttributeError): except (NameError, AttributeError):
return [] return []
from scapy.packet import Packet, Packet_metaclass from scapy.packet import Packet, Packet_metaclass
...@@ -499,32 +499,32 @@ def interact(mydict=None,argv=None,mybanner=None,loglevel=20): ...@@ -499,32 +499,32 @@ def interact(mydict=None,argv=None,mybanner=None,loglevel=20):
try: try:
import IPython import IPython
IPYTHON=True IPYTHON=True
except ImportError as e: except ImportError:
log_loading.warning("IPython not available. Using standard Python shell instead.") log_loading.warning("IPython not available. Using standard Python "
"shell instead.")
IPYTHON=False IPYTHON=False
if IPYTHON: if IPYTHON:
banner = the_banner + " using IPython %s" % IPython.__version__ banner = the_banner + " using IPython %s" % IPython.__version__
# Old way to embed IPython kept for backward compatibility # Old way to embed IPython kept for backward compatibility
try: try:
args = [''] # IPython command line args (will be seen as sys.argv) args = [''] # IPython command line args (will be seen as sys.argv)
ipshell = IPython.Shell.IPShellEmbed(args, banner = banner) ipshell = IPython.Shell.IPShellEmbed(args, banner = banner)
ipshell(local_ns=session) ipshell(local_ns=SESSION)
except AttributeError as e: except AttributeError:
pass pass
# In the IPython cookbook, see 'Updating-code-for-use-with-IPython-0.11-and-later' # In the IPython cookbook, see 'Updating-code-for-use-with-IPython-0.11-and-later'
IPython.embed(user_ns=session, banner2=banner) IPython.embed(user_ns=SESSION, banner2=banner)
else: else:
code.interact(banner=the_banner, local=session, readfunc=conf.readfunc) code.interact(banner=the_banner, local=SESSION, readfunc=conf.readfunc)
if conf.session: if conf.session:
save_session(conf.session, session) save_session(conf.session, SESSION)
for k in globkeys: for k in GLOBKEYS:
try: try:
del(six.moves.builtins.__dict__[k]) del(six.moves.builtins.__dict__[k])
except: except:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment