Skip to content

Commit

Permalink
Merge pull request #240 from 6WIND/lib-friendly
Browse files Browse the repository at this point in the history
Make scapy usable as a third party library
  • Loading branch information
p-l- committed Aug 1, 2016
2 parents 1e48e13 + 4d31afe commit 9201f1c
Show file tree
Hide file tree
Showing 76 changed files with 412 additions and 293 deletions.
64 changes: 32 additions & 32 deletions scapy/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,41 @@
Aggregate top level objects from all Scapy modules.
"""

from base_classes import *
from config import *
from dadict import *
from data import *
from error import *
from themes import *
from arch import *

from plist import *
from fields import *
from packet import *
from asn1fields import *
from asn1packet import *

from utils import *
from route import *
from scapy.base_classes import *
from scapy.config import *
from scapy.dadict import *
from scapy.data import *
from scapy.error import *
from scapy.themes import *
from scapy.arch import *

from scapy.plist import *
from scapy.fields import *
from scapy.packet import *
from scapy.asn1fields import *
from scapy.asn1packet import *

from scapy.utils import *
from scapy.route import *
if conf.ipv6_enabled:
from utils6 import *
from route6 import *
from sendrecv import *
from supersocket import *
from volatile import *
from as_resolvers import *
from scapy.utils6 import *
from scapy.route6 import *
from scapy.sendrecv import *
from scapy.supersocket import *
from scapy.volatile import *
from scapy.as_resolvers import *

from ansmachine import *
from automaton import *
from autorun import *
from scapy.ansmachine import *
from scapy.automaton import *
from scapy.autorun import *

from main import *
from scapy.main import *

from layers.all import *
from scapy.layers.all import *

from asn1.asn1 import *
from asn1.ber import *
from asn1.mib import *
from scapy.asn1.asn1 import *
from scapy.asn1.ber import *
from scapy.asn1.mib import *

from pipetool import *
from scapypipes import *
from scapy.pipetool import *
from scapy.scapypipes import *
6 changes: 3 additions & 3 deletions scapy/ansmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
## Answering machines ##
########################

from sendrecv import send,sendp,sniff
from config import conf
from error import log_interactive
from scapy.sendrecv import send,sendp,sniff
from scapy.config import conf
from scapy.error import log_interactive

class ReferenceAM(type):
def __new__(cls, name, bases, dct):
Expand Down
16 changes: 9 additions & 7 deletions scapy/arch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@


import sys,os,socket

from scapy.error import *
import scapy.config
from scapy.pton_ntop import inet_pton

try:
from matplotlib import get_backend as matplotlib_get_backend
Expand Down Expand Up @@ -79,19 +81,19 @@ def get_if_hwaddr(iff):


if LINUX:
from linux import *
from scapy.arch.linux import *
if scapy.config.conf.use_pcap or scapy.config.conf.use_dnet:
from pcapdnet import *
from scapy.arch.pcapdnet import *
elif BSD:
from bsd import LOOPBACK_NAME
from unix import read_routes, read_routes6, in6_getifaddr
from scapy.arch.bsd import LOOPBACK_NAME
from scapy.arch.unix import read_routes, read_routes6, in6_getifaddr
scapy.config.conf.use_pcap = True
scapy.config.conf.use_dnet = True
from pcapdnet import *
from scapy.arch.pcapdnet import *
elif SOLARIS:
from solaris import *
from scapy.arch.solaris import *
elif WINDOWS:
from windows import *
from scapy.arch.windows import *

if scapy.config.conf.iface is None:
scapy.config.conf.iface = LOOPBACK_NAME
Expand Down
3 changes: 2 additions & 1 deletion scapy/arch/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
import sys,os,struct,socket,time
from select import select
from fcntl import ioctl

import scapy.utils
import scapy.utils6
from scapy.packet import Packet, Padding
from scapy.config import conf
from scapy.data import *
from scapy.supersocket import SuperSocket
import scapy.arch
from scapy.error import warning, Scapy_Exception
from scapy.error import warning, Scapy_Exception, log_interactive, log_loading
from scapy.arch.common import get_if


Expand Down
12 changes: 6 additions & 6 deletions scapy/arch/pcapdnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"""

import time,struct,sys
import socket
if not sys.platform.startswith("win"):
from fcntl import ioctl

from scapy.data import *
from scapy.config import conf
from scapy.utils import warning, mac2str
from scapy.supersocket import SuperSocket
from scapy.error import Scapy_Exception
from scapy.error import Scapy_Exception, log_loading
import scapy.arch
import socket


if conf.use_winpcapy:
#mostly code from https://github.com/phaethon/scapy translated to python2.X
Expand Down Expand Up @@ -145,17 +145,17 @@ def datalink(self):
return pcap_datalink(self.pcap)
def fileno(self):
if sys.platform.startswith("win"):
error("Cannot get selectable PCAP fd on Windows")
log_loading.error("Cannot get selectable PCAP fd on Windows")
return 0
return pcap_get_selectable_fd(self.pcap)
def setfilter(self, f):
filter_exp = create_string_buffer(f)
if pcap_compile(self.pcap, byref(self.bpf_program), filter_exp, 0, -1) == -1:
error("Could not compile filter expression %s" % f)
log_loading.error("Could not compile filter expression %s" % f)
return False
else:
if pcap_setfilter(self.pcap, byref(self.bpf_program)) == -1:
error("Could not install filter %s" % f)
log_loading.error("Could not install filter %s" % f)
return False
return True
def setnonblock(self, i):
Expand Down
2 changes: 1 addition & 1 deletion scapy/arch/solaris.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@

LOOPBACK_NAME="lo0"

from unix import *
from scapy.arch.unix import *
6 changes: 3 additions & 3 deletions scapy/arch/unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@

import sys,os,struct,socket,time
from fcntl import ioctl
from scapy.error import warning
import socket

from scapy.error import warning, log_interactive
import scapy.config
import scapy.utils
from scapy.utils6 import in6_getscope, construct_source_candidate_set
from scapy.utils6 import in6_isvalid, in6_ismlladdr, in6_ismnladdr
import scapy.arch
from scapy.config import conf

import socket


##################
## Routes stuff ##
Expand Down
3 changes: 1 addition & 2 deletions scapy/arch/windows/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from scapy.base_classes import Gen, Net, SetGen
import scapy.plist as plist
from scapy.sendrecv import debug, srp1
from scapy.layers.l2 import Ether, ARP
from scapy.data import MTU, ETHER_BROADCAST, ETH_P_ARP

conf.use_pcap = False
Expand Down Expand Up @@ -211,7 +210,7 @@ def get_windows_if_list():
]

