/
main.rs
369 lines (327 loc) · 11.7 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use std::net::{SocketAddr, Shutdown};
use std::net::{Ipv6Addr, SocketAddrV4, SocketAddrV6};
use std::time::Duration;
use anyhow::Context;
use colored::*;
use futures::{future, TryFutureExt};
use lettre::{ClientSecurity, Transport};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::mpsc;
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use tokio::sync::Mutex;
use tokio::time::timeout;
mod config;
use config::Config;
type VoidRes = Result<(), anyhow::Error>;
type EmailType = lettre_email::Email;
lazy_static::lazy_static! {
static ref CONFIG: Config = Config::from_env();
static ref SPAM_LIMITER: Arc<Mutex<HashMap<Ipv6Addr, usize>>> = Arc::new(Mutex::new(HashMap::new()));
static ref CONN_COUNTER: Arc<Mutex<HashMap<Ipv6Addr, i8>>> = Arc::new(Mutex::new(HashMap::new()));
}
#[tokio::main]
async fn main() -> VoidRes {
env_logger::builder().format_timestamp(None).init();
log::info!("Starting up with configuration {:#?}", *CONFIG);
let (email_tx, email_rx) = mpsc::unbounded_channel();
tokio::select! {
res = email_queue(email_rx) => log::error!("email queue stopped {:?}", res),
res = ipv4_listener() => log::error!("ipv4 listener stopped {:?}", res),
res = ipv6_listener(email_tx) => log::error!("ipv6 listener stopped {:?}", res),
};
log::info!("Exiting");
Ok(())
}
async fn email_queue(mut rx: UnboundedReceiver<EmailType>) -> Result<(), anyhow::Error> {
let mut smtp = lettre::SmtpClient::new(
(CONFIG.smtp_forward_host.as_str(), 25),
ClientSecurity::None,
)?
.transport();
while let Some(msg) = rx.recv().await {
tokio::task::block_in_place(|| smtp.send(msg.into()))?;
log::info!("Mail sent");
}
log::info!("Exiting email loop");
Ok(())
}
async fn ipv4_listener() -> VoidRes {
let bind_addr = format!("{}:{}", CONFIG.ipv4_addr, CONFIG.public_port);
let mut listener = TcpListener::bind(bind_addr).await?;
log::info!("Listening ipv4");
loop {
let (socket, client_addr) = listener.accept().await?;
let addr = match client_addr {
SocketAddr::V4(addr) => addr,
_ => unreachable!()
};
tokio::spawn(handle_ipv4(socket, addr)
.inspect_err(|e| log::error!("handling ipv4failed {:?}", e)));
}
}
async fn ipv6_listener(email_tx: UnboundedSender<EmailType>) -> VoidRes {
let bind_addr = format!("{}:{}", CONFIG.ipv6_addr, CONFIG.public_port);
let mut listener = TcpListener::bind(bind_addr).await?;
log::info!("Listening ipv6");
loop {
let (socket, client_addr) = listener.accept().await?;
let addr = match client_addr {
SocketAddr::V6(addr) => addr,
_ => unreachable!()
};
tokio::spawn(handle_ipv6(socket, addr, email_tx.clone())
.inspect_err(|e| log::error!("handling ipv6 failed {:?}", e)));
}
}
async fn handle_ipv4(mut socket: TcpStream, addr: SocketAddrV4) -> VoidRes {
log::info!("Connection from {}", addr);
let msg = format!(
"\n\
You connected to the IPv4 version of the service.\n\
To force netcat to use IPv6, do\n\
\n\
$ nc -6 {hostname} {port}\n\
\n\
If it fails to connect or you see this msg again, you\n\
don't have IPv6 connectivity and this tool is of no use\n\
for you.\n\
\n\
Find sources of this tool from:\n\
\n\
\thttps://github.com/vvilhonen/ipv6shell\n\
\n",
hostname = CONFIG.public_hostname,
port = CONFIG.public_port
);
let timeout_fut = timeout(Duration::from_secs(2), socket.write_all(msg.as_bytes())).await;
if let Err(_) = timeout_fut {
log::warn!("Timeout sending ipv4");
}
Ok(())
}
async fn handle_ipv6(
socket: TcpStream,
addr: SocketAddrV6,
email_tx: UnboundedSender<EmailType>,
) -> VoidRes {
let ip_addr = *addr.ip();
log::info!("Connection open {}", addr);
track_conn(ip_addr, 1).await?;
let mut shell = Shell::new(socket, ip_addr, email_tx);
let result = run_session(&mut shell).await;
if let Err(e) = shell.save_debug_log().await {
log::error!("Saving debug log failed {}", e);
}
if let Err(e) = shell.socket.shutdown(Shutdown::Both) {
log::error!("Shutdown failed {}", e);
}
log::info!("Connection close {}", addr);
track_conn(ip_addr, -1).await?;
result
}
async fn track_conn(addr: Ipv6Addr, delta: i8) -> VoidRes {
let mut conn_counter = CONN_COUNTER.lock().await;
let entry = conn_counter.entry(addr).or_insert(0);
if *entry > 5 && delta > 0 {
anyhow::bail!("Too many connections from {}", addr);
} else {
*entry += delta;
}
Ok(())
}
async fn run_session(shell: &mut Shell) -> VoidRes {
shell.print_banner().await?;
loop {
shell.prompt().await?;
let cmd = shell.read_cmd().await?;
let parts = cmd.split_whitespace().collect::<Vec<_>>();
match &parts[..] {
["scan"] => shell.scan(PORTS).await?,
["scan", port] => match port.parse::<u16>() {
Ok(port) => {
shell.scan(&[port]).await?;
}
_ => shell.write_all("Invalid port\n").await?,
},
["support"] => shell.support().await?,
["quit"] => {
shell.end().await?;
break;
},
other => {
shell
.write_all(&format!("Unknown/empty command: {:?}\n", other))
.await?
}
};
}
Ok(())
}
struct Shell {
socket: TcpStream,
log: String,
addr: Ipv6Addr,
email_tx: UnboundedSender<EmailType>,
}
impl Shell {
pub fn new(socket: TcpStream, addr: Ipv6Addr, email_tx: UnboundedSender<EmailType>) -> Self {
socket.set_nodelay(true).unwrap();
socket.set_recv_buffer_size(1024).unwrap();
socket.set_send_buffer_size(1024).unwrap();
// let (rx, tx) = socket.into_split();
Shell {
socket,
log: format!("Connection from {}\n", addr),
addr,
email_tx,
}
}
pub async fn print_banner(&mut self) -> VoidRes {
let addr_text = format!("{}", self.addr);
let msg = format!("\n{header}\n\
\n\
You are connected from:\n\
\n\
\t{addr}\n\
\n\
IPv6 addresses are routable in the public internet by default\n\
and you might have your current computer behind IPv4 NAT\n\
unexpectedly exposed this way.\n\
\n\
This tool allows you to scan yourself for open ports\n\
and if it shows your known open ports closed, your\n\
firewall or something else on the way is blocking \n\
the connection attempts as it should.\n\
\n\
Commands:\n\
\n\
\tscan - scans some common ports\n\
\tscan <port> - scans <port>\n\
\tsupport - contact support\n\
\tquit\n\
\n\
Find sources of this tool from:\n\
\n\
\thttps://github.com/vvilhonen/ipv6shell\n\
\n\
",
header="==== ipv6shell ====".bold(),
addr=addr_text.red().bold());
self.write_all(&msg).await?;
Ok(())
}
pub async fn end(&mut self) -> VoidRes {
self.write_all("Thanks, see you soon again!\n").await?;
Ok(())
}
pub async fn prompt(&mut self) -> VoidRes {
self.write_all(&"$ ".green()).await?;
Ok(())
}
pub async fn scan(&mut self, ports: &[u16]) -> VoidRes {
self.write_all("Scanning ports\n").await?;
let ports_open_fut = ports.iter().map(|port| Self::scan_port(&self.addr, *port));
let ports_open: Vec<bool> = future::join_all(ports_open_fut).await;
self.write_all(" port status\n ==== ======\n").await?;
for (port, open) in ports.iter().zip(ports_open) {
let open_text = if open {
"OPEN".red().bold()
} else {
"CLOSED".green().bold()
};
let msg = format!("{:6} {}\n", port, open_text);
self.write_all(&msg.red().bold()).await?;
}
Ok(())
}
async fn scan_port(ip: &Ipv6Addr, port: u16) -> bool {
let addr: SocketAddr = (*ip, port).into();
let connect = TcpStream::connect(addr);
let res = timeout(Duration::from_secs(2), connect).await;
match res {
Ok(Ok(_)) => true,
_ => false,
}
}
pub async fn support(&mut self) -> VoidRes {
self.write_all("Write your message and end the msg with empty line\n")
.await?;
let mut buf = Vec::new();
while buf.len() < 1024 {
let mut chunk = [0u8; 256];
let n = timeout(Duration::from_secs(500), self.socket.read(&mut chunk)).await??;
if n == 0 {
break;
}
buf.extend(&chunk[..n]);
let empty = buf
.iter()
.zip(buf.iter().skip(1))
.any(|(a, b)| *a == b'\n' && *b == b'\n');
if empty {
break;
}
}
let msg = std::str::from_utf8(&buf)?.trim().to_owned();
let email = lettre_email::EmailBuilder::new()
.to((CONFIG.email_to_addr.as_str(), CONFIG.email_to_name.as_str()))
.from(CONFIG.email_from.as_str())
.subject("Ascii6 support msg!")
.text(msg)
.build()?;
let mut spam = SPAM_LIMITER.lock().await;
let entry = spam.entry(self.addr).or_insert(0);
if *entry > 5 {
self.write_all("Sorry, we have enough support requests already\n")
.await?;
} else {
*entry += 1;
self.email_tx.send(email)?;
self.write_all("Thanks! We \"sent\" your message to support.\nWe'll reply if you included any contact details.\n").await?;
}
Ok(())
}
pub async fn read_cmd(&mut self) -> Result<String, anyhow::Error> {
let mut buf = vec![0; 200];
let timeout_fut = timeout(Duration::from_secs(10), self.socket.read(&mut buf)).await;
let n = match timeout_fut {
Err(e) => {
self.write_all("Idle, closing..\n").await?;
return Err::<_, anyhow::Error>(e.into()).context("read timeout");
}
Ok(read) => read?,
};
if n == 0 {
anyhow::bail!("closed input");
}
let data = std::str::from_utf8(&buf[..n])?.trim().to_owned();
self.log.push_str(&data);
Ok(data)
}
async fn write_all(&mut self, src: &str) -> VoidRes {
self.log.push_str(src);
let timeout_fut = timeout(Duration::from_secs(2), self.socket.write_all(src.as_bytes())).await;
match timeout_fut {
Err(e) => {
self.log.push_str("<write timeout, closing>\n");
return Err::<_, anyhow::Error>(e.into()).context("write timeout");
}
Ok(write) => write?,
};
Ok(())
}
async fn save_debug_log(&self) -> VoidRes {
if let Some(ref debug_dir) = &CONFIG.debug_dir {
let time = chrono::Local::now().to_rfc3339();
let path = format!("{}/{}_{}.log", debug_dir, time, self.addr);
::tokio::fs::write(path, &self.log).await?;
}
Ok(())
}
}
const PORTS: &[u16] = &[
20, 21, 22, 23, 25, 80, 443, 445, 465, 3000, 5000, 8000, 8080,
];