Something went wrong. Try again.
Expose HTTP/SSH/TCP services through SSH port forwarding. sandhole.com.br
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508// sandhole: Expose HTTP/SSH/TCP services through SSH port forwarding// Copyright (C) 2024-2026 Eric Rodrigues Pires//// This program is free software: you can redistribute it and/or modify it under// the terms of the GNU Affero General Public License as published by the Free// Software Foundation, either version 3 of the License, or (at your option)// any later version.//// This program is distributed in the hope that it will be useful, but WITHOUT// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for// more details.//// You should have received a copy of the GNU Affero General Public License along// with this program. If not, see <https://www.gnu.org/licenses/>.
use std::{net::SocketAddr, sync::Arc, time::Duration};
use axum::{Json, Router, extract::Request, response::IntoResponse, routing::post};use clap::Parser;use http::StatusCode;use hyper::{body::Incoming, service::service_fn};use hyper_util::{ rt::{TokioExecutor, TokioIo}, server::conn::auto::Builder,};use regex::Regex;use russh::{ Channel, client::{self, Msg, Session},};use russh::{ client::ChannelOpenHandle, keys::{key::PrivateKeyWithHashAlg, load_secret_key},};use sandhole::{ApplicationConfig, entrypoint};use serde::Deserialize;use tokio::{ io::AsyncWriteExt, net::TcpStream, sync::watch, time::{sleep, timeout},};use tower::Service;
use crate::common::SandholeHandle;
const CMD_SHIFT_TAB: &[u8] = b"\x1b[Z";const CMD_DOWN: &[u8] = b"\x1b[B";const CMD_DELETE: &[u8] = b"\x1b[3~";const CMD_ENTER: &[u8] = b"\r";const CMD_CTRL_C: &[u8] = b"\x03";
/// This test follows an admin user that removes a connected user through the/// admin interface.#[test_log::test(tokio::test(flavor = "multi_thread"))]async fn admin_remove_user() { // 1. Initialize Sandhole let config = ApplicationConfig::parse_from([ "sandhole", "--domain=foobar.tld", "--user-keys-directory", &(format!( "{}/tests/data/user_keys", std::env::var("CARGO_MANIFEST_DIR").unwrap() )), "--admin-keys-directory", &(format!( "{}/tests/data/admin_keys", std::env::var("CARGO_MANIFEST_DIR").unwrap() )), "--certificates-directory", &(format!( "{}/tests/data/certificates", std::env::var("CARGO_MANIFEST_DIR").unwrap() )), "--private-key-file", &(format!( "{}/tests/data/server_keys/ssh", std::env::var("CARGO_MANIFEST_DIR").unwrap() )), "--acme-cache-directory", &(format!( "{}/tests/data/acme_cache", std::env::var("CARGO_MANIFEST_DIR").unwrap() )), "--disable-directory-creation", "--listen-address=127.0.0.1", "--ssh-port=18022", "--http-port=18080", "--https-port=18443", "--acme-use-staging", "--password-authentication-url=http://127.0.0.1:38080/authenticate", "--bind-hostnames=all", "--allow-requested-subdomains", "--allow-requested-ports", "--idle-connection-timeout=1s", "--authentication-request-timeout=5s", "--http-request-timeout=5s", ]); let _sandhole_handle = SandholeHandle(tokio::spawn(async move { entrypoint(config).await })); if timeout(Duration::from_secs(5), async { while TcpStream::connect("127.0.0.1:18022").await.is_err() { sleep(Duration::from_millis(100)).await; } }) .await .is_err() { panic!("Timeout waiting for Sandhole to start.") };
// 2. Start SSH client that will host the login API and not be removed let key = load_secret_key( std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()) .join("tests/data/private_keys/key1"), None, ) .expect("Missing file key1"); let ssh_client = SshClient; let mut session_1 = russh::client::connect(Default::default(), "127.0.0.1:18022", ssh_client) .await .expect("Failed to connect to SSH server"); assert!( session_1 .authenticate_publickey( "api_user", PrivateKeyWithHashAlg::new( Arc::new(key), session_1.best_supported_rsa_hash().await.unwrap().flatten() ) ) .await .expect("SSH authentication failed") .success(), "authentication didn't succeed" ); session_1 .tcpip_forward("localhost", 38080) .await .expect("tcpip_forward failed"); session_1 .tcpip_forward("aaa.foobar.tld", 80) .await .expect("tcpip_forward failed");
// 3. Start SSH client that will be proxied and later removed by the admin let ssh_client = SshClient; let mut session_2 = russh::client::connect(Default::default(), "127.0.0.1:18022", ssh_client) .await .expect("Failed to connect to SSH server"); assert!( session_2 .authenticate_password("custom_user", "password") .await .expect("SSH authentication failed") .success(), "authentication didn't succeed" ); session_2 .tcpip_forward("aaa.foobar.tld", 443) .await .expect("tcpip_forward failed"); session_2 .tcpip_forward("localhost", 12345) .await .expect("tcpip_forward failed"); // Required for updating the admin interface data sleep(Duration::from_secs(3)).await;
// 4. Request admin pty let key = load_secret_key( std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()) .join("tests/data/private_keys/admin"), None, ) .expect("Missing file admin"); let ssh_client = SshClientAdmin; let mut session = client::connect(Default::default(), "127.0.0.1:18022", ssh_client) .await .expect("Failed to connect to SSH server"); assert!( session .authenticate_publickey( "admin", PrivateKeyWithHashAlg::new( Arc::new(key), session.best_supported_rsa_hash().await.unwrap().flatten() ) ) .await .expect("SSH authentication failed") .success(), "authentication didn't succeed" ); let mut channel = session .channel_open_session() .await .expect("channel_open_session failed"); channel .request_pty(false, "xterm", 140, 30, 640, 480, &[]) .await .expect("request_pty failed"); channel .exec(false, "admin") .await .expect("exec admin failed");
// 5. Interact with the admin interface and verify displayed data let (tx, mut rx) = watch::channel(String::new()); let rx_clone = rx.clone(); let mut writer = channel.make_writer(); let jh = tokio::spawn(async move { let mut parser = vt100_ctt::Parser::new(30, 140, 0); let mut screen = Vec::new(); while let Some(msg) = channel.wait().await { match msg { russh::ChannelMsg::Data { data } => { parser.process(&data); let new_screen = parser.screen(); let contents_formatted = new_screen.contents_formatted(); if contents_formatted != screen { screen = contents_formatted; let _ = tx.send(new_screen.contents()); } } _ => break, } } }); if timeout(Duration::from_secs(10), async move { // 5a. Validate HTTP tab data let search_strings: Vec<Regex> = [ r"Sandhole admin v\d+\.\d+\.\d+", r"HTTP services", r"aaa\.foobar\.tld", r"SHA256:GehKyA\S*", r"127\.0\.0\.1:\d{4,5}", r"custom_user", r"127\.0\.0\.1:\d{4,5}", ] .into_iter() .map(|re| Regex::new(re).expect("Invalid regex")) .collect(); loop { assert!(rx.changed().await.is_ok(), "channel closed unexpectedly"); let screen = rx.borrow_and_update(); if search_strings.iter().all(|re| re.is_match(&screen)) { break; } } // 5b. Select HTTP service and open connected users prompt writer .write_all(CMD_DOWN) .await .expect("channel write failed"); writer .write_all(CMD_ENTER) .await .expect("channel write failed"); let search_strings: Vec<Regex> = [ r"Sandhole admin v\d+\.\d+\.\d+", r"Connected users", r"SHA256:GehKyA21BBK6eJCouziacUmqYDNl8BPMGG0CTtLSrbQ", r"custom_user", r" <Enter> Details ", ] .into_iter() .map(|re| Regex::new(re).expect("Invalid regex")) .collect(); loop { assert!(rx.changed().await.is_ok(), "channel closed unexpectedly"); let screen = rx.borrow_and_update(); if search_strings.iter().all(|re| re.is_match(&screen)) { break; } } // 5c. Select custom_user and open user details prompt writer .write_all(CMD_DOWN) .await .expect("channel write failed"); writer .write_all(CMD_DOWN) .await .expect("channel write failed"); writer .write_all(CMD_ENTER) .await .expect("channel write failed"); let search_strings: Vec<Regex> = [ r"Sandhole admin v\d+\.\d+\.\d+", r"User details", r"custom_user", r"Type: User", r"(authenticated with password)", r" <Esc> Close <Delete> Remove ", ] .into_iter() .map(|re| Regex::new(re).expect("Invalid regex")) .collect(); loop { assert!(rx.changed().await.is_ok(), "channel closed unexpectedly"); let screen = rx.borrow_and_update(); if search_strings.iter().all(|re| re.is_match(&screen)) { break; } } // 5d. Open removal prompt writer .write_all(CMD_DELETE) .await .expect("channel write failed"); let search_strings: Vec<Regex> = [ r"Sandhole admin v\d+\.\d+\.\d+", r"Remove user?", r"Are you sure you want to remove the following user?", r"custom_user", r"They might still be able to reconnect via the login API!", r" <Esc> Cancel <Enter> Confirm ", ] .into_iter() .map(|re| Regex::new(re).expect("Invalid regex")) .collect(); loop { assert!(rx.changed().await.is_ok(), "channel closed unexpectedly"); let screen = rx.borrow_and_update(); if search_strings.iter().all(|re| re.is_match(&screen)) { break; } } // 5e. Confirm removal writer .write_all(CMD_ENTER) .await .expect("channel write failed"); let search_strings: Vec<Regex> = [ r"Sandhole admin v\d+\.\d+\.\d+", r"User removed successfully!", r" <Enter> Close ", ] .into_iter() .map(|re| Regex::new(re).expect("Invalid regex")) .collect(); loop { assert!(rx.changed().await.is_ok(), "channel closed unexpectedly"); let screen = rx.borrow_and_update(); if search_strings.iter().all(|re| re.is_match(&screen)) { break; } } // Required for updating the admin interface data sleep(Duration::from_secs(3)).await; assert!(session_2.is_closed(), "user session wasn't terminated"); // 5f. Close prompt, head to the TCP tab, and ensure that the window still displays the first service there assert!( !session_1.is_closed(), "proxy session shouldn't have been terminated" ); writer .write_all(CMD_ENTER) .await .expect("channel write failed"); writer .write_all(CMD_SHIFT_TAB) .await .expect("channel write failed"); writer .write_all(CMD_SHIFT_TAB) .await .expect("channel write failed"); writer .write_all(CMD_SHIFT_TAB) .await .expect("channel write failed"); writer .write_all(CMD_SHIFT_TAB) .await .expect("channel write failed"); let search_strings: Vec<Regex> = [ r"Sandhole admin v\d+\.\d+\.\d+", r"TCP services", r"38080", r"SHA256:GehKyA\S*", r"127\.0\.0\.1:\d{4,5}", ] .into_iter() .map(|re| Regex::new(re).expect("Invalid regex")) .collect(); loop { assert!(rx.changed().await.is_ok(), "channel closed unexpectedly"); let screen = rx.borrow_and_update(); if search_strings.iter().all(|re| re.is_match(&screen)) { break; } } // 5g. Select user and open details writer .write_all(CMD_DOWN) .await .expect("channel write failed"); writer .write_all(CMD_ENTER) .await .expect("channel write failed"); let search_strings: Vec<Regex> = [ r"Sandhole admin v\d+\.\d+\.\d+", r"User details", r"SHA256:GehKyA21BBK6eJCouziacUmqYDNl8BPMGG0CTtLSrbQ", r"Type: User", r"Key comment: key1", r"Algorithm: ssh-ed25519", r" <Esc> Close <Delete> Remove ", ] .into_iter() .map(|re| Regex::new(re).expect("Invalid regex")) .collect(); loop { assert!(rx.changed().await.is_ok(), "channel closed unexpectedly"); let screen = rx.borrow_and_update(); if search_strings.iter().all(|re| re.is_match(&screen)) { break; } } // 5h. Quit the admin interface with Ctrl-C (ETX) writer .write_all(CMD_CTRL_C) .await .expect("channel write failed"); }) .await .is_err() { eprintln!("Last screen:"); eprintln!("{}", rx_clone.borrow().as_str()); panic!("Timed out waiting for admin interface."); } sleep(Duration::from_millis(200)).await; assert!(session.is_closed(), "session didn't close properly"); jh.abort();}
struct SshClient;
impl russh::client::Handler for SshClient { type Error = color_eyre::eyre::Error;
async fn check_server_key( &mut self, _key: &russh::keys::PublicKeyOrCertificate, ) -> Result<bool, Self::Error> { Ok(true) }
async fn server_channel_open_forwarded_tcpip( &mut self, channel: Channel<Msg>, _connected_address: &str, _connected_port: u32, _originator_address: &str, _originator_port: u32, reply: ChannelOpenHandle, _session: &mut Session, ) -> Result<(), Self::Error> { #[derive(Debug, Deserialize)] struct AuthenticationRequest { user: String, password: String, remote_address: SocketAddr, } async fn authentication_route( Json(body): Json<AuthenticationRequest>, ) -> impl IntoResponse { if body.user == "custom_user" && body.password == "password" && body.remote_address.ip().to_canonical().is_loopback() { StatusCode::OK } else { StatusCode::FORBIDDEN } } let router = Router::new().route("/authenticate", post(authentication_route)); let service = service_fn(move |req: Request<Incoming>| router.clone().call(req)); tokio::spawn(async move { Builder::new(TokioExecutor::new()) .serve_connection_with_upgrades(TokioIo::new(channel.into_stream()), service) .await .expect("Invalid request"); }); reply.accept().await; Ok(()) }}
struct SshClientAdmin;
impl russh::client::Handler for SshClientAdmin { type Error = color_eyre::eyre::Error;
async fn check_server_key( &mut self, _key: &russh::keys::PublicKeyOrCertificate, ) -> Result<bool, Self::Error> { Ok(true) }}