def test_DE04(self): data = Datastore("DB") data.validatesInput = False data.implementsPOLP = False ThreatObj = Threat( next(item for item in threats_json if item["SID"] == "DE04")) self.assertTrue(ThreatObj.apply(data))
def test_multilevel_dfd(self): random.seed(0) dir_path = os.path.dirname(os.path.realpath(__file__)) install_path = os.path.dirname(os.path.realpath(pytm.__file__)) with open(os.path.join(dir_path, "dfd_level0.txt")) as x: level_0 = ( x.read().strip().replace("INSTALL_PATH", os.path.dirname(install_path)) ) with open(os.path.join(dir_path, "dfd_level1.txt")) as x: level_1 = ( x.read().strip().replace("INSTALL_PATH", os.path.dirname(install_path)) ) TM.reset() tm = TM("my test tm", description="aaa") tm.isOrdered = False internet = Boundary("Internet") server_db = Boundary("Server/DB") user = Actor("User", inBoundary=internet, levels=0) web = Server("Web Server") db = Datastore("SQL Database", inBoundary=server_db) Dataflow(user, web, "User enters comments (*)", note="bbb") Dataflow(web, db, "Insert query with comments", note="ccc") Dataflow(db, web, "Retrieve comments") Dataflow(web, user, "Show comments (*)") self.assertTrue(tm.check()) output = tm.dfd(levels={0}) with open(os.path.join(dir_path, "0.txt"), "w") as x: x.write(output) self.assertEqual(output, level_0) TM.reset() tm = TM("my test tm", description="aaa") tm.isOrdered = False internet = Boundary("Internet") server_db = Boundary("Server/DB") user = Actor("User", inBoundary=internet, levels=1) web = Server("Web Server") db = Datastore("SQL Database", inBoundary=server_db) Dataflow(user, web, "User enters comments (*)", note="bbb") Dataflow(web, db, "Insert query with comments", note="ccc") Dataflow(db, web, "Retrieve comments") Dataflow(web, user, "Show comments (*)") self.assertTrue(tm.check()) output = tm.dfd(levels={1}) with open(os.path.join(dir_path, "1.txt"), "w") as x: x.write(output) self.maxDiff = None self.assertEqual(output, level_1)
def test_AC01(self): web = Server("Web Server") process1 = Process("Process1") db = Datastore("DB") web.hasAccessControl = False web.authorizesSource = True process1.hasAccessControl = False process1.authorizesSource = False db.hasAccessControl = False db.authorizesSource = False ThreatObj = Threat(next(item for item in threats_json if item["SID"] == "AC01")) self.assertTrue(ThreatObj.apply(process1)) self.assertTrue(ThreatObj.apply(web)) self.assertTrue(ThreatObj.apply(db))
def test_DO02(self): process1 = Process("Process1") lambda1 = Lambda("Lambda1") web = Server("Web Server") db = Datastore("DB") process1.handlesResourceConsumption = False lambda1.handlesResourceConsumption = False web.handlesResourceConsumption = False db.handlesResourceConsumption = False threat = threats["DO02"] self.assertTrue(threat.apply(process1)) self.assertTrue(threat.apply(lambda1)) self.assertTrue(threat.apply(web)) self.assertTrue(threat.apply(db))
def test_DO02(self): process1 = Process("Process1") lambda1 = Lambda("Lambda1") web = Server("Web Server") db = Datastore("DB") process1.handlesResourceConsumption = False lambda1.handlesResourceConsumption = False web.handlesResourceConsumption = False db.handlesResourceConsumption = False ThreatObj = Threat(next(item for item in threats_json if item["SID"] == "DO02")) self.assertTrue(ThreatObj.apply(process1)) self.assertTrue(ThreatObj.apply(lambda1)) self.assertTrue(ThreatObj.apply(web)) self.assertTrue(ThreatObj.apply(db))
def test_AC01(self): web = Server("Web Server") process1 = Process("Process1") db = Datastore("DB") web.hasAccessControl = False web.authorizesSource = True process1.hasAccessControl = False process1.authorizesSource = False db.hasAccessControl = False db.authorizesSource = False threat = threats["AC01"] self.assertTrue(threat.apply(process1)) self.assertTrue(threat.apply(web)) self.assertTrue(threat.apply(db))
def test_dfd(self): dir_path = os.path.dirname(os.path.realpath(__file__)) with open(os.path.join(dir_path, 'dfd.dot')) as x: expected = x.read().strip() random.seed(0) TM.reset() tm = TM("my test tm", description="aaa") internet = Boundary("Internet") server_db = Boundary("Server/DB") user = Actor("User", inBoundary=internet) web = Server("Web Server") db = Datastore("SQL Database", inBoundary=server_db) Dataflow(user, web, "User enters comments (*)") Dataflow(web, db, "Insert query with comments") Dataflow(db, web, "Retrieve comments") Dataflow(web, user, "Show comments (*)") tm.check() with captured_output() as (out, err): tm.dfd() output = out.getvalue().strip() self.maxDiff = None self.assertEqual(output, expected)
def test_report(self): random.seed(0) dir_path = os.path.dirname(os.path.realpath(__file__)) with open(os.path.join(dir_path, "output.md")) as x: expected = x.read().strip() TM.reset() tm = TM("my test tm", description="aaa", threatsFile="pytm/threatlib/threats.json") tm.isOrdered = True internet = Boundary("Internet") server_db = Boundary("Server/DB") user = Actor("User", inBoundary=internet) web = Server("Web Server") func = Lambda("Lambda func") worker = Process("Task queue worker") db = Datastore("SQL Database", inBoundary=server_db) Dataflow(user, web, "User enters comments (*)", note="bbb", data="auth cookie") Dataflow(web, db, "Insert query with comments", note="ccc") Dataflow(web, func, "Call func") Dataflow(db, web, "Retrieve comments") Dataflow(web, user, "Show comments (*)") Dataflow(worker, db, "Query for tasks") self.assertTrue(tm.check()) output = tm.report("docs/template.md") self.maxDiff = None self.assertEqual(output.strip(), expected.strip())
def test_seq_unused(self): random.seed(0) dir_path = os.path.dirname(os.path.realpath(__file__)) with open(os.path.join(dir_path, "seq_unused.plantuml")) as x: expected = x.read().strip() TM.reset() tm = TM("my test tm", description="aaa", ignoreUnused=True) internet = Boundary("Internet") server_db = Boundary("Server/DB") user = Actor("User", inBoundary=internet) web = Server("Web Server") db = Datastore("SQL Database", inBoundary=server_db) Lambda("Unused Lambda") Dataflow(user, web, "User enters comments (*)", note="bbb") Dataflow(web, db, "Insert query with comments", note="ccc") Dataflow(db, web, "Retrieve comments") Dataflow(web, user, "Show comments (*)") self.assertTrue(tm.check()) output = tm.seq() self.maxDiff = None self.assertEqual(output, expected)
def test_dfd(self): dir_path = os.path.dirname(os.path.realpath(__file__)) with open(os.path.join(dir_path, "dfd.dot")) as x: expected = x.read().strip() random.seed(0) TM.reset() tm = TM("my test tm", description="aaa") internet = Boundary("Internet") net = Boundary("Company net") dmz = Boundary("dmz", inBoundary=net) backend = Boundary("backend", inBoundary=net) user = Actor("User", inBoundary=internet) gw = Server("Gateway", inBoundary=dmz) web = Server("Web Server", inBoundary=backend) db = Datastore("SQL Database", inBoundary=backend) Dataflow(user, gw, "User enters comments (*)") Dataflow(gw, web, "Request") Dataflow(web, db, "Insert query with comments") Dataflow(db, web, "Retrieve comments") Dataflow(web, gw, "Response") Dataflow(gw, user, "Show comments (*)") self.assertTrue(tm.check()) output = tm.dfd() self.maxDiff = None self.assertEqual(output, expected)
def test_dfd_duplicates_raise(self): random.seed(0) TM.reset() tm = TM("my test tm", description="aaa", onDuplicates=Action.RESTRICT) internet = Boundary("Internet") server_db = Boundary("Server/DB") user = Actor("User", inBoundary=internet) web = Server("Web Server") db = Datastore("SQL Database", inBoundary=server_db) Dataflow(user, web, "User enters comments (*)") Dataflow(user, web, "User views comments") Dataflow(web, db, "Insert query with comments") Dataflow(web, db, "Select query") Dataflow(db, web, "Retrieve comments") Dataflow(web, user, "Show comments (*)") e = re.escape( "Duplicate Dataflow found between Actor(User) " "and Server(Web Server): Dataflow(User enters comments (*)) " "is same as Dataflow(User views comments)" ) with self.assertRaisesRegex(ValueError, e): tm.check()
def test_resolve(self): random.seed(0) TM.reset() tm = TM("my test tm", description="aaa") internet = Boundary("Internet") server_db = Boundary("Server/DB") user = Actor("User", inBoundary=internet, inScope=False) web = Server("Web Server") db = Datastore("SQL Database", inBoundary=server_db) req = Dataflow(user, web, "User enters comments (*)") query = Dataflow(web, db, "Insert query with comments") results = Dataflow(db, web, "Retrieve comments") resp = Dataflow(web, user, "Show comments (*)") TM._BagOfThreats = [ Threat(SID=klass, target=klass) for klass in ["Actor", "Server", "Datastore", "Dataflow"] ] tm.resolve() self.maxDiff = None self.assertListEqual([f.id for f in tm.findings], [ 'Server', 'Datastore', 'Dataflow', 'Dataflow', 'Dataflow', 'Dataflow' ]) self.assertListEqual([f.id for f in user.findings], []) self.assertListEqual([f.id for f in web.findings], ["Server"]) self.assertListEqual([f.id for f in db.findings], ["Datastore"]) self.assertListEqual([f.id for f in req.findings], ["Dataflow"]) self.assertListEqual([f.id for f in query.findings], ["Dataflow"]) self.assertListEqual([f.id for f in results.findings], ["Dataflow"]) self.assertListEqual([f.id for f in resp.findings], ["Dataflow"])
def test_DR01(self): web = Server("Web Server") db = Datastore("Database") insert = Dataflow(web, db, "Insert query") insert.data = Data("ssn", isPII=True, isStored=True) insert.isEncrypted = False threat = threats["DR01"] self.assertTrue(threat.apply(insert))
def test_CR05(self): web = Server("Web Server") db = Datastore("db") web.usesEncryptionAlgorithm != "RSA" web.usesEncryptionAlgorithm != "AES" db.usesEncryptionAlgorithm != "RSA" db.usesEncryptionAlgorithm != "AES" threat = threats["CR05"] self.assertTrue(threat.apply(web)) self.assertTrue(threat.apply(db))
def test_CR05(self): web = Server("Web Server") db = Datastore("db") web.usesEncryptionAlgorithm != 'RSA' web.usesEncryptionAlgorithm != 'AES' db.usesEncryptionAlgorithm != 'RSA' db.usesEncryptionAlgorithm != 'AES' ThreatObj = Threat(next(item for item in threats_json if item["SID"] == "CR05")) self.assertTrue(ThreatObj.apply(web)) self.assertTrue(ThreatObj.apply(db))
def create_dataflow( source=Classification.RESTRICTED, sink=Classification.RESTRICTED, dataflow=Classification.RESTRICTED, data=Classification.RESTRICTED, define_data=True, ): source_ = Server("Source", maxClassification=source) sink_ = Datastore("Sink", maxClassification=sink) flow_ = Dataflow(source_, sink_, "Flow", maxClassification=dataflow) if define_data: flow_.data = Data("Data", classification=data) return flow_
def test_overrides(self): random.seed(0) TM.reset() tm = TM("my test tm", description="aaa") internet = Boundary("Internet") server_db = Boundary("Server/DB") user = Actor("User", inBoundary=internet, inScope=False) web = Server( "Web Server", overrides=[ Finding(threat_id="Server", response="mitigated by adding TLS"), ], ) db = Datastore( "SQL Database", inBoundary=server_db, overrides=[ Finding( threat_id="Datastore", response="accepted since inside the trust boundary", ), ], ) req = Dataflow(user, web, "User enters comments (*)") query = Dataflow(web, db, "Insert query with comments") results = Dataflow(db, web, "Retrieve comments") resp = Dataflow(web, user, "Show comments (*)") TM._threats = [ Threat(SID="Server", target="Server", condition="False"), Threat(SID="Datastore", target="Datastore"), ] tm.resolve() self.maxDiff = None self.assertEqual( [f.threat_id for f in tm.findings], ["Server", "Datastore"], ) self.assertEqual([f.response for f in web.findings], ["mitigated by adding TLS"]) self.assertEqual( [f.response for f in db.findings], ["accepted since inside the trust boundary"], )
def test_json_dumps(self): random.seed(0) dir_path = os.path.dirname(os.path.realpath(__file__)) with open(os.path.join(dir_path, "output.json")) as x: expected = x.read().strip() TM.reset() tm = TM("my test tm", description="aaa", threatsFile="pytm/threatlib/threats.json") tm.isOrdered = True internet = Boundary("Internet") server_db = Boundary("Server/DB") user = Actor("User", inBoundary=internet) web = Server("Web Server") func = Lambda("Lambda func") worker = Process("Task queue worker") db = Datastore("SQL Database", inBoundary=server_db) cookie = Data( name="auth cookie", description="auth cookie description", classification=Classification.PUBLIC, ) Dataflow(user, web, "User enters comments (*)", note="bbb", data=cookie) Dataflow(web, db, "Insert query with comments", note="ccc") Dataflow(web, func, "Call func") Dataflow(db, web, "Retrieve comments") Dataflow(web, user, "Show comments (*)") Dataflow(worker, db, "Query for tasks") self.assertTrue(tm.check()) output = json.dumps(tm, default=to_serializable, sort_keys=True, indent=4) with open(os.path.join(dir_path, "output_current.json"), "w") as x: x.write(output) self.maxDiff = None self.assertEqual(output, expected)
server_db.levels = [2] vpc = Boundary("AWS VPC") user = Actor("User") user.inBoundary = internet user.levels = [2] web = Server("Web Server") web.OS = "Ubuntu" web.isHardened = True web.sanitizesInput = False web.encodesOutput = True web.authorizesSource = False web.sourceFiles = ["pytm/json.py", "docs/template.md"] db = Datastore("SQL Database") db.OS = "CentOS" db.isHardened = False db.inBoundary = server_db db.isSQL = True db.inScope = True db.maxClassification = Classification.RESTRICTED db.levels = [2] secretDb = Datastore("Real Identity Database") secretDb.OS = "CentOS" secretDb.sourceFiles = ["pytm/pytm.py"] secretDb.isHardened = True secretDb.inBoundary = server_db secretDb.isSQL = True secretDb.inScope = True
apigee = Element("Apigee") apigee.inBoundary = internet apigee.isHardened = True apigee = Element("Apigee") apigee.inBoundary = internet apigee.isHardened = True server = Server("Apps Server") server.inBoundary = apps_vpc server.isHardened = True server.hasAccessControl = True server.encodesOutput = True db = Datastore("MySQL DB") db.isHardened = True db.hasAccessControl = True db.inBoundary = apps_vpc db.inBoundary = rds_boundary db.isSQL = True db.inScope = True db.onAWS = True db.isShared = True db.storesSensitiveData = False redis = Datastore("Redis") redis.isHardened = True redis.inBoundary = apps_vpc redis.inBoundary = cache_boundary redis.isSQL = False
def test_AC02(self): db = Datastore("DB") db.isShared = True threat = threats["AC02"] self.assertTrue(threat.apply(db))
Web_DB = Boundary("Web/DB") VPC = Boundary("AWS VPC") user = Actor("User") user.inBoundary = User_Web web = Server("Web Server") web.OS = "CloudOS" web.isHardened = True my_lambda = Lambda("cleanDBevery6hours") my_lambda.hasAccessControl = True my_lambda.inBoundary = Web_DB #my_lambda.inBoundary = VPC # TODO: need multiple boundaries capability for these situations db = Datastore("SQL Database") db.OS = "CentOS" db.isHardened = False db.inBoundary = Web_DB db.isSQL = True db.inScope = False my_lambda_to_db = Dataflow(my_lambda, db, "(λ)Periodically cleans DB") my_lambda_to_db.protocol = "SQL" my_lambda_to_db.dstPort = 3306 user_to_web = Dataflow(user, web, "User enters comments (*)") user_to_web.protocol = "HTTP" user_to_web.dstPort = 80 user_to_web.data = 'Comments in HTML or Markdown' user_to_web.order = 1
internet = Boundary("Internet") server_db = Boundary("Server/DB") vpc = Boundary("AWS VPC") user = Actor("User") user.inBoundary = internet web = Server("Web Server") web.OS = "Ubuntu" web.isHardened = True web.sanitizesInput = False web.encodesOutput = True web.authorizesSource = False db = Datastore("SQL Database") db.OS = "CentOS" db.isHardened = False db.inBoundary = server_db db.isSQL = True db.inScope = True db.maxClassification = Classification.RESTRICTED secretDb = Datastore("Real Identity Database") secretDb.OS = "CentOS" secretDb.isHardened = True secretDb.inBoundary = server_db secretDb.isSQL = True secretDb.inScope = True secretDb.storesPII = True secretDb.maxClassification = Classification.TOP_SECRET
internet = Boundary("Internet") server_db = Boundary("Server/DB") vpc = Boundary("AWS VPC") user = Actor("User") user.inBoundary = internet web = Server("Web Server") web.OS = "Ubuntu" web.isHardened = True web.sanitizesInput = False web.encodesOutput = True web.authorizesSource = False db = Datastore("SQL Database") db.OS = "CentOS" db.isHardened = False db.inBoundary = server_db db.isSQL = True db.inScope = True my_lambda = Lambda("AWS Lambda") my_lambda.hasAccessControl = True my_lambda.inBoundary = vpc user_to_web = Dataflow(user, web, "User enters comments (*)") user_to_web.protocol = "HTTP" user_to_web.dstPort = 80 user_to_web.data = 'Comments in HTML or Markdown' user_to_web.note = "This is a simple web app\nthat stores and retrieves user comments."
def test_DE04(self): data = Datastore("DB") data.validatesInput = False data.implementsPOLP = False threat = threats["DE04"] self.assertTrue(threat.apply(data))
def test_AC02(self): db = Datastore("DB") db.isShared = True ThreatObj = Threat( next(item for item in threats_json if item["SID"] == "AC02")) self.assertTrue(ThreatObj.apply(db))