Skip to content

Commit bd1b291

Browse files
committed
Better logging, cleanup of HTTP_Server code, fix in ChannelBindings of Kerberos
1 parent cd17acb commit bd1b291

2 files changed

Lines changed: 59 additions & 42 deletions

File tree

scapy/layers/http.py

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,47 +1073,17 @@ def BEGIN(self):
10731073
self.authenticated = False
10741074
self.sspcontext = None
10751075

1076-
@ATMT.condition(BEGIN, prio=0)
1077-
def should_authenticate(self):
1076+
@ATMT.receive_condition(BEGIN, prio=1)
1077+
def should_authenticate(self, pkt):
10781078
if self.authmethod == HTTP_AUTH_MECHS.NONE.value:
1079-
raise self.SERVE()
1079+
raise self.SERVE(pkt)
10801080
else:
1081-
raise self.AUTH()
1081+
raise self.AUTH(pkt)
10821082

10831083
@ATMT.state()
1084-
def AUTH(self):
1085-
pass
1086-
1087-
@ATMT.state()
1088-
def AUTH_ERROR(self, proxy):
1089-
self.sspcontext = None
1090-
self._ask_authorization(proxy, self.authmethod)
1091-
self.vprint("AUTH ERROR")
1092-
1093-
@ATMT.condition(AUTH_ERROR)
1094-
def allow_reauth(self):
1095-
raise self.AUTH()
1096-
1097-
def _ask_authorization(self, proxy, data):
1098-
if proxy:
1099-
self.send(
1100-
HTTPResponse(
1101-
Status_Code=b"407",
1102-
Reason_Phrase=b"Proxy Authentication Required",
1103-
Proxy_Authenticate=data,
1104-
)
1105-
)
1106-
else:
1107-
self.send(
1108-
HTTPResponse(
1109-
Status_Code=b"401",
1110-
Reason_Phrase=b"Unauthorized",
1111-
WWW_Authenticate=data,
1112-
)
1113-
)
1114-
1115-
@ATMT.receive_condition(AUTH, prio=1)
1116-
def received_unauthenticated(self, pkt):
1084+
def AUTH(self, pkt=None):
1085+
if pkt is None:
1086+
return
11171087
if HTTPRequest in pkt:
11181088
self.vprint(pkt.summary())
11191089
if pkt.Method == b"CONNECT":
@@ -1137,10 +1107,12 @@ def received_unauthenticated(self, pkt):
11371107
# Parse authorization
11381108
method, data = authorization.split(b" ", 1)
11391109
if plain_str(method) != self.authmethod:
1110+
self.debug(3, "Bad auth method.")
11401111
raise self.AUTH_ERROR(proxy)
11411112
try:
11421113
data = base64.b64decode(data)
11431114
except Exception:
1115+
self.debug(3, "Couldn't unpack base64 of auth.")
11441116
raise self.AUTH_ERROR(proxy)
11451117
# Now process the authorization
11461118
if not self.basic:
@@ -1149,6 +1121,7 @@ def received_unauthenticated(self, pkt):
11491121
except Exception:
11501122
self.sspcontext = None
11511123
self._ask_authorization(proxy, self.authmethod)
1124+
self.debug(3, "Couldn't unpack GSSAPI_BLOB of auth.")
11521125
raise self.AUTH_ERROR(proxy)
11531126
# And call the SSP
11541127
self.sspcontext, tok, status = self.ssp.GSS_Accept_sec_context(
@@ -1164,9 +1137,11 @@ def received_unauthenticated(self, pkt):
11641137
)
11651138
tok, status = None, GSS_S_COMPLETE
11661139
except StopIteration:
1140+
self.debug(3, "Basic authentication failed with 'unknown user'.")
11671141
tok, status = None, GSS_S_FAILURE
11681142
# Send answer
11691143
if status not in [GSS_S_COMPLETE, GSS_S_CONTINUE_NEEDED]:
1144+
self.debug(3, "Authentication failed.")
11701145
raise self.AUTH_ERROR(proxy)
11711146
elif status == GSS_S_CONTINUE_NEEDED:
11721147
data = self.authmethod.encode()
@@ -1180,6 +1155,38 @@ def received_unauthenticated(self, pkt):
11801155
self.vprint("AUTH OK")
11811156
raise self.SERVE(pkt)
11821157

1158+
@ATMT.state()
1159+
def AUTH_ERROR(self, proxy):
1160+
self.sspcontext = None
1161+
self._ask_authorization(proxy, self.authmethod)
1162+
self.vprint("AUTH ERROR")
1163+
1164+
@ATMT.condition(AUTH_ERROR)
1165+
def allow_reauth(self):
1166+
raise self.AUTH()
1167+
1168+
def _ask_authorization(self, proxy, data):
1169+
if proxy:
1170+
self.send(
1171+
HTTPResponse(
1172+
Status_Code=b"407",
1173+
Reason_Phrase=b"Proxy Authentication Required",
1174+
Proxy_Authenticate=data,
1175+
)
1176+
)
1177+
else:
1178+
self.send(
1179+
HTTPResponse(
1180+
Status_Code=b"401",
1181+
Reason_Phrase=b"Unauthorized",
1182+
WWW_Authenticate=data,
1183+
)
1184+
)
1185+
1186+
@ATMT.receive_condition(AUTH, prio=1)
1187+
def received_unauthenticated(self, pkt):
1188+
raise self.AUTH(pkt)
1189+
11831190
@ATMT.eof(AUTH)
11841191
def auth_eof(self):
11851192
raise self.CLOSED()

scapy/layers/kerberos.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,11 +2108,21 @@ class KRB_GSS_EXT(Packet):
21082108
class KRB_AuthenticatorChecksum(Packet):
21092109
fields_desc = [
21102110
FieldLenField("Lgth", None, length_of="Bnd", fmt="<I"),
2111-
PacketLenField(
2112-
"Bnd",
2113-
GssChannelBindings(),
2114-
GssChannelBindings,
2115-
length_from=lambda pkt: pkt.Lgth,
2111+
MultipleTypeField(
2112+
[
2113+
(
2114+
# If using a MD5 hash.
2115+
XStrFixedLenField("Bnd", b"", length=16),
2116+
lambda pkt: pkt.Lgth == 16,
2117+
),
2118+
],
2119+
# Default to using the gss_channel_bindings_struct
2120+
PacketLenField(
2121+
"Bnd",
2122+
GssChannelBindings(),
2123+
GssChannelBindings,
2124+
length_from=lambda pkt: pkt.Lgth,
2125+
),
21162126
),
21172127
FlagsField(
21182128
"Flags",

0 commit comments

Comments
 (0)