Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 94 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,76 @@ See: https://github.com/rust-lang/rust/issues/25860
This program is open-source! View the source for all these exploits here: https://github.com/Speykious/cve-rs
";

#[repr(C)]
#[derive(Debug)]
struct Boy {
age: u32,
name: String,
github_username: String,
}

#[repr(C)]
#[derive(Debug)]
struct Girl {
age: u32,
name: String,
github_username: String,
}

fn transmute_demo(command: &str) -> std::io::Result<()> {
use std::hash::{DefaultHasher, Hash as _, Hasher as _};
use std::io::Write as _;
use std::str::FromStr;

#[repr(C)]
#[derive(Debug)]
struct Boy {
age: u32,
name: String,
github_username: String,
snips: u32,
snails: u32,
puppy_dogs_tails: u64,
}

#[repr(C)]
#[derive(Debug)]
struct Girl {
age: u32,
name: String,
github_username: String,
sugar: f32,
spice: f32,
everything_nice: f64,
}

// Trait to obtain the ingredients from hashing arbitrary strings,
// if the user doesn't want to give us numbers.
trait FromHash: Sized {
fn from_hash(hash: u64) -> Self;
}

impl FromHash for u64 {
fn from_hash(hash: u64) -> Self {
hash
}
}

impl FromHash for u32 {
fn from_hash(hash: u64) -> Self {
(hash & Self::MAX as u64) as Self
}
}

impl FromHash for f64 {
fn from_hash(hash: u64) -> Self {
// Cauchy distribution
let angle = (hash as f64 / u64::MAX as f64 - 0.5) * std::f64::consts::PI;
angle.tan()
}
}

impl FromHash for f32 {
fn from_hash(hash: u64) -> Self {
f64::from_hash(hash) as f32
}
}

fn parse_or_hash<T>(input: String) -> T
where
T: FromStr,
T: FromHash,
{
input.parse::<T>().unwrap_or_else(|_| {
let mut h = DefaultHasher::new();
input.hash(&mut h);
T::from_hash(h.finish())
})
}

let stdin = std::io::stdin();
let mut stdout = std::io::stdout();
Expand Down Expand Up @@ -149,12 +201,35 @@ fn transmute_demo(command: &str) -> std::io::Result<()> {
input_buf.trim().to_owned()
};

let (ingredient1, ingredient2, ingredient3) = {
let recipe = match command {
"boy-to-girl" => "snips, snails and puppy-dogs' tails",
"girl-to-boy" => "sugar and spice and everything nice",
_ => unreachable!(),
};
print!("Enter {recipe} values, separated by commas: ");
stdout.flush()?;

input_buf.clear();
stdin.read_line(&mut input_buf)?;

let mut iter = input_buf.split(",").map(str::trim);
(
iter.next().unwrap_or("0").to_owned(),
iter.next().unwrap_or("0").to_owned(),
iter.next().unwrap_or("0").to_owned(),
)
};

match command {
"boy-to-girl" => {
let boy: Boy = Boy {
age,
name,
github_username,
snips: parse_or_hash(ingredient1),
snails: parse_or_hash(ingredient2),
puppy_dogs_tails: parse_or_hash(ingredient3),
};
println!("Before transmute: {boy:?}");

Expand All @@ -167,6 +242,9 @@ fn transmute_demo(command: &str) -> std::io::Result<()> {
age,
name,
github_username,
sugar: parse_or_hash(ingredient1),
spice: parse_or_hash(ingredient2),
everything_nice: parse_or_hash(ingredient3),
};
println!("Before transmute: {girl:?}");

Expand Down