Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickTulskie committed Dec 20, 2022
0 parents commit e3aabcf
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
53 changes: 53 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members=["defangify", "refangify"]
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Defangify

Defangify IOCs (ips, emails, urls, etc)

## Usage

```defangify something@somewhere.com```

## Notes

Still a work in progress...
10 changes: 10 additions & 0 deletions defangify/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "defangify"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
regex = "1"
lazy_static = "1"
63 changes: 63 additions & 0 deletions defangify/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use lazy_static::lazy_static;
use regex::Regex;
use std::env;

lazy_static! {
// Replacers
static ref DOTS_REGEX: Regex = Regex::new(r"\.").unwrap();
static ref COLONS_REGEX: Regex = Regex::new(r":").unwrap();
static ref AT_REGEX: Regex = Regex::new(r"@").unwrap();
static ref HTTP_REGEX: Regex = Regex::new(r"(?i)http").unwrap();
static ref SLASHES_REGEX: Regex = Regex::new(r"://").unwrap();

// Matchers
static ref EMAIL_REGEX: Regex = Regex::new(r"(?i)([A-Za-z0-9!#$%&'*+/=?^_{|.}~-]+@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)").unwrap();
static ref IPV4_REGEX: Regex = Regex::new(r"(?:(?:25[0-5]|2[0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(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?::(?:(?:(?::[0-9A-Fa-f]{1,4}){1,7})|(?:(?::[0-9A-Fa-f]{1,4}){0,5}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(?:%.+)?\s*").unwrap();
}

fn main() {
let args: Vec<String> = env::args().collect();
let first_email = &args[1];
let defangged = defangify(first_email);
println!("{}", defangged);
}

fn defangify(input: &str) -> String {
if IPV4_REGEX.is_match(input) {
return defang_ipv4(input);
} else if IPV6_REGEX.is_match(input) {
return defang_ipv6(input);
} else if EMAIL_REGEX.is_match(input) {
return defangify_email(input)
} else {
return defangify_url(input);
}
}

fn defangify_url(input: &str) -> String {
let no_dots = DOTS_REGEX.replace_all(input, "[.]");
let no_http = HTTP_REGEX.replace_all(&no_dots, "hxxp");
let no_slashes = SLASHES_REGEX.replace_all(&no_http, "[://]");

return no_slashes.to_string();
}

fn defang_ipv4(input: &str) -> String {
let no_dots = DOTS_REGEX.replace_all(input, "[.]");

return no_dots.to_string();
}

fn defang_ipv6(input: &str) -> String {
let no_colons = COLONS_REGEX.replace_all(input, "[:]");

return no_colons.to_string();
}

fn defangify_email(input: &str) -> String {
let no_dots = DOTS_REGEX.replace_all(input, "[.]");
let no_at = AT_REGEX.replace_all(&no_dots, "[@]");

return no_at.to_string();
}
8 changes: 8 additions & 0 deletions refangify/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "refangify"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
4 changes: 4 additions & 0 deletions refangify/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
// (?:[a-zA-Z]+:\/\/)?[\w]+?(?:\[\.\]|\.)[\w]+?(?:\[\.\]|\.)[\w]+
println!("all the fangs!");
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

0 comments on commit e3aabcf

Please sign in to comment.