首页 > 其他分享 >SMTP 通过 ssh 通道发送垃圾邮件

SMTP 通过 ssh 通道发送垃圾邮件

时间:2023-08-03 22:06:14浏览次数:46  
标签:spam tunnel SMTP 垃圾邮件 ssh email localhost SSH

通过SSH隧道传输SMTP

根据设计,我们不允许校外机器使用我们的SMTP服务器。如果我们允许它,我们将允许任何和所有使用我们的SMTP服务器来分发垃圾邮件。
但是也可以通过我们的SMTP服务器发送邮件,这种机制称为SMTP隧道。

Linux说明

1.在Linux机器上安装SSH(默认情况下应该安装)。

2.运行以下命令:(需要你有ssh账号和密码)

   ssh -l username -L 25:mail.cs.toronto.edu:25 cs.toronto.edu

3.在您的邮件客户端中调整您的smtp设置,以便
   SMTP服务器设置为:127.0.0.1

4.发送邮件。

参考:http://www.cs.toronto.edu/~simon/howto/smtptunnels.html

Warning: Hackers Are Using SSH Tunnels to Send Spam

from:https://www.rackaid.com/blog/spam-ssh-tunnel/

Don’t get knocked-out by spammers.

Tired of spammers finding new ways to exploit your server?

Me too.

I spend a lot of time investigating spam incidents for customers.  Typically, hackers use a few common techniques to send their spam.   The most common issues I see are:

  • Web application exploits.
  • Contact form exploits.
  • Compromised user password

With these exploits, you can spot them as they leave clear evidence in the logs.  In the case of PHP, you can enable PHP mail logging and quickly find the script sending the spam.   This makes stopping these types of attacks easy.

This week, I ran into something I’ve not seen before.

An attacker was using an SSH tunnel to send spam.

This attack can be difficult to detect as it leaves little log evidence – just a ssh login entry and the spam itself.

Fortunately, it is easy to stop once you know how it works.

Setting up SSH Tunnels

Sometimes the best way to recognize an attack is to do it yourself.  So once I discovered how the attackers were compromising the server, I tried it myself.

So here’s what I did:

ssh -f [email protected] -L 2000:localhost:25 –N

Then

telnet localhost 2000
Connected to localhost.
Escape character is '^]'.
220 remote.host.com ESMTP Postfix

What just happened?

The ssh command tunneled port 25 on the localhost back to my system on port 2000.

I can now send email through the remote host by connecting locally on port 2000.

The attackers used this technique to inject 10,000’s of emails into the server.

This is a clever approach.  Unlike other attacks, this method leaves few clues.

With some sleuthing, however, you can catch this attack.  You can even prevent it with a simple change to SSH.

Sending Spam with SSH Tunnels

I don’t want to be alarmist, so I want to make it clear:

This SSH spam method requires access to a user account.

In this incident, the attacker had compromised a user account due to a poor quality password.

This is essentially a password compromise, but unlike most attacks, the attackers used a SSH tunnel.  The tunnel made it more difficult to detect and block the exploit.

Here’s a breakdown of how the technique works.

TCP Tunnel to SMTP

SSH, by default, permits TCP port forwarding.  The attackers were using this feature to forward the SMTP port over SSH back to their local system.

Using an SSH tunnel, you can forward a remote port 25 connection back to your local system and use it to send email without authentication.

As you can see in the diagram, The attackers connects to your server over SSH using a compromised user account.   Then, they setup a SSH tunnel to forward port 25 back to their system.  They can then connect locally to port 2000 (or any port they select) to send spam.  Since most servers trust SMTP connections on localhost, no authentication is required.

With this tunnel in place, they attacker can now send spam via the SSH tunnel.

SMTP AUTH & Localhost

In most spam cases involving exploited password, attackers connect directly to the mail server.  As a result, your mail logs will be filled with SMTP authentication attempts – often from many IP addresses.   This makes it easy to identify the compromised account.

With the SSH tunnel technique, SMTP authentication is not required.  As a result, there’s remarkably little evidence in the logs of an attack.

The only indication of a problem is a high volume of bounces or email being sent via localhost.

The attack works because most servers implicitly trust email from localhost.  For email sent via a localhost host connection to the SMTP server, SMTP AUTH is not required.

Without SMTP authentication, there is no log evidence to identify the compromised account.  You just see a lot of email coming from localhost.

Investigating SSH Tunnel Attacks

 

There are two clues I found with this attack

  • Email logs showing SMTP connections from localhost
  • Netstat showing SSH connecting to SMTP

 

Email from Localhost

In most attacks, either attackers either use a web application exploit or compromised user account.  These methods produce distinct signatures in the mail logs.

In the case of web application attacks, you can often correlate web logs to email logs to find the site or use PHP mail logging to identify the offending scripts.

For compromised user accounts, SMTP authentication logs will quickly reveal the problem. You will see 100’s of authentications, typically from different IP addresses.  Just change the user’s password and your done.

With the SSH tunnel attack, the logs looked like this:

Mar  3 16:05:16 psa001 postfix/smtpd[13792]: 058D82002A: client=localhost.localdomain[127.0.0.1]
Mar  3 16:05:18 psa001 postfix/cleanup[14128]: 058D82002A: message-id=<[email protected]>
Mar  3 16:05:18 psa001 postfix/qmgr[2459]: 058D82002A: from=<[email protected]>, size=405, nrcpt=1 (queue active)
Mar  3 16:05:20 psa001 postfix/smtp[14129]: 058D82002A: to=<[email protected]>, relay=rackaid.com.inbound10.mxlogic.net[208.65.145.3]:25, delay=9.4, delays=7.7/0/0.39/1.3, dsn=2.0.0,
status=sent (250 Backend Replied [e8ee4135.0.326436.00-1980.504496.p02c12m006.mxlogic.net]:  2.0.0 Ok: queued as 149162059B (Mode: n)
Mar  3 16:05:20 psa001 postfix/qmgr[2459]: 058D82002A: removed

There’s no SMTP authentication happening.  This works because the system trusts emails from localhost.

When you email from scripts (e.g. php’s mail() function) or the command line, the email is sent directly via the servers email binary program.

Message sent directly look like this:

Mar  3 16:04:40 psa001 postfix/pickup[13103]: 886D388003: uid=0 from=

Message sent via SMTP look like this:

Mar  3 16:05:16 psa001 postfix/smtpd[13792]: 058D82002A: client=localhost.localdomain[127.0.0.1]

In the second example, you will see that the email has a client associated with it:  127.0.0.1.  This will also be included in the mail headers.

This was the clue I needed.

I now know that the email is not being sent via a script’s mail function but rather, something or someone is opening a direct connection to the SMTP sever over localhost.

More Clues: Netstat & PS

If you catch the attack in progress, netstat and ps can provide further clues.

During this case, I happened to login while the attackers were sending their spam.  As a result, you could see the localhost connection to port 25 in netstat from sshd.

tcp        0      0 127.0.0.1:46298             127.0.0.1:25                ESTABLISHED 8163/sshd

Note that the remote and local connections are both localhost.  For SMTP, this is a strange connection.

Netstat gives you the process id that has the connection open.   Checking ps on that id returns:

root      8157  0.0  0.2  77680  1256 ?        Ss   14:38   0:00 /usr/sbin/sshd
root      8160  0.1  0.6 109352  4196 ?        Ss   14:38   0:00  _ sshd: jeffh [priv]
jeffh     8163  0.0  0.3 109352  2112 ?        S    14:38   0:00      _ sshd: jeffh

During this attack, we see both an SSH process as well as a local connection from SSH to SMTP.

These are two excellent clues to this type of attack.

Access Logs

I usually use the “last” command on systems to review who’s recently logged into the server.  This usually works well in most cases.  However, there’s a problem with last.

The last command uses /var/log/wtmp and not all logins are recorded in wtmp.

SSH does not log a hit into wtmp if it is a non-interactive session.   In the case of a SSH tunnel, you do not need an interactive session.  So you have to check /var/log/secure to find the logins.

Shells

As it turns out, you also do not need a valid shell to use SSH tunnels.

You can still use SSH tunneling even if the user’s shell is set to /bin/false or /sbin/nologin.  So if you need to restrict SSH use, you have toset the user-level security features in sshd.

 Preventing SSH Tunnels

Fortunately, you can easily block tunneling of ports by changing:

AllowTCPForwarding no

If you set this in SSH then try a tunnel, you can still connect to SSH but will get this result:

channel 2: open failed: administratively prohibited: open failed

So this is a quick an easy way to add a layer of security to SSH  — along with our other recommended SSH hardening changes.

Conclusion

Attackers will always find subtle ways to avoid detection.   Their goal is to use your server as much as possible, so techniques like this one make it harder to identify and fix the issue.

In my research, I only found one mention of a similar case spam using SSH port forwarding and this was in 2009.

Hopefully, this is an isolated incident.   However, I know we will start checking for this type of attack as part of our spam incident services.

标签:spam,tunnel,SMTP,垃圾邮件,ssh,email,localhost,SSH
From: https://blog.51cto.com/u_11908275/6953438

相关文章

  • openssh
    正在使用的文件在windows下无法使用ftp工具下载,就考虑在windows下建立opensshserver,然后使用sshclient来下载的方式解决。opensshforwindows安装文件及相关说明如下:http://sourceforge.net/projects/sshwindows/http://sshwindows.sourceforge.net/http://www.openssh.com/rea......
  • [8月摸鱼计划]无法将“ssh”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。
    无法将“ssh”项识别为cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次gitee生成自己的公钥之后,运行ssh-T [email protected]检测是否成功却说没办法识别ssh遇到了类似的问题在powershell会这样。所以我直接到gitbash里面去敲......
  • SSH 使用 root 权限登录 Nas
    一、启用SSH服务转到Synology设备的终端设置页面:SynologyNAS:DSM控制面板>终端机和SNMP>终端机勾选启用SSH服务。指定SSH连接的端口号并保存设置。为了确保系统安全性,建议将默认端口22替换为其他端口号。 二、sshDSM/SRM管理员帐户@DSM/SRMIP地址-pSSH......
  • [转]PuTTY的ppk密钥与OpenSSH密钥之间的相互转换
    在手机上使用juiceSSH用密钥连接服务器,以前是用win10命令行生成的私钥,使用没有问题,这次换了MobaXterm带的工具(后来发现应该是开源的PuTTY项目里的工具)生成的密钥,就登不上了,最后用文本编辑器打开两种密钥,发现格式大不一样,遂查找转换方法,找到一篇说的比较明白,特转载备用......
  • 网工应用层:电子邮件Email、SMTP协议与使用Telnet发送电子邮件
    参考资料:https://www.bilibili.com/video/BV1c4411d7jb?p=72&vd_source=e66dd25b0246f28e772d75f11c80f03c湖科大的高军老师做的计算机网络课,去年选修的计网全靠高老师,生动,举例丰富,配套实验课,无废话纯干货,单推一波!电子邮件介绍电子邮件(E-mail)是一种用电子手段提供信息交换的......
  • WebSSH之录屏安全审计(三)
    第一篇:Gin+Xterm.js实现WebSSH远程Kubernetes Pod(一)第二篇:WebSSH远程管理Linux服务器、Web终端窗口自适应(二)支持用户名密码认证支持SSH密钥认证支持Web终端窗口自适应支持录屏审计AsciinemaAsciinema是一款开源的终端会话录制工具。官网:https://asciinema.or......
  • 小米路由器R3G稳定2.28.44 固化SSH
    1.SSH参考恩山论坛的帖子SSH即可[R3G]R3G和R3GV2解锁SSH我做了实验R3G稳定2.28.44是可以一键SSH的。2.固化SSH帖子中SSH所使用的本质是OpenWRTInvasion原理是通过小米路由器的Rootshell漏洞上传二进制文件进而获取SSH及root但是所有的二进制文件全部上传于/tmp目录......
  • Ubuntu 22.04上启用SSH服务
    要在Ubuntu22.04上启用SSH服务,请按照以下步骤操作:1.打开终端,输入以下命令安装SSH服务器:sudoapt-getinstallopenssh-server2.安装完成后,SSH服务将自动启动。您可以通过以下命令检查SSH服务的状态:sudosystemctlstatusssh3.如果SSH服务未启动,请使用以下命令手动启动它......
  • Linux 上保护 SSH 服务器连接方法
    SSH是一种广泛使用的协议,用于安全地访问Linux服务器。大多数用户使用默认设置的SSH连接来连接到远程服务器。但是,不安全的默认配置也会带来各种安全风险。具有开放SSH访问权限的服务器的root帐户可能存在风险。尤其是如果你使用的是公共IP地址,则破解root密码要容易得......
  • linux终止进程案例:踢掉非法登录用户 | 关闭sshd | 杀死终端
    摘要目的:举例linux杀死进程的使用一、踢掉某个非法登录用户要求:假设tom通过ssh非法登录了,需要踢掉这个用户1.查找tom对应的sshd下面两个都可以ps-ef|grepsshdps-aux|grepsshd可以看到进程号是19342.踢掉tomkill1934//tom的sshd的进程号二、终止远程登......