mirror of
https://github.com/Noratrieb/cluelessh.git
synced 2026-01-15 00:45:06 +01:00
more
This commit is contained in:
parent
1adf798c5d
commit
ed30d5b4dc
8 changed files with 75 additions and 87 deletions
|
|
@ -16,6 +16,7 @@ pub(crate) struct Session {
|
|||
pub(crate) trait Decryptor: Send + Sync + 'static {
|
||||
fn decrypt_len(&mut self, bytes: &mut [u8; 4], packet_number: u64);
|
||||
fn decrypt_packet(&mut self, raw_packet: RawPacket, packet_number: u64) -> Result<Packet>;
|
||||
fn additional_mac_len(&self) -> usize;
|
||||
fn rekey(&mut self, h: [u8; 32], k: [u8; 32]) -> Result<(), ()>;
|
||||
}
|
||||
|
||||
|
|
@ -25,6 +26,9 @@ impl Decryptor for Plaintext {
|
|||
fn decrypt_packet(&mut self, raw: RawPacket, _: u64) -> Result<Packet> {
|
||||
Packet::from_raw(&raw.rest())
|
||||
}
|
||||
fn additional_mac_len(&self) -> usize {
|
||||
0
|
||||
}
|
||||
fn rekey(&mut self, _: [u8; 32], _: [u8; 32]) -> Result<(), ()> {
|
||||
Err(())
|
||||
}
|
||||
|
|
@ -65,6 +69,10 @@ impl Decryptor for Session {
|
|||
.decrypt_packet(bytes, packet_number)
|
||||
}
|
||||
|
||||
fn additional_mac_len(&self) -> usize {
|
||||
poly1305::BLOCK_SIZE
|
||||
}
|
||||
|
||||
fn rekey(&mut self, h: [u8; 32], k: [u8; 32]) -> Result<(), ()> {
|
||||
*self = Self::from_keys(self.session_id, h, k);
|
||||
Ok(())
|
||||
|
|
@ -120,53 +128,33 @@ type SshChaCha20 = chacha20::ChaCha20Legacy;
|
|||
|
||||
struct SshChaCha20Poly1305 {
|
||||
header_key: chacha20::Key,
|
||||
main_key2: chacha20::Key,
|
||||
main_key:
|
||||
chacha20poly1305::ChaChaPoly1305<chacha20::ChaCha20Legacy, chacha20::cipher::typenum::U8>,
|
||||
main_key: chacha20::Key,
|
||||
}
|
||||
|
||||
impl SshChaCha20Poly1305 {
|
||||
fn new(key: [u8; 64]) -> Self {
|
||||
Self {
|
||||
main_key2: <[u8; 32]>::try_from(&key[..32]).unwrap().into(),
|
||||
main_key: chacha20poly1305::ChaChaPoly1305::new(
|
||||
&<[u8; 32]>::try_from(&key[..32]).unwrap().into(),
|
||||
),
|
||||
main_key: <[u8; 32]>::try_from(&key[..32]).unwrap().into(),
|
||||
header_key: <[u8; 32]>::try_from(&key[32..]).unwrap().into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn decrypt_len(&self, bytes: &mut [u8], packet_number: u64) {
|
||||
eprintln!("encrypted len: {:x?}", &bytes);
|
||||
// <https://github.com/openssh/openssh-portable/blob/1ec0a64c5dc57b8a2053a93b5ef0d02ff8598e5c/PROTOCOL.chacha20poly1305>
|
||||
let mut cipher = SshChaCha20::new(&self.header_key, &packet_number.to_be_bytes().into());
|
||||
cipher.apply_keystream(bytes);
|
||||
}
|
||||
|
||||
fn decrypt_packet(&mut self, bytes: RawPacket, packet_number: u64) -> Result<Packet> {
|
||||
fn decrypt_packet(&mut self, mut bytes: RawPacket, packet_number: u64) -> Result<Packet> {
|
||||
// <https://github.com/openssh/openssh-portable/blob/1ec0a64c5dc57b8a2053a93b5ef0d02ff8598e5c/PROTOCOL.chacha20poly1305>
|
||||
|
||||
//let mut aead_payload = bytes.into_full_packet();
|
||||
//let mut associated_data = [0; 4];
|
||||
//associated_data.copy_from_slice(&aead_payload[0..4]);
|
||||
//aead_payload.splice(0..4, []);
|
||||
|
||||
//chacha20poly1305::AeadInPlace::decrypt_in_place(
|
||||
// &self.main_key,
|
||||
// &packet_number.to_be_bytes().into(),
|
||||
// &associated_data,
|
||||
// &mut aead_payload,
|
||||
//)
|
||||
//.map_err(|err| crate::client_error!("failed to decrypt invalid poly1305 MAC: {err}"))?;
|
||||
|
||||
let mut cipher = <chacha20::ChaCha20Legacy as chacha20::cipher::KeyIvInit>::new(
|
||||
&self.main_key2,
|
||||
let mut cipher = <SshChaCha20 as chacha20::cipher::KeyIvInit>::new(
|
||||
&self.main_key,
|
||||
&packet_number.to_be_bytes().into(),
|
||||
);
|
||||
|
||||
let tag_offset = bytes.full_packet().len() - 16;
|
||||
let data_to_mac = &bytes.full_packet()[..tag_offset];
|
||||
eprintln!("data_to_mac: {:x?}", &data_to_mac);
|
||||
|
||||
let mac = {
|
||||
let mut poly1305_key = [0; poly1305::KEY_SIZE];
|
||||
|
|
@ -176,20 +164,17 @@ impl SshChaCha20Poly1305 {
|
|||
|
||||
let read_tag = poly1305::Tag::from_slice(&bytes.full_packet()[tag_offset..]);
|
||||
|
||||
eprintln!("expected MAC: {mac:x?}");
|
||||
eprintln!("found MAC: {read_tag:x?}");
|
||||
|
||||
if !bool::from(mac.ct_eq(read_tag)) {
|
||||
return Err(crate::client_error!(
|
||||
"failed to decrypt: invalid poly1305 MAC"
|
||||
));
|
||||
}
|
||||
|
||||
//mac.verify(read_tag)
|
||||
// .map_err(|err| crate::client_error!("failed to decrypt invalid poly1305 MAC: {err}"))?;
|
||||
|
||||
cipher.seek(64);
|
||||
|
||||
todo!()
|
||||
let encrypted_packet_content = bytes.content_mut();
|
||||
cipher.apply_keystream(encrypted_packet_content);
|
||||
|
||||
Packet::from_raw(encrypted_packet_content)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,9 +10,10 @@ use packet::{
|
|||
DhKeyExchangeInitPacket, DhKeyExchangeInitReplyPacket, KeyExchangeInitPacket, Packet,
|
||||
PacketTransport, SshPublicKey, SshSignature,
|
||||
};
|
||||
use parse::{MpInt, NameList};
|
||||
use parse::{MpInt, NameList, Parser};
|
||||
use rand::RngCore;
|
||||
use sha2::Digest;
|
||||
use tracing::{debug, info};
|
||||
use x25519_dalek::{EphemeralSecret, PublicKey};
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -304,7 +305,18 @@ impl ServerConnection {
|
|||
self.state = ServerState::ServiceRequest {};
|
||||
self.packet_transport.set_key(h, k);
|
||||
}
|
||||
ServerState::ServiceRequest {} => {}
|
||||
ServerState::ServiceRequest {} => {
|
||||
if packet.payload.get(0) != Some(&Packet::SSH_MSG_SERVICE_REQUEST) {
|
||||
return Err(client_error!("did not send SSH_MSG_SERVICE_REQUEST"));
|
||||
}
|
||||
let mut p = Parser::new(&packet.payload[1..]);
|
||||
let service = p.utf8_string()?;
|
||||
debug!(?service, "Client requesting service");
|
||||
|
||||
if service != "ssh-userauth" {
|
||||
return Err(client_error!("only supports ssh-userauth"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -63,6 +63,7 @@ pub(crate) struct Packet {
|
|||
pub(crate) payload: Vec<u8>,
|
||||
}
|
||||
impl Packet {
|
||||
pub(crate) const SSH_MSG_SERVICE_REQUEST: u8 = 5;
|
||||
pub(crate) const SSH_MSG_KEXINIT: u8 = 20;
|
||||
pub(crate) const SSH_MSG_NEWKEYS: u8 = 21;
|
||||
pub(crate) const SSH_MSG_KEXDH_INIT: u8 = 30;
|
||||
|
|
@ -78,9 +79,10 @@ impl Packet {
|
|||
};
|
||||
let payload = &bytes[1..][..payload_len];
|
||||
|
||||
if (bytes.len() + 4) % 8 != 0 {
|
||||
return Err(client_error!("full packet length must be multiple of 8"));
|
||||
}
|
||||
// TODO: this fails with OpenSSH client... why?
|
||||
//if (bytes.len() + 4) % 8 != 0 {
|
||||
// return Err(client_error!("full packet length must be multiple of 8: {}", bytes.len()));
|
||||
//}
|
||||
|
||||
Ok(Self {
|
||||
payload: payload.to_vec(),
|
||||
|
|
@ -138,7 +140,7 @@ impl<'a> KeyExchangeInitPacket<'a> {
|
|||
"expected SSH_MSG_KEXINIT packet, found {kind}"
|
||||
));
|
||||
}
|
||||
let cookie = c.read_array::<16>()?;
|
||||
let cookie = c.array::<16>()?;
|
||||
let kex_algorithms = c.name_list()?;
|
||||
let server_host_key_algorithms = c.name_list()?;
|
||||
let encryption_algorithms_client_to_server = c.name_list()?;
|
||||
|
|
@ -258,6 +260,7 @@ impl<'a> DhKeyExchangeInitReplyPacket<'a> {
|
|||
|
||||
pub(crate) struct RawPacket {
|
||||
len: usize,
|
||||
mac_len: usize,
|
||||
raw: Vec<u8>,
|
||||
}
|
||||
impl RawPacket {
|
||||
|
|
@ -267,8 +270,9 @@ impl RawPacket {
|
|||
pub(crate) fn full_packet(&self) -> &[u8] {
|
||||
&self.raw
|
||||
}
|
||||
pub(crate) fn into_full_packet(self) -> Vec<u8> {
|
||||
self.raw
|
||||
pub(crate) fn content_mut(&mut self) -> &mut [u8] {
|
||||
let mac_start = self.raw.len() - self.mac_len;
|
||||
&mut self.raw[4..mac_start]
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -323,9 +327,9 @@ impl PacketParser {
|
|||
|
||||
decrytor.decrypt_len(&mut len_to_decrypt, next_seq_nr);
|
||||
let packet_length = u32::from_be_bytes(len_to_decrypt);
|
||||
let packet_length = packet_length.try_into().unwrap();
|
||||
let packet_length: usize = packet_length.try_into().unwrap();
|
||||
|
||||
dbg!(packet_length);
|
||||
let packet_length = packet_length + decrytor.additional_mac_len();
|
||||
|
||||
self.packet_length = Some(packet_length);
|
||||
|
||||
|
|
@ -347,6 +351,7 @@ impl PacketParser {
|
|||
consumed,
|
||||
RawPacket {
|
||||
raw: std::mem::take(&mut self.raw_data),
|
||||
mac_len: decrytor.additional_mac_len(),
|
||||
len: packet_length,
|
||||
},
|
||||
)))
|
||||
|
|
|
|||
|
|
@ -12,16 +12,16 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
pub(crate) fn u8(&mut self) -> Result<u8> {
|
||||
let arr = self.read_array::<1>()?;
|
||||
let arr = self.array::<1>()?;
|
||||
Ok(arr[0])
|
||||
}
|
||||
|
||||
pub(crate) fn u32(&mut self) -> Result<u32> {
|
||||
let arr = self.read_array()?;
|
||||
let arr = self.array()?;
|
||||
Ok(u32::from_be_bytes(arr))
|
||||
}
|
||||
|
||||
pub(crate) fn read_array<const N: usize>(&mut self) -> Result<[u8; N]> {
|
||||
pub(crate) fn array<const N: usize>(&mut self) -> Result<[u8; N]> {
|
||||
if self.0.len() < N {
|
||||
return Err(crate::client_error!("packet too short"));
|
||||
}
|
||||
|
|
@ -30,7 +30,7 @@ impl<'a> Parser<'a> {
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
pub(crate) fn read_slice(&mut self, len: usize) -> Result<&'a [u8]> {
|
||||
pub(crate) fn slice(&mut self, len: usize) -> Result<&'a [u8]> {
|
||||
if self.0.len() < len {
|
||||
return Err(crate::client_error!("packet too short"));
|
||||
}
|
||||
|
|
@ -49,19 +49,28 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
pub(crate) fn name_list(&mut self) -> Result<NameList<'a>> {
|
||||
let len = self.u32()?;
|
||||
let list = self.read_slice(len.try_into().unwrap())?;
|
||||
let Ok(list) = str::from_utf8(list) else {
|
||||
return Err(crate::client_error!("name-list is invalid UTF-8"));
|
||||
};
|
||||
let list = self.utf8_string()?;
|
||||
Ok(NameList(list))
|
||||
}
|
||||
|
||||
pub(crate) fn mpint(&mut self) -> Result<MpInt<'a>> {
|
||||
let len = self.u32()?;
|
||||
let data = self.read_slice(len as usize)?;
|
||||
let data = self.string()?;
|
||||
Ok(MpInt(data))
|
||||
}
|
||||
|
||||
pub(crate) fn string(&mut self) -> Result<&'a [u8]> {
|
||||
let len = self.u32()?;
|
||||
let data = self.slice(len.try_into().unwrap())?;
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
pub(crate) fn utf8_string(&mut self) -> Result<&'a str> {
|
||||
let s = self.string()?;
|
||||
let Ok(s) = str::from_utf8(s) else {
|
||||
return Err(crate::client_error!("name-list is invalid UTF-8"));
|
||||
};
|
||||
Ok(s)
|
||||
}
|
||||
}
|
||||
|
||||
/// A simplified `byteorder` clone that emits client errors when the data is too short.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue