AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 819845
Accepted
FreedomRings
FreedomRings
Asked: 2016-12-10 09:38:59 +0800 CST2016-12-10 09:38:59 +0800 CST 2016-12-10 09:38:59 +0800 CST

OpenBSD 6.0 chrooted httpd 与 php 7.0 mail() 工作,但没有邮件出去?

  • 772

我从全新安装的 OpenBSD 6.0 开始,它在他们的 httpd 服务器(不是 Apache)上有一个 chroot(/var/www)。我安装了 PHP 7.0 并使用二进制安装设置了 php-fpm。在 web 根目录中同时存在 sendmail 和 femail 对象。我将一个网站移动到适当的位置,php 运行良好,php 查询 postgresql 数据库(也从二进制安装)并且一切运行良好 - 除了 mail()。

我在 /var/www/logs/php.mail.log 中设置了一个日志文件,它看到邮件被 php 识别,日志条目如下:

[09-Dec-2016 15:04:34 UTC] mail() on [/do_quick_mail_test.php:23]: To: [email protected] -- Headers: From: [email protected] (domain.com Robot)

/var/www/logs/error.log 和系统消息中都没有错误发生。

系统邮件日志中没有电子邮件指示。

当我像这样从命令行运行命令时,它可以正常工作并且邮件正常传递,没有问题:

echo 'Subject: test' | chroot /var/www /usr/sbin/sendmail -v [email protected]

我用浏览器访问的 php 程序是一个非常简单的程序:

<?php
session_start();

header( "Content-Type: text/plain" );

echo( 'Configuration Tests:'."\n" );

echo('Testing DNS:'."\n" );
print_r( dns_get_record("trialtoaster.com") );
echo( 'localhost lookup: '.gethostbyname( "localhost" )."\n" );

echo('Testing DateTime:'."\n" );
print_r( getdate() );

echo('Sending test email:'."\n" );
if ( mail("[email protected]", "PHP Test mail", "PHP email - test message.", "From: [email protected] (domain.com Robot)") ) {
        echo '- PHP thinks the email went normally.';
} else {
        echo '- PHP thinks the email failed.';
}
?>

该程序不会产生任何失败,除了 mail() 死亡。DNS 测试返回所有记录,包括 MX 记录,并且日期准确。尽管正确记录在 php 邮件日志中。

显示时, phpinfo() 正确反映了配置:

sendmail_path:  /usr/sbin/sendmail -t
SMTP:  localhost
smtp_port: 25

当我检查数据包过滤器时,它允许 lo0 上的任何东西去任何地方,当我运行命令时,我可以在 pftop 上看到它,但是当我从浏览器运行 mail() 时没有任何显示。

我已将 sendmail.ini 安装在与 chrooted sendmail 相同的目录中,这没有任何区别。

它开始看起来像 OpenBSD chrooted httpd 安装不完整,因为为了使用 php mail() 命令,有些东西完全丢失了,我担心它可能是 bash shell 和库,因为邮件从命令发送得很好线。这对我来说似乎不合时宜,因为 chroot 的目的是监禁黑客,并为被监禁的系统提供 bash shell 和库似乎有很多表面区域可以进行攻击。

我只是觉得这不是问题,因为否则,您还不如放弃 chroot 并在没有监狱的情况下运行它(看起来)。

有没有人看到我缺少的东西 - 如果没有,我在 shell 和库中进行复制,那么在不编写自定义包装器的情况下,最安全的方法是什么?

bash sendmail chroot openbsd php-fpm
  • 1 1 个回答
  • 1459 Views

1 个回答

  • Voted
  1. Best Answer
    FreedomRings
    2016-12-14T12:27:31+08:002016-12-14T12:27:31+08:00

    我相信这可以通过以下两种方式之一解决:

    (1) 您可以通过在 chroot 中安装可执行 shell 来解决此问题,以便 sendmail 二进制文件可以运行。如果你这样做了,即使你将它安装在一个包装器中,你也增加了攻击的表面积,你还不如放弃 chroot。包装器可以重新包装,您需要做的就是执行重新启动,您的系统就会被破解。我的投票是不这样做。

    (2) 最好的选择是放弃邮件并直接通过套接字使用 SMTP——这与 PHP 和 Web 服务器本身已经在运行的方式几乎相同。chroot 上没有 shell,您所做的只是安装更多 php 代码并让该代码在 localhost 上打开一个套接字到端口 25,您的 MTA 已经在该端口上侦听并传递到外部世界,但不执行任何任意代码。

    以下是它的工作原理:

    如果尚未安装 Pear,请安装它,然后安装邮件脚本。您可以像这样轻松地做到这一点:

    pkg_add install Pear
    pear install Mail_smtp
    pear install Net_SMTP
    

    根据您的系统 - 第一个 pear 安装可能会为您执行第二个安装作为依赖项。

    从那里我在自己的 php 程序中添加了一个 php 函数:

    <?php
    /**
     * Sends an email using SMTP directly rather than using the sendmail binary (which requires
     * a shell environment to run).  This allows the chrooted server to run with less exposure.
    */
    require_once "Mail.php";
    
    function SMTP_mail($recipients, $subjectHeader, $message, $fromHeader)
    {
        $headers['From']    = $fromHeader;
        $headers['Subject'] = $subjectHeader;
    
        $smtpinfo["host"] = "localhost";
        $smtpinfo["port"] = "25";
        $smtpinfo["auth"] = false;
    
        // Create the mail object using the Mail::factory method
        $mail_object = Mail::factory("smtp", $smtpinfo);
    
        $mail_object->send($recipients, $headers, $message);
    }
    

    剩下要做的就是检查我正在迁移的代码并将邮件说明转换为使用此功能:

    SMTP_mail($sendToEmailAddr, $subjectLine, $messageBody, 'From: [email protected] (Domain.com Robot)');
    

    如果您还没有这样做,那么在 chroot 之外发送邮件的能力应该已经可以工作了。重要但对我们来说是在 /etc/mail/smtp.conf 文件中:

    listen on lo0
    
    # Since we are only listening on the lo0 (local) we can safely use
    # commands that are "accept from any" or bare "accept" commands.
    
    # accept from any for domain "example.org" alias <aliases> deliver to mbox
    accept for local alias <aliases> deliver to mbox
    
    # accept from the lo0 (local) interface anything and relay it out
    accept for any relay
    
    # This was the original command - use it if you ever open up
    # the external interface by doing a "listen on any" rather than
    # the above command - that will keep us from being an open relay:
    #accept from local for any relay
    

    给它一个很好的重启,它应该可以工作 - 假设你有我在我的问题中所做的相同设置。PHP 将通过 chroot 中的 localhost 打开与 chroot 外部 localhost 的 SMTP 连接,发送您编程发送的电子邮件,然后关闭连接。OpenBSD 的 mailer.conf 将确保“真正的”sendmail (smtpctl) 获取它并根据该电子邮件地址的邮件主机的 DNS 中的 MX 条目将其路由到外部世界。您需要通过在 /etc/rc.conf.local 系统文件中设置 smtpd_flags 来确保 SMTP 正在运行。

    全部由恶魔运行,与您的程序代码一样安全。我希望这有帮助!

    • 0

相关问题

  • Mac OS X:从 python 脚本中更改 $PATH

  • Bash 脚本:要求脚本以 root 身份运行(或使用 sudo)

  • crontab ifconfig 什么都不输出

  • 使用命令行工具按排序顺序计算重复项

  • 是否有 bash 等效于 ruby​​ 的“一些内容#{foo}”?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve