Skip to content

Commit

Permalink
Implement global & extensible interfaces (#2707)
Browse files Browse the repository at this point in the history
* Implement global & extensible interfaces

* Simplify detection of valid interfaces

* Linux: handle interfaces with no IPv4

* Reimplement get_working_ifaces

* Remove 'main' IPv6 in interfaces

* Don't show invalid interfaces by default

* Update error message
  • Loading branch information
gpotter2 authored Sep 24, 2020
1 parent 96e0094 commit 4521d8a
Show file tree
Hide file tree
Showing 35 changed files with 1,155 additions and 649 deletions.
6 changes: 3 additions & 3 deletions .config/ci/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# Install on osx
if [ "$OSTYPE" = "darwin"* ] || [ "$TRAVIS_OS_NAME" = "osx" ]
then
if [ ! -z $SCAPY_USE_PCAPDNET ]
if [ ! -z $SCAPY_USE_LIBPCAP ]
then
brew update
brew install libdnet libpcap
brew install libpcap
fi
fi

Expand All @@ -18,7 +18,7 @@ then
fi

# Make sure libpcap is installed
if [ ! -z $SCAPY_USE_PCAPDNET ]
if [ ! -z $SCAPY_USE_LIBPCAP ]
then
sudo apt-get -qy install libpcap-dev || exit 1
fi
Expand Down
1 change: 1 addition & 0 deletions .config/codespell_ignore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fo
gost
iff
inout
microsof
mitre
nd
negociate
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
- os: linux
python: 3.8
env:
- SCAPY_USE_PCAPDNET=yes TOXENV=py38-linux_root,codecov
- SCAPY_USE_LIBPCAP=yes TOXENV=py38-linux_root,codecov

# warnings/deprecations
- os: linux
Expand Down
12 changes: 12 additions & 0 deletions doc/scapy/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ Use ``get_if_list()`` to get the interface list
>>> get_if_list()
['lo', 'eth0']
You can also use the :py:attr:`conf.ifaces <scapy.interfaces.NetworkInterfaceDict>` object to get interfaces.
In this example, the object is first displayed as as column. Then, the :py:attr:`dev_from_index() <scapy.interfaces.NetworkInterfaceDict.dev_from_index>` is used to access the interface at index 2.

.. code-block:: pycon
>>> conf.ifaces
SRC INDEX IFACE IPv4 IPv6 MAC
sys 2 eth0 10.0.0.5 fe80::10a:2bef:dc12:afae Microsof:12:cb:ef
sys 1 lo 127.0.0.1 ::1 00:00:00:00:00:00
>>> conf.ifaces.dev_from_index(2)
<NetworkInterface eth0 [UP+BROADCAST+RUNNING+SLAVE]>
IPv4 routes
-----------

Expand Down
2 changes: 1 addition & 1 deletion doc/scapy/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ Provided that your wireless card and driver are correctly configured for frame i

On Windows, if using Npcap, the equivalent would be to call::

>>> # Of course, conf.iface can be replaced by any interfaces accessed through IFACES
>>> # Of course, conf.iface can be replaced by any interfaces accessed through conf.ifaces
... conf.iface.setmonitor(True)

you can have a kind of FakeAP::
Expand Down
4 changes: 2 additions & 2 deletions run_scapy.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
set PYTHONPATH=%~dp0
REM shift will not work with %*
set "_args=%*"
IF "%1" == "--2" (
IF "%1" == "-2" (
set PYTHON=python
set "_args=%_args:~3%"
) ELSE IF "%1" == "--3" (
) ELSE IF "%1" == "-3" (
set PYTHON=python3
set "_args=%_args:~3%"
)
Expand Down
1 change: 1 addition & 0 deletions scapy/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from scapy.error import *
from scapy.themes import *
from scapy.arch import *
from scapy.interfaces import *

from scapy.plist import *
from scapy.fields import *
Expand Down
21 changes: 12 additions & 9 deletions scapy/arch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@
from scapy.compat import orb


# Duplicated from scapy/utils.py for import reasons

def str2mac(s):
return ("%02x:" * 6)[:-1] % tuple(orb(x) for x in s)


if not WINDOWS:
if not conf.use_pcap:
from scapy.arch.bpf.core import get_if_raw_addr


def get_if_addr(iff):
return inet_ntop(socket.AF_INET, get_if_raw_addr(iff))
"""
Returns the IPv4 of an interface or "0.0.0.0" if not available
"""
return inet_ntop(socket.AF_INET, get_if_raw_addr(iff)) # noqa: F405


def get_if_hwaddr(iff):
"""
Returns the MAC (hardware) address of an interface
"""
addrfamily, mac = get_if_raw_hwaddr(iff) # noqa: F405
if addrfamily in [ARPHDR_ETHER, ARPHDR_LOOPBACK]:
return str2mac(mac)
Expand All @@ -51,23 +54,23 @@ def get_if_hwaddr(iff):
# def get_if(iff,cmd):
# def get_if_index(iff):

from scapy.interfaces import get_working_if # noqa F401

if LINUX:
from scapy.arch.linux import * # noqa F403
elif BSD:
from scapy.arch.unix import read_routes, read_routes6, in6_getifaddr # noqa: F401, E501
from scapy.arch.bpf.core import * # noqa F403
if not conf.use_pcap:
# Native
from scapy.arch.bpf.supersocket import * # noqa F403
from scapy.arch.bpf.supersocket import * # noqa F403
conf.use_bpf = True
elif SOLARIS:
from scapy.arch.solaris import * # noqa F403
elif WINDOWS:
from scapy.arch.windows import * # noqa F403
from scapy.arch.windows.native import * # noqa F403

if conf.iface is None:
conf.iface = conf.loopback_name

_set_conf_sockets() # Apply config

Expand Down
Loading

0 comments on commit 4521d8a

Please sign in to comment.