From 3452ce58ee68cea79f8fd97e627aa80926fe3138 Mon Sep 17 00:00:00 2001 From: serxoz Date: Wed, 7 Dec 2022 17:31:04 +0100 Subject: [PATCH] random response to register --- Cargo.lock | 48 +++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 83 +++++++++++++++++++++++++++++++++++++++----------- src/sip/mod.rs | 1 + 4 files changed, 115 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a53a275..1f1a8bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,17 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "getrandom" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "hermit-abi" version = "0.1.19" @@ -117,6 +128,12 @@ version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "proc-macro2" version = "1.0.47" @@ -135,6 +152,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "redox_syscall" version = "0.2.16" @@ -163,6 +210,7 @@ dependencies = [ name = "sip-tarpit" version = "0.1.0" dependencies = [ + "rand", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index 9d09345..797e524 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +rand = "0.8.5" tokio = { version = "1", features = ["full"] } diff --git a/src/main.rs b/src/main.rs index 8b2f3fb..2ce9c07 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,10 +5,12 @@ */ pub mod sip; +use crate::sip::forbidden::*; use crate::sip::not_found::*; use crate::sip::options::*; use crate::sip::register::*; use crate::sip::unauthorized::*; +use rand::Rng; use std::error::Error; use std::net::SocketAddr; use std::{env, io}; @@ -75,24 +77,69 @@ impl Server { sleep(Duration::from_secs(1)).await; let register = Register::parse(&msg); - let unauthorized = Unauthorized { - command: "SIP/2.0 401 Unauthorized".to_string(), - via: register.via, - call_id: register.call_id, - from: register.from, - to: format!("{};tag={}", register.to, register.branch), - cseq: register.cseq, - www_authenticate: Some("Digest realm=\"asterisk\",nonce=\"1670408728/f2288b61ff3e1c5a8fbad2282734a53b\",opaque=\"37899594559ff83d\",algorithm=md5,qop=\"auth\"".to_string()), - server: Some("Asterisk PBX 18.9.0".to_string()), - content_length: Some(0) - }; - let amt = socket.send_to(&unauthorized.serialize(), &peer).await?; - println!( - "Sent {}/{} bytes to {}", - amt, - &unauthorized.serialize().len(), - peer - ); + + // responde aleatoriamente unauthorized (a ext. existe) + // ou forbidden (non existe) + let mut num = rand::thread_rng().gen_range(0..1000); + + //To: "6666666666" + // get exten, if is not in range send forbidden + let mut exten = String::new(); + if register.to.contains("<") && register.to.contains(">") { + let start = register.to.find("<").unwrap_or(0); //ou inicio + let end = register.to.find(">").unwrap_or(register.to.len()); //ou fin + let ext_with_host = ®ister.to[start..end]; + exten = ext_with_host + .split("@") + .collect::>()[0] + .split(":") + .collect::>()[1] + .to_string(); + } + // si ten máis de 5 díxitos é a exten de proba que manda + // o sipvicius como test (normalmente é a primeira) + if exten.len() > 5 { + num = 9000; + } + + if num < 500 { + let unauthorized = Unauthorized { + command: "SIP/2.0 401 Unauthorized".to_string(), + via: register.via, + call_id: register.call_id, + from: register.from, + to: format!("{};tag={}", register.to, register.branch), + cseq: register.cseq, + www_authenticate: Some("Digest realm=\"asterisk\",nonce=\"1670408728/f2288b61ff3e1c5a8fbad2282734a53b\",opaque=\"37899594559ff83d\",algorithm=md5,qop=\"auth\"".to_string()), + server: Some("Asterisk PBX 18.9.0".to_string()), + content_length: Some(0) + }; + let amt = socket.send_to(&unauthorized.serialize(), &peer).await?; + println!( + "Unauthorized: Sent {}/{} bytes to {}", + amt, + &unauthorized.serialize().len(), + peer + ); + } else { + let forbidden = Forbidden { + command: "SIP/2.0 403 Forbidden".to_string(), + via: register.via, + call_id: register.call_id, + from: register.from, + to: format!("{};tag={}", register.to, register.branch), + cseq: register.cseq, + server: Some("Asterisk PBX 18.9.0".to_string()), + content_length: Some(0) + }; + let amt = socket.send_to(&forbidden.serialize(), &peer).await?; + println!( + "Forbidden: Sent {}/{} bytes to {}", + amt, + &forbidden.serialize().len(), + peer + ); + } }, _ => println!("Packet don't known, yet..."), } diff --git a/src/sip/mod.rs b/src/sip/mod.rs index 5b91b0b..96c25bd 100644 --- a/src/sip/mod.rs +++ b/src/sip/mod.rs @@ -1,3 +1,4 @@ +pub mod forbidden; pub mod not_found; pub mod options; pub mod register;