def testMACValidation(): """ Tests whether the server properly rejects messages when their MAC is modified. """ print("Testing validation of individual MAC bits...") failBits = [] for maskBit in range(0, 96): rejected = False try: # formulate a bit mask based on the current mask bit index mask = bytearray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) maskIndex = int((maskBit - (maskBit % 8)) / 8) mask[maskIndex] = (0x80 >> (maskBit % 8)) if args.verbose: maskBinString = ''.join(format(x, 'b').zfill(8) for x in mask) print("\tTesting bit %d, mask: %s" % (maskBit, maskBinString)) else: print("+", end="") # connect to the server and do a handshake sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((args.host, args.port)) tls = TLSConnection(sock) # assign mask as tweak tls.macTweak = bytearray(mask) tls.handshakeClientCert() # send a packet tls.send("GET / HTTP/1.0\n\n\n") # try to read some data back data = tls.read() except (TLSRemoteAlert, socket.error): rejected = True if args.verbose: print("\tBit %d rejected correctly!" % maskBit) except (TLSAbruptCloseError, socket.error): rejected = True if args.verbose: print("\tBit %d rejected correctly!" % maskBit) if not rejected: failBits.append(maskBit) if not args.verbose: print("") if len(failBits) > 0: macValidationIssue = getIssueTemplate("MAC_VALIDATION_ERROR") macValidationIssue.findings = ', '.join(str(b) for b in failBits) report.addIssue(macValidationIssue) print("The following modified MAC bits were incorrectly accepted: ", end='') print(', '.join(str(b) for b in failBits)) else: print("All modified MAC bits were correctly rejected.")
msg.RID = '' msg.STIME = 0 msgType = 0 msgType = setBit(msgType, 0) # Identity msgType = setBit(msgType, 1) # Authenticate msg.TYPE = msgType msg.BODY = '{"token":"foo"}' data = Packet.Pack(msg) connection.write(data) ## P2P message msg.RID = 'a' msg.TYPE = 0 msg.BODY = 'x' data = Packet.Pack(msg) data = connection.read() # You must implement your own tcp receiver msg = Packet.UnPack(data) # Must catch error print msg raw_input('Press enter to exit') connection.close()