/// <summary> /// constructor that initializes the sniffer form /// </summary> public RawSockSnifferForm(TcpClient client, AesCrypto aes) { InitializeComponent(); pA = new PacketAnalyzer(); this.client = client; this.aes = aes; currentLocalPackets = new List <string[]>(); currentLocalPacketsTag = new List <byte[]>(); // Read data from the client async data = new byte[client.ReceiveBufferSize]; // BeginRead will begin async read from the NetworkStream // This allows the server to remain responsive and continue accepting new connections from other clients // When reading complete control will be transfered to the ReviveMessage() function. client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(client.ReceiveBufferSize), ReceiveMessage, null); }
/// <summary> /// recursive method that recieves a message from the server and handles it according to the request or response number /// </summary> /// <param name="ar"></param> private void ReceiveMessage(IAsyncResult ar) { try { int bytesRead; lock (client.GetStream()) { // call EndRead to handle the end of an async read and read the data from the server bytesRead = client.GetStream().EndRead(ar); } if (rsa.ServerPublicKey == null) // if RSA object is missing server's public key { rsa.SetServerPublicKey(ByteConverter.GetString(data).Split('#')[1]); // creates an Aes instance for symmetric encryption and sends the key to the server aes = new AesCrypto(); byte[] bytesArray = AesKeyAndIVBytesToSend(aes.GetKey(), aes.GetIV()); SendRsaEncryptedMessage(bytesArray); } else { byte[] arrived = new byte[bytesRead]; Array.Copy(data, arrived, bytesRead); string messageReceived; if (aes == null) // if AES object wasn't initialize yet { messageReceived = ByteConverter.GetString(arrived); } else { messageReceived = aes.DecryptStringFromBytes(arrived, aes.GetKey(), aes.GetIV()); } Debug.WriteLine("received: " + messageReceived); string[] arrayReceived = messageReceived.Split('#'); int requestNumber = Convert.ToInt32(arrayReceived[0]); string text = arrayReceived[1]; if (requestNumber == registerStatusResponse) { if (text.Equals("ok")) { name = textBoxName.Text; OpenSnifferForm(); return; } else if (text.Equals("not ok")) { MessageBox.Show("Wrong username or password\nPlease try again", "CAPCKET login error"); } } else if (requestNumber == EmailRequest) { string answer = CreateInteractionForm("Please enter the code that was sent to your email address:", "CAPCKET email verification"); string textToSend = text + "/" + answer; SendAesEncryptedMessage(CodeResponse + "#" + textToSend + "#" + textToSend.Length); } else if (requestNumber == QuestionResponse) { string[] textArray = text.Split('/'); if (textArray.Length > 1) { MessageBox.Show("Wrong answer\nPlease try again", "CAPCKET login error"); } string answer = CreateInteractionForm(textArray[0], "CAPCKET changing password"); SendAesEncryptedMessage(AnswerResponse + "#" + answer + "#" + answer.Length); } else if (requestNumber == PasswordRequest) { string password = CreateInteractionForm("Please enter a new password:"******"CAPCKET changing password"); if (!IsPasswordValid(password)) //checking password validity { //password isn't valid. MessageBox.Show("The pasword isn't valid. Please try again.\nIt should be 6-8 charcters and contain both digits and letters.", "CAPCKET changing password"); } else { // hash password string hashpassword = HashString(password); Debug.WriteLine("hash length: " + hashpassword.Length); SendAesEncryptedMessage(PasswordResponse + "#" + hashpassword + "#" + password.Length); } } else if (requestNumber == PasswordChangeStatusResponse) { if (text.Equals("ok")) { MessageBox.Show("Password changed successfully", "CAPCKET login · changing password"); } else { string password = CreateInteractionForm("Please try again and enter a new password:"******"CAPCKET login error · changing password"); SendAesEncryptedMessage(PasswordResponse + "#" + password + "#" + password.Length); } } else if (requestNumber == RSAPublicKeyTransfer) { rsa.SetServerPublicKey(text); } } // continue reading client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(client.ReceiveBufferSize), ReceiveMessage, null); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }