masilia/clamav-bundle

此包的最新版本(v1.0.0-beta)没有提供许可证信息。

此包为Symfony和eZ Platform提供使用clamAv杀毒软件的简单病毒扫描。

安装: 0

依赖项: 0

建议者: 0

安全: 0

星级: 2

关注者: 1

分支: 0

开放性问题: 0

类型:symfony-bundle

v1.0.0-beta 2021-05-02 00:00 UTC

This package is auto-updated.

Last update: 2024-09-27 23:05:11 UTC


README

此包为Symfony和eZ Platform formBuilder提供使用clamAv进行杀毒扫描的功能。

要求

  • eZ Platform 2.5+

安装说明

  1. 添加项目仓库

    在主composer.json中添加

    "repositories": [
        // ...
        {
            "type": "vcs",
            "url": "https://github.com:masilia/clamav-bundle.git"
        }
    ],
    
  2. 通过Composer安装此包(首次安装)

    $ composer require masilia/clamav-bundle

    2.1 如果需要,通过Composer更新包

    $ composer update masilia/clamav-bundle
  3. 在您的app/AppKernel.php中激活包

    $bundles = [
        ...
        new Masilia\ClamavBundle\MasiliaClamavBundle(),
        ...
    ];
  4. 在您的服务器上安装clamAv模块

    clamav
    clamav-daemon
    clamav-freshclam
    

    4.1 在Debian发行版下,运行以下命令(根据使用的发行版进行修改)

     apt-get install clamav &&
     apt-get install clamav-daemon &&
     apt-get install clamav-freshclam

    4.2 要更新数据库,我们停止clamav-freshclam服务,然后按以下方式运行相应的命令

    /etc/init.d/clamav-freshclam stop && freshclam

    4.3 配置ClamAv服务

    • 4.3.1 使用TCP|IP协议运行clamAv。
      在文件/etc/clamav/clamd.conf中添加以下行到配置中
    TCPSocket {{port}} (3310 often we choose this one)
    TCPAddr {{server ip}} (127.0.0.1 if the clamav service is installed locally)
    

    在文件/etc/systemd/system/clamav-daemon.service.d/extend.conf中添加以下行

    ListenStream = {{ip: port}} (127.0.0.1:3310 in our example)
    
    • 或使用UNIX套接字协议运行clamAv。
      在文件/etc/clamav/clamd.conf中添加以下行
     LocalSocket: /var/run/clamav/clamd.sock
    
    • 4.3.3 完成这些配置后,您可以重新启动服务
    $ systemctl daemon-reload && /etc/init.d/clamav-daemon restart
    • 4.3.4 将clamav用户添加到www-data
      $ usermod -a -G www-data clamav && /etc/init.d/clamav-daemon restart
    • 要确保,请执行以下命令
     $ groups clamav
    • 输出
      clamav : clamav www-data
    
  • 要确保我们的clamav-daemon服务正在配置的socket ip:port或UNIX本地套接字下运行,我们执行以下命令
netstat -lpn | grep clam
  • 如果clamAv正在连接到配置的UNIX本地套接字,则输出
unix  2      [ ACC ]     STREAM     LISTENING     239406   1121/clamd           /var/run/clamav/clamd.sock
  • 如果clamAv正在连接到配置的TCP|IP套接字,则输出
TCP   0         0 127.0.0.1:3310            0.0.0.0.*           LISTEN          97/clamd
  • 如果我们配置了两种协议,则输出
unix  2      [ ACC ]     STREAM     LISTENING     239406   1121/clamd           /var/run/clamav/clamd.sock
TCP   0         0 127.0.0.1:3310            0.0.0.0.*           LISTEN          97/clamd
  1. 配置app/config/config.yml文件
  • masilia_clamav:
        #required config , 'unix:///path/to/clamav/unix_local_socket' if unix socket or 'tcp://ip:port' if tcp|ip socket>>
        socket_path: 'unix:///path/to/clamav/unix_local_socket|tcp://ip:port'
        #optional config, if you have chroot partition in your server you can the path 
        root_path: '/opt/www/project-root' ('' as default)
        #optional config, you can add it if your form is handled with ezForm Builder 
        ezform_builder_enabled: true|false (false as default)
        #optional config, if we want to scan on streaming the uploaded file
        # if the clamAv is running in the remote server , we must use the the TCP|IP protocol and enable the stream scan
        # when there issue with the scan and you get in logs the message like ('reason' => 'lstat()'), this option may resolve your issue
        enable_stream_scan: true|false (false as default value)
        #To enable validation on BO with ezbinaryfile FieldType
        enable_binary_field_type_validator: true|false (false as default value)
       

使用方法

5.1 抗病毒约束

  • 在symfony对象表单中,要启用表单上的抗病毒约束,为输入文件类型添加以下行。

    <?php
    
    use Masilia\ClamavBundle\Constraints\Antivirus;
    use Symfony\Component\Form\Extension\Core\Type\FileType;
    use Symfony\Component\Form\FormBuilder;
    
    public function buildForm(FormBuilder $builder, array $options)
    {
        // ...
        $builder->add('file',FileType::class,[
                        'constraints'=>[new Antivirus()]
                    ]);
        // ...
    }
  • 在eZ Platform表单构建器中,您只需将参数ezform_builder_enabled传递给true,然后在BO的配置文件面板上添加抗病毒验证即可。

  • 在BO中,要激活使用ezbinaryfile字段类型的扫描,只需将参数enable_binary_field_type_validator传递给true

5.4 自定义错误消息

  • 要自定义服务器错误消息,请将此消息错误键“antivirus_constraint_message”添加到消息翻译文件Resources/translations/messages.{locale}.{yml|php|xliff}
  1. 覆盖抗病毒服务 *要执行,请实现接口Masilia\ClamavBundle\Services\AntivirusInterface,并从您的服务返回包含键[status => Ok|KO][reason => 'your statement ... ']的数组
  • 然后按以下方式声明您的服务
services:
     Masilia\ClamavBundle\Services\AntiVirusInterface: '@your.antivirus.service_alias'

故障排除

如果您的日志中出现类似的消息

'status' => 'KO',   'reason' => 'file not scanned :Socket operation failed: No such file or directory (SOCKET_ENOENT)',   'originalFilename' => 'Test (4).pdf', )

与抗病毒服务器的连接失败,请确保socket_root路径正确

 'id' => '2',   'filename' => '/tmp/phpZa1j3O',   'reason' => 'lstat()',   'status' => 'failed',   'originalFilename' => 'Test (4).pdf', )

存在文件路径问题,首先检查root_path参数,如果在那种情况下问题仍然存在,流扫描是受权的,您可以在参数中启用它,这可能会解决您的问题。

 'status' => 'KO',   'reason' => 'file not scanned :Warning: file_get_contents(/var/www//tmp/php9w4fcr): failed to open stream: No such file or directory',   'originalFilename' => 'Test (4).pdf', )

检查参数root_path,可能你将文件上传的路径放置错误了