def get_ip_from_name(ifname, v6=False):
for descr, ipadrr in exec_query(['Get-WmiObject',
for descr, ipaddr in exec_query(['Get-WmiObject',
'Win32_NetworkAdapterConfiguration'],
['Description', 'IPAddress']):
if descr == ifname.strip():
Expand Down
2 changes: 1 addition & 1 deletion scapy/as_resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


import socket
from config import conf
from scapy.config import conf

class AS_resolver:
server = None
Expand Down
2 changes: 1 addition & 1 deletion scapy/asn1/asn1.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from datetime import datetime
from scapy.config import conf
from scapy.error import Scapy_Exception, warning
from scapy.volatile import RandField
from scapy.volatile import RandField, RandIP
from scapy.utils import Enum_metaclass, EnumElement, binrepr

class RandASN1Object(RandField):
Expand Down
2 changes: 1 addition & 1 deletion scapy/asn1/ber.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from scapy.error import warning
from scapy.utils import binrepr,inet_aton,inet_ntoa
from asn1 import ASN1_Decoding_Error,ASN1_Encoding_Error,ASN1_BadTag_Decoding_Error,ASN1_Codecs,ASN1_Class_UNIVERSAL,ASN1_Error,ASN1_DECODING_ERROR,ASN1_BADTAG
from scapy.asn1.asn1 import ASN1_Decoding_Error,ASN1_Encoding_Error,ASN1_BadTag_Decoding_Error,ASN1_Codecs,ASN1_Class_UNIVERSAL,ASN1_Error,ASN1_DECODING_ERROR,ASN1_BADTAG

##################
## BER encoding ##
Expand Down
21 changes: 9 additions & 12 deletions scapy/asn1fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
Classes that implement ASN.1 data structures.
"""

from asn1.asn1 import *
from asn1.ber import *
from asn1.mib import *
from volatile import *
from base_classes import BasePacket
from utils import binrepr
from scapy.asn1.asn1 import *
from scapy.asn1.ber import *
from scapy.asn1.mib import *
from scapy.volatile import *
from scapy.base_classes import BasePacket
from scapy.utils import binrepr
from scapy import packet

class ASN1F_badsequence(Exception):
pass
Expand Down Expand Up @@ -376,7 +377,7 @@ def build(self, pkt):
return self.i2m(pkt, s)

def randval(self):
return fuzz(self.asn1pkt())
return packet.fuzz(self.asn1pkt())
def __repr__(self):
return "<%s %s>" % (self.__class__.__name__, self.name)

Expand Down Expand Up @@ -496,7 +497,7 @@ def i2m(self, pkt, x):
explicit_tag=exp)
return BER_tagging_enc(s, explicit_tag=self.explicit_tag)
def randval(self):
return RandChoice(*(fuzz(x()) for x in self.choices.itervalues()))
return RandChoice(*(packet.fuzz(x()) for x in self.choices.itervalues()))

class ASN1F_PACKET(ASN1F_field):
holds_packets = 1
Expand Down Expand Up @@ -579,7 +580,3 @@ def i2repr(self, pkt, x):
pretty_s = ", ".join(self.get_flags(pkt))
return pretty_s + " " + repr(x)
return repr(x)


# This import must come last to avoid problems with cyclic dependencies
import packet
3 changes: 2 additions & 1 deletion scapy/asn1packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
Packet holding data in Abstract Syntax Notation (ASN.1).
"""

from packet import *
from scapy.base_classes import Packet_metaclass
from scapy.packet import Packet

class ASN1Packet_metaclass(Packet_metaclass):
def __new__(cls, name, bases, dct):
Expand Down
12 changes: 6 additions & 6 deletions scapy/automaton.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from select import select
from collections import deque
import thread
from config import conf
from utils import do_graph
from error import log_interactive
from plist import PacketList
from data import MTU
from supersocket import SuperSocket
from scapy.config import conf
from scapy.utils import do_graph
from scapy.error import log_interactive
from scapy.plist import PacketList
from scapy.data import MTU
from scapy.supersocket import SuperSocket

class ObjectPipe:
def __init__(self):
Expand Down
8 changes: 4 additions & 4 deletions scapy/autorun.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"""

import code,sys
from config import conf
from themes import *
from error import Scapy_Exception
from utils import tex_escape
from scapy.config import conf
from scapy.themes import *
from scapy.error import Scapy_Exception
from scapy.utils import tex_escape


#########################
Expand Down
6 changes: 3 additions & 3 deletions scapy/base_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
################

import re,random,socket
import config
import error
import types

class Gen(object):
Expand Down Expand Up @@ -202,6 +200,7 @@ def __new__(cls, name, bases, dct):
for f in newcls.fields_desc:
if hasattr(f, "register_owner"):
f.register_owner(newcls)
from scapy import config
config.conf.layers.register(newcls)
return newcls

Expand All @@ -216,6 +215,7 @@ def __call__(cls, *args, **kargs):
try:
cls = cls.dispatch_hook(*args, **kargs)
except:
from scapy import config
if config.conf.debug_dissector:
raise
cls = config.conf.raw_layer
Expand All @@ -238,7 +238,7 @@ class NewDefaultValues(Packet_metaclass):
and it should still work.
"""
def __new__(cls, name, bases, dct):
from error import log_loading
from scapy.error import log_loading
import traceback
try:
for tb in traceback.extract_stack()+[("??",-1,None,"")]:
Expand Down

0 comments on commit 9201f1c

Please sign in to comment.