Skip to content

Commit

Permalink
Add calculate valid bit_length for Integer fields
Browse files Browse the repository at this point in the history
Add Header Field parameters by rfc2616. Example:
Content-Type: text/html; charset=utf-8
  • Loading branch information
GH0st3rs committed May 13, 2019
1 parent bb72d74 commit 35525dd
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions katnip/legos/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,72 @@ def _keyname(txt):
return '%s_key' % txt


def bit_length(num):
for x in range(1, 100):
if num.bit_length() > (2 << x):
continue
else:
return 2 << x


class CustomHeaderField(Container):
def __init__(self, key, value, end=False, fuzzable_key=False, fuzzable_value=True):
def __init__(self, key, value, params={}, end=False, fuzzable_key=False, fuzzable_value=True, name=None):
fields = [
String(name=_keyname(key), value=key, fuzzable=fuzzable_key),
Static(': '),
Container(name=_valuename(key), fields=value, fuzzable=fuzzable_value),
Static('\r\n')
]
# Append Field value params, splitted by '; '
for k, v in params.items():
if isinstance(k, bytes):
k = k.decode()
if isinstance(v, bytes):
v = v.decode()
fields += [
Static('; '),
String(value=k, name='%s_param_%s' % (_valuename(key), k)),
Static('="'),
String(value=v, name='%s_param_%s' % (_valuename(key), v)),
Static('"')
]
# Append end of line for current Field
fields.append(Static(b'\r\n'))
if end:
fields.append(Static('\r\n'))
super(CustomHeaderField, self).__init__(name=key, fields=fields, fuzzable=fuzzable_value)
fields.append(Static(b'\r\n'))
name = key if name is None else name
super(CustomHeaderField, self).__init__(name=name, fields=fields, fuzzable=fuzzable_value)


class TextField(CustomHeaderField):
def __init__(self, key, value, end=False, fuzzable_key=False, fuzzable_value=True):
def __init__(self, key, value, params={}, end=False, fuzzable_key=False, fuzzable_value=True, name=None):
value_field = String(name="value", value=value)
super(TextField, self).__init__(key, value_field, end, fuzzable_key, fuzzable_value)
super(TextField, self).__init__(key=key, value=value_field, params=params,
end=end, fuzzable_key=fuzzable_key,
fuzzable_value=fuzzable_value, name=name)


class IntField(CustomHeaderField):
def __init__(self, key, value, end=False, fuzzable_key=False, fuzzable_value=True):
def __init__(self, key, value, params={}, end=False, fuzzable_key=False, fuzzable_value=True, name=None):
value_field = DecimalNumber(
name="value",
value=value,
num_bits=32,
num_bits=bit_length(value),
signed=True
)
super(IntField, self).__init__(key, value_field, end, fuzzable_key, fuzzable_value)
super(IntField, self).__init__(key=key, value=value_field, params=params,
end=end, fuzzable_key=fuzzable_key,
fuzzable_value=fuzzable_value, name=name)


class ContentLengthField(CustomHeaderField):
def __init__(self, value, sized_field, end=False, fuzzable_key=False, fuzzable_value=True):
value_field = OneOf(fields=[
Size(sized_field=sized_field, length=32, encoder=ENC_INT_DEC),
DecimalNumber(name="value", value=value, num_bits=32, signed=True),
Size(sized_field=sized_field, length=bit_length(value), encoder=ENC_INT_DEC),
DecimalNumber(name="value", value=value, num_bits=bit_length(value), signed=True),
])
super(ContentLengthField, self).__init__('Content-Length', value_field, end, fuzzable_key, fuzzable_value)
super(ContentLengthField, self).__init__(key='Content-Length', value=value_field,
end=end, fuzzable_key=fuzzable_key,
fuzzable_value=fuzzable_value)


class AuthorizationField(CustomHeaderField):
Expand All @@ -61,7 +90,9 @@ def __init__(self, key, username, password, end=False, delim=':', fuzz_username=
String(name='password', value=password, fuzzable=fuzz_password),
], encoder=ENC_BITS_BASE64)
]
super(AuthorizationField, self).__init__(key, value_field, end, fuzzable_key, fuzzable)
super(AuthorizationField, self).__init__(key=key, value=value_field, params={},
end=end, fuzzable_key=fuzzable_key,
fuzzable_value=fuzzable)


class HttpRequestLine(Container):
Expand Down

0 comments on commit 35525dd

Please sign in to comment.