Skip to content

Commit

Permalink
Remove NC code (#3824)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpotter2 committed Dec 24, 2022
1 parent ef352bd commit 9473f77
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 207 deletions.
113 changes: 0 additions & 113 deletions scapy/volatile.py
Expand Up @@ -22,7 +22,6 @@
from scapy.base_classes import Net
from scapy.compat import bytes_encode, chb, plain_str
from scapy.utils import corrupt_bits, corrupt_bytes
from scapy.libs.six.moves import zip_longest

from scapy.compat import (
List,
Expand Down Expand Up @@ -1412,115 +1411,3 @@ class CorruptedBits(CorruptedBytes):
def _fix(self):
# type: () -> bytes
return corrupt_bits(self.s, self.p, self.n)


class CyclicPattern(VolatileValue[bytes]):
"""
Generate a cyclic pattern
:param size: Size of generated pattern. Default is random size.
:param start: Start offset of the generated pattern.
:param charset_type: Charset types:
0: basic (0-9A-Za-z)
1: extended
2: maximum (almost printable chars)
The code of this class was inspired by
PEDA - Python Exploit Development Assistance for GDB
Copyright (C) 2012 Long Le Dinh <longld at vnsecurity.net>
License: This work is licensed under a Creative Commons
Attribution-NonCommercial-ShareAlike 3.0 Unported License.
"""

@staticmethod
def cyclic_pattern_charset(charset_type=None):
# type: (Optional[int]) -> str
"""
:param charset_type: charset type
0: basic (0-9A-Za-z)
1: extended (default)
2: maximum (almost printable chars)
:return: list of charset
"""

charset = \
[string.ascii_uppercase, string.ascii_lowercase, string.digits]

if charset_type == 1: # extended type
charset[1] = "%$-;" + re.sub("[sn]", "", charset[1])
charset[2] = "sn()" + charset[2]

if charset_type == 2: # maximum type
charset += [string.punctuation]

return "".join(
["".join(k) for k in zip_longest(*charset, fillvalue="")])

@staticmethod
def de_bruijn(charset, n, maxlen):
# type: (str, int, int) -> str
"""
Generate the De Bruijn Sequence up to `maxlen` characters
for the charset `charset` and subsequences of length `n`.
Algorithm modified from wikipedia
https://en.wikipedia.org/wiki/De_Bruijn_sequence
"""
k = len(charset)
a = [0] * k * n
sequence = [] # type: List[str]

def db(t, p):
# type: (int, int) -> None
if len(sequence) == maxlen:
return

if t > n:
if n % p == 0:
for j in range(1, p + 1):
sequence.append(charset[a[j]])
if len(sequence) == maxlen:
return
else:
a[t] = a[t - p]
db(t + 1, p)
for j in range(a[t - p] + 1, k):
a[t] = j
db(t + 1, t)

db(1, 1)
return ''.join(sequence)

def __init__(self, size=None, start=0, charset_type=None):
# type: (Optional[int], int, Optional[int]) -> None
self.size = size if size is not None else RandNumExpo(0.01)
self.start = start
self.charset_type = charset_type

def _command_args(self):
# type: () -> str
ret = ""
if isinstance(self.size, VolatileValue):
if self.size.lambd != 0.01 or self.size.base != 0:
ret += "size=%r" % self.size.command()
else:
ret += "size=%r" % self.size

if self.start != 0:
ret += ", start=%r" % self.start

if self.charset_type:
ret += ", charset_type=%r" % self.charset_type

return ret

def _fix(self):
# type: () -> bytes
if isinstance(self.size, VolatileValue):
size = self.size._fix()
else:
size = self.size
charset = self.cyclic_pattern_charset(self.charset_type or 0)
pattern = self.de_bruijn(charset, 3, size + self.start)
return pattern[self.start:size + self.start].encode('utf-8')
94 changes: 0 additions & 94 deletions test/random.uts
Expand Up @@ -134,97 +134,3 @@ assert de.command() == "DelayedEval(expr='3 + 1')"
v = IncrementalValue(restart=2)
assert v == 0 and v == 1 and v == 2 and v == 0
assert v.command() == "IncrementalValue(restart=2)"

= CyclicPattern charset 0

cs0 = b'AAAaAA0AABAAbAA1AACAAcAA2AADAAdAA3AAEAAeAA4AAFAAfAA5AAGAAgAA6AAHAAhAA7AAIAAiAA8AAJAAjAA9AAKAAkAALAAlAAMAAmAANAAnAAOAAoAAPAApAAQAAqAARAArAASAAsAATAAtAAUAAuAAVAAvAAWAAwAAXAAxAAYAAyAAZAAzAaaAa0AaBAabAa1AaCAacAa2AaDAadAa3AaEAaeAa4AaFAafAa5AaGAagAa6AaHAahAa7AaIAaiAa8AaJ'

p = Raw(load=CyclicPattern())
b = bytes(p)
if len(b):
if len(b) > len(cs0):
assert cs0 in b
else:
assert b in cs0 or b == cs0

p = Raw(load=CyclicPattern(5))
b = bytes(p)
assert len(b) == 5
assert b == b'AAAaA'

p = Raw(load=CyclicPattern(2, 3))
b = bytes(p)
print(b)
assert len(b) == 2
assert b == b'aA'

= CyclicPattern charset 1

cs1 = b'AAA%AAsAABAA$AAnAACAA-AA(AADAA;AA)AAEAAaAA0AAFAAbAA1AAGAAcAA2AAHAAdAA3AAIAAeAA4AAJAAfAA5AAKAAgAA6AALAAhAA7AAMAAiAA8AANAAjAA9AAOAAkAAPAAlAAQAAmAARAAoAASAApAATAAqAAUAArAAVAAtAAWAAuAAXAAvAAYAAwAAZAAxAAyAAzA%%A%sA%BA%$A%nA%CA%-A%(A%DA%;A%)A%EA%aA%0A%FA%bA%1A%GA%'

p = Raw(load=CyclicPattern(None, 0, 1))
b = bytes(p)
if len(b):
if len(b) > len(cs1):
assert cs1 in b
else:
assert b in cs1 or b == cs1

p = Raw(load=CyclicPattern(10, 0, 1))
b = bytes(p)
assert len(b) == 10
assert b == b'AAA%AAsAAB'

p = Raw(load=CyclicPattern(2, 8, 1))
b = bytes(p)
print(b)
assert len(b) == 2
assert b == b'AB'

= CyclicPattern charset 2

cs2 = b'AAAaAA0AA!AABAAbAA1AA"AACAAcAA2AA#AADAAdAA3AA$AAEAAeAA4AA%AAFAAfAA5AA&AAGAAgAA6AA\'AAHAAhAA7AA(AAIAAiAA8AA)AAJAAjAA9AA*AAKAAkAA+AALAAlAA,AAMAAmAA-AANAAnAA.AAOAAoAA/AAPAApAA:AAQAAqAA;AARAArAA<AASAAsAA=AATAAtAA>AAUAAuAA?AAVAAvAA@AAWAAwAA[AAXAAxAA\\AAYAAyAA]AAZAAzAA^AA_AA`AA{AA|AA}AA~AaaAa0Aa!AaBAabAa1Aa"Aa'

p = Raw(load=CyclicPattern(None, 0, 2))
b = bytes(p)
if len(b):
if len(b) > len(cs2):
assert cs2 in b
else:
assert b in cs2 or b == cs2

p = Raw(load=CyclicPattern(10, 0, 2))
b = bytes(p)
assert len(b) == 10
assert b == b'AAAaAA0AA!'

p = Raw(load=CyclicPattern(2, 8, 2))
b = bytes(p)
print(b)
assert len(b) == 2
assert b == b'A!'

= CyclicPattern command

p = Raw(load=CyclicPattern(2, 8, 2))
cmd = p.command()

assert "charset_type=2" in cmd
assert "start=8" in cmd
assert "size=2" in cmd

p = Raw(load=CyclicPattern(2))
cmd = p.command()

assert "charset_type" not in cmd
assert "start" not in cmd
assert "size=2" in cmd

p = Raw(load=CyclicPattern())
cmd = p.command()

assert "charset_type" not in cmd
assert "start" not in cmd
assert "size" not in cmd


0 comments on commit 9473f77

Please sign in to comment.