Skip to content

Commit d54a457

Browse files
committed
Support stupid OSes
1 parent f9113dc commit d54a457

File tree

7 files changed

+117
-79
lines changed

7 files changed

+117
-79
lines changed

scapy/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ def __getattribute__(self, attr):
952952

953953
if not Conf.ipv6_enabled:
954954
log_scapy.warning("IPv6 support disabled in Python. Cannot load Scapy IPv6 layers.") # noqa: E501
955-
for m in ["inet6", "dhcp6"]:
955+
for m in ["inet6", "dhcp6", "sixlowpan"]:
956956
if m in Conf.load_layers:
957957
Conf.load_layers.remove(m)
958958

scapy/layers/dns.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,32 @@
2626
from scapy.compat import orb, raw, chb, bytes_encode, plain_str
2727
from scapy.error import log_runtime, warning, Scapy_Exception
2828
from scapy.packet import Packet, bind_layers, Raw
29-
from scapy.fields import BitEnumField, BitField, ByteEnumField, ByteField, \
30-
ConditionalField, Field, FieldLenField, FlagsField, IntField, \
31-
PacketListField, ShortEnumField, ShortField, StrField, \
32-
StrLenField, MultipleTypeField, UTCTimeField, I
29+
from scapy.fields import (
30+
BitEnumField,
31+
BitField,
32+
ByteEnumField,
33+
ByteField,
34+
ConditionalField,
35+
Field,
36+
FieldLenField,
37+
FlagsField,
38+
I,
39+
IP6Field,
40+
IntField,
41+
MultipleTypeField,
42+
PacketListField,
43+
ShortEnumField,
44+
ShortField,
45+
StrField,
46+
StrLenField,
47+
UTCTimeField,
48+
)
3349
from scapy.sendrecv import sr1
3450
from scapy.supersocket import StreamSocket
3551
from scapy.pton_ntop import inet_ntop, inet_pton
3652
from scapy.volatile import RandShort
3753

3854
from scapy.layers.inet import IP, DestIPField, IPField, UDP, TCP
39-
from scapy.layers.inet6 import IPv6, DestIP6Field, IP6Field
40-
4155

4256
from typing import (
4357
Any,
@@ -1103,7 +1117,9 @@ def pre_dissect(self, s):
11031117
bind_layers(UDP, DNS, dport=53)
11041118
bind_layers(UDP, DNS, sport=53)
11051119
DestIPField.bind_addr(UDP, "224.0.0.251", dport=5353)
1106-
DestIP6Field.bind_addr(UDP, "ff02::fb", dport=5353)
1120+
if conf.ipv6_enabled:
1121+
from scapy.layers.inet6 import DestIP6Field
1122+
DestIP6Field.bind_addr(UDP, "ff02::fb", dport=5353)
11071123
bind_layers(TCP, DNS, dport=53)
11081124
bind_layers(TCP, DNS, sport=53)
11091125

@@ -1135,6 +1151,7 @@ def dns_resolve(qname, qtype="A", raw=False, verbose=1, **kwargs):
11351151

11361152
kwargs.setdefault("timeout", 3)
11371153
kwargs.setdefault("verbose", 0)
1154+
res = None
11381155
for nameserver in conf.nameservers:
11391156
# Try all nameservers
11401157
try:
@@ -1310,6 +1327,7 @@ def is_request(self, req):
13101327
)
13111328

13121329
def make_reply(self, req):
1330+
from scapy.layers.inet6 import IPv6
13131331
if IPv6 in req:
13141332
resp = IPv6(dst=req[IPv6].src, src=self.src_ip6)
13151333
else:

scapy/layers/hsrp.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
http://www.smartnetworks.jp/2006/02/hsrp_8_hsrp_version_2.html
1313
"""
1414

15+
from scapy.config import conf
1516
from scapy.fields import ByteEnumField, ByteField, IPField, SourceIPField, \
1617
StrFixedLenField, XIntField, XShortField
1718
from scapy.packet import Packet, bind_layers, bind_bottom_up
1819
from scapy.layers.inet import DestIPField, UDP
19-
from scapy.layers.inet6 import DestIP6Field
2020

2121

2222
class HSRP(Packet):
@@ -66,4 +66,6 @@ def post_build(self, p, pay):
6666
bind_layers(UDP, HSRP, dport=1985, sport=1985)
6767
bind_layers(UDP, HSRP, dport=2029, sport=2029)
6868
DestIPField.bind_addr(UDP, "224.0.0.2", dport=1985)
69-
DestIP6Field.bind_addr(UDP, "ff02::66", dport=2029)
69+
if conf.ipv6_enabled:
70+
from scapy.layers.inet6 import DestIP6Field
71+
DestIP6Field.bind_addr(UDP, "ff02::66", dport=2029)

scapy/layers/ntp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
FieldListField,
2323
FixedPointField,
2424
FlagsField,
25+
IP6Field,
2526
IPField,
2627
IntField,
2728
LEIntField,
@@ -39,7 +40,6 @@
3940
XByteField,
4041
XStrFixedLenField,
4142
)
42-
from scapy.layers.inet6 import IP6Field
4343
from scapy.layers.inet import UDP
4444
from scapy.utils import lhex
4545
from scapy.compat import orb

scapy/main.py

Lines changed: 79 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,84 @@ def _len(line):
561561
return lines
562562

563563

564+
def get_fancy_banner(mini: Optional[bool] = None) -> str:
565+
"""
566+
Generates the fancy Scapy banner
567+
568+
:param mini: if set, force a mini banner or not. Otherwise detect
569+
"""
570+
from scapy.config import conf
571+
from scapy.utils import get_terminal_width
572+
if mini is None:
573+
mini_banner = (get_terminal_width() or 84) <= 75
574+
else:
575+
mini_banner = mini
576+
577+
the_logo = [
578+
" ",
579+
" aSPY//YASa ",
580+
" apyyyyCY//////////YCa ",
581+
" sY//////YSpcs scpCY//Pp ",
582+
" ayp ayyyyyyySCP//Pp syY//C ",
583+
" AYAsAYYYYYYYY///Ps cY//S",
584+
" pCCCCY//p cSSps y//Y",
585+
" SPPPP///a pP///AC//Y",
586+
" A//A cyP////C",
587+
" p///Ac sC///a",
588+
" P////YCpc A//A",
589+
" scccccp///pSP///p p//Y",
590+
" sY/////////y caa S//P",
591+
" cayCyayP//Ya pY/Ya",
592+
" sY/PsY////YCc aC//Yp ",
593+
" sc sccaCY//PCypaapyCP//YSs ",
594+
" spCPY//////YPSps ",
595+
" ccaacs ",
596+
" ",
597+
]
598+
599+
# Used on mini screens
600+
the_logo_mini = [
601+
" .SYPACCCSASYY ",
602+
"P /SCS/CCS ACS",
603+
" /A AC",
604+
" A/PS /SPPS",
605+
" YP (SC",
606+
" SPS/A. SC",
607+
" Y/PACC PP",
608+
" PY*AYC CAA",
609+
" YYCY//SCYP ",
610+
]
611+
612+
the_banner = [
613+
"",
614+
"",
615+
" |",
616+
" | Welcome to Scapy",
617+
" | Version %s" % conf.version,
618+
" |",
619+
" | https://github.com/secdev/scapy",
620+
" |",
621+
" | Have fun!",
622+
" |",
623+
]
624+
625+
if mini_banner:
626+
the_logo = the_logo_mini
627+
the_banner = [x[2:] for x in the_banner[3:-1]]
628+
the_banner = [""] + the_banner + [""]
629+
else:
630+
quote, author = choice(QUOTES)
631+
the_banner.extend(_prepare_quote(quote, author, max_len=39))
632+
the_banner.append(" |")
633+
return "\n".join(
634+
logo + banner for logo, banner in zip_longest(
635+
(conf.color_theme.logo(line) for line in the_logo),
636+
(conf.color_theme.success(line) for line in the_banner),
637+
fillvalue=""
638+
)
639+
)
640+
641+
564642
def interact(mydict=None, argv=None, mybanner=None, loglevel=logging.INFO):
565643
# type: (Optional[Any], Optional[Any], Optional[Any], int) -> None
566644
"""
@@ -635,72 +713,7 @@ def interact(mydict=None, argv=None, mybanner=None, loglevel=logging.INFO):
635713
)
636714

637715
if conf.fancy_banner:
638-
from scapy.utils import get_terminal_width
639-
mini_banner = (get_terminal_width() or 84) <= 75
640-
641-
the_logo = [
642-
" ",
643-
" aSPY//YASa ",
644-
" apyyyyCY//////////YCa ",
645-
" sY//////YSpcs scpCY//Pp ",
646-
" ayp ayyyyyyySCP//Pp syY//C ",
647-
" AYAsAYYYYYYYY///Ps cY//S",
648-
" pCCCCY//p cSSps y//Y",
649-
" SPPPP///a pP///AC//Y",
650-
" A//A cyP////C",
651-
" p///Ac sC///a",
652-
" P////YCpc A//A",
653-
" scccccp///pSP///p p//Y",
654-
" sY/////////y caa S//P",
655-
" cayCyayP//Ya pY/Ya",
656-
" sY/PsY////YCc aC//Yp ",
657-
" sc sccaCY//PCypaapyCP//YSs ",
658-
" spCPY//////YPSps ",
659-
" ccaacs ",
660-
" ",
661-
]
662-
663-
# Used on mini screens
664-
the_logo_mini = [
665-
" .SYPACCCSASYY ",
666-
"P /SCS/CCS ACS",
667-
" /A AC",
668-
" A/PS /SPPS",
669-
" YP (SC",
670-
" SPS/A. SC",
671-
" Y/PACC PP",
672-
" PY*AYC CAA",
673-
" YYCY//SCYP ",
674-
]
675-
676-
the_banner = [
677-
"",
678-
"",
679-
" |",
680-
" | Welcome to Scapy",
681-
" | Version %s" % conf.version,
682-
" |",
683-
" | https://github.com/secdev/scapy",
684-
" |",
685-
" | Have fun!",
686-
" |",
687-
]
688-
689-
if mini_banner:
690-
the_logo = the_logo_mini
691-
the_banner = [x[2:] for x in the_banner[3:-1]]
692-
the_banner = [""] + the_banner + [""]
693-
else:
694-
quote, author = choice(QUOTES)
695-
the_banner.extend(_prepare_quote(quote, author, max_len=39))
696-
the_banner.append(" |")
697-
banner_text = "\n".join(
698-
logo + banner for logo, banner in zip_longest(
699-
(conf.color_theme.logo(line) for line in the_logo),
700-
(conf.color_theme.success(line) for line in the_banner),
701-
fillvalue=""
702-
)
703-
)
716+
banner_text = get_fancy_banner()
704717
else:
705718
banner_text = "Welcome to Scapy (%s)" % conf.version
706719
if mybanner is not None:

scapy/pton_ntop.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ def inet_pton(af, addr):
8787
addr = plain_str(addr)
8888
# Use inet_pton if available
8989
try:
90+
if not socket.has_ipv6:
91+
raise AttributeError
9092
return socket.inet_pton(af, addr)
9193
except AttributeError:
9294
try:
@@ -134,6 +136,8 @@ def inet_ntop(af, addr):
134136
# Use inet_ntop if available
135137
addr = bytes_encode(addr)
136138
try:
139+
if not socket.has_ipv6:
140+
raise AttributeError
137141
return socket.inet_ntop(af, addr)
138142
except AttributeError:
139143
try:

scapy/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,8 +2927,9 @@ def get_terminal_width():
29272927
s = struct.pack('HHHH', 0, 0, 0, 0)
29282928
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
29292929
sizex = struct.unpack('HHHH', x)[1]
2930-
except IOError:
2931-
pass
2930+
except (IOError, ModuleNotFoundError):
2931+
# If everything failed, return default terminal size
2932+
sizex = 79
29322933
return sizex
29332934

29342935

0 commit comments

Comments
 (0)