Coder Social home page Coder Social logo

kissit / php-clamav-scan Goto Github PK

View Code? Open in Web Editor NEW
40.0 9.0 16.0 10 KB

A simple PHP library for ClamAV that is CodeIgniter compatible

Home Page: https://kissitconsulting.com/blog/post/php-clamav-scan-a-simple-library-for-scanning-files-with-clamav-from-php

License: BSD 2-Clause "Simplified" License

PHP 100.00%
php clamav codeigniter

php-clamav-scan's Introduction

php-clamav-scan

A simple PHP class for scanning files using a LOCAL ClamAV/clamd install either via a socket file or network socket (windows). Can either be used on its own or dropped into a Codeigniter app as a library. The main reason this was created was because the legacy php-clamav module is not compatible with PHP 7 and all other options I found were either not drop in compatible with CodeIgniter or were designed for use with Composer. Such a trivial task/library should not be that difficult.

As previously mentioned, the files are scanned using their full path so clamd needs to be run on the web server. Even when using a network socket, the class does not handle the streaming of files over the network in a client/server model(yet). I didn't have a need to do scans over the network but support could easily be added. That being said, it requires the PHP Sockets module be installed. If not already present you must install it based on your system.

To use this on Windows, you must use the network socket option. As far as I'm aware the Windows version of Clamd does not support sockets.

Example standalone usage:

  • Include the class
require 'Clamav.php';
  • Instantiate an object.
$clamav = new Clamav();
  • If you need to customize the Clamd socket path you can do so
$clamav = new Clamav(array('clamd_sock' => '/path/to/clamd.sock'));
  • Scan a file
if($clamav->scan("/path/to/file/to/scan.txt")) {
    echo "YAY, file is safe\n";
} else {
    echo "BOO, file is a virus.  Message: " . $clamav->getMessage() . "\n";
}

Example CodeIgniter Usage

  • Download the file into your application/libraries directory
  • Load the library where desired
$this->load->library('clamav');
  • If you need to customize the Clamd socket path you can do so
$this->load->library('clamav', array('clamd_sock' => '/path/to/clamd.sock'));
  • Scan a file
if($this->clamav->scan("/path/to/file/to/scan.txt")) {
    echo "YAY, file is safe\n";
} else {
    echo "BOO, file is a virus.  Message: " . $clamav->getMessage() . "\n";
}

Example Network Socket Usage

Everything is the same to use a network socket other than you must pass in at least the IP when instantiating the object. You can also include an optional port if you are using something other than the standard 3310.

$clamav = new Clamav(array('clamd_sock' => '/path/to/clamd.sock'));

php-clamav-scan's People

Contributors

kissit avatar quigleymd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-clamav-scan's Issues

socket_connect(): unable to connect [10061]

In Clamav.php at line - 33, I changed the path from "private $clamd_sock = "/var/run/clamav/clamd.sock";" to
private $clamd_sock = "/var/run/clamav/clamd.sock";

I have installed ClamAV using .exe at "C:\Program Files\ClamAV". The "clamd.sock" is being seen there when I tried to execute the code give in the URL - "http://localhost/test/scan/php-clamav-scan-master/test.php". I am using windows 10.
I am getting the error as follows:

Testing using the local socket option... Testing a good file...
Warning: socket_connect(): unable to connect [10061]: No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\test\scan\php-clamav-scan-master\Clamav.php on line 68
BOO, file is a virus. Message: Scan failed Testing a bad file...
Warning: socket_connect(): unable to connect [10061]: No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\test\scan\php-clamav-scan-master\Clamav.php on line 68
BOO, file is a virus. Message: Scan failed

Please guide. I shall be grateful to your kind reply.

php extension

does this need a php extension? like an .so file?

permission denied to read file

Hey, thanks for great code.

I'm stock at trying to scan a file, i'm using your test script to test, but I can't seem to figure out which permissions I need to set for the upload folder I need to scan for virus.

I don't use the tmp folder, I use another folder which is used by my app for uploads. I can fine create the 2 test files (bad and good file), but I can't get the script to read the file after it's been created by PHP. What permissions do I need to set? which user/group needs to have permissions?

Thanks,

OS: Ubuntu 20.04
PHP: v7

/note: I can fine scan the files when doing from CLI, so I would guess that clamav has permissions to read the file, however the script doesn't. Is it because it done thru the socket?

clamd.sock file not found

Hi,

I am trying to implement this library but I am not able to locate clamd.sock file inside path /var/run/clamav/.
I have also checked the clamd.conf file and able to locate LocalSocket as /var/run/clamav/clamd.ctl so I have tried with clamd.ctl in the Clamav.php file but getting issue as UNKNOWN COMMAND after scanning for test file.

I have also tried changing of LocalSocket as /var/run/clamav/clamd.sock but its not creating this file and getting file is missing error.

Can you please help me with this?

Thank you.

Since last php-fpm upgrade (5.6 & 7.3), clamav timeout on PDF files

Hello,

Since last php-fpm upgrade (5.6 & 7.3), clamav timeout on PDF files when I upload a file trough PHP and php-clamav-scan :

clamd[588]: LibClamAV debug: yara_exec: executing 255
clamd[588]: LibClamAV debug: hashtab: Freeing hashset, elements: 0, capacity: 0
clamd[588]: LibClamAV debug: Descriptor[13]: CL_ETIMEOUT: Time limit reached
clamd[588]: LibClamAV debug: cli_magic_scandesc: returning 0 at line 3202
clamd[588]: Tue Oct 27 16:19:03 2020 -> $Closed fd 13
clamd[588]: Tue Oct 27 16:19:03 2020 -> $Finished scanthread
clamd[588]: Tue Oct 27 16:19:03 2020 -> $Scanthread: connection shut down (FD 12)
clamd[588]: Tue Oct 27 16:19:03 2020 -> $THRMGR: queue (single) crossed low threshold -> signaling
clamd[588]: Tue Oct 27 16:19:03 2020 -> $THRMGR: queue (bulk) crossed low threshold -> signaling

got this on a simple 2 mo PDF file and on each large PDF files (exemple, for something less than ~300 ko, no problem)

But got no problem on image large sizes (more than 5 mo)

Any idea ? Why a timeout (CL_ETIMEOUT: Time limit reached) with PDF file and no with images files ?

unable to connect

Not work for me. I got always error:
socket_connect (): unable to connect [10022]: An invalid argument was entered.

With my normal code:
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
if(socket_connect($socket, '/var/run/clamav/clamd.sock')) {
return $socket;
}
My server Centos7 and clamav normal work. File clamd.sock exists.

Put this code in xampp and the operating system is windows 10

Set
$test_dir = 'C:\xampp\htdocs\test\scan\php-clamav-scan-master';

Output:
Testing using the local socket option... Testing a good file...
Fatal error: Uncaught Error: Call to undefined function socket_create() in C:\xampp\htdocs\test\scan\php-clamav-scan-master\Clamav.php:66 Stack trace: #0 C:\xampp\htdocs\test\scan\php-clamav-scan-master\Clamav.php(148): Clamav->socket() #1 C:\xampp\htdocs\test\scan\php-clamav-scan-master\Clamav.php(91): Clamav->send('SCAN C:\xampp\h...') #2 C:\xampp\htdocs\test\scan\php-clamav-scan-master\test.php(17): Clamav->scan('C:\xampp\htdocs...') #3 {main} thrown in C:\xampp\htdocs\test\scan\php-clamav-scan-master\Clamav.php on line 66

Please help, how to execute this program

Cannot find socket file

I am getting,

Warning: socket_connect(): unable to connect [2]: No such file or directory in clamav.php on line 48

How can I find default socket path for ClamAV?

Environment - Ubuntu 16.04/PHP7/NGINX

how to pass clamd commands ?

Hello

it's more a question than a issue report . Suppose I want execute the scan without using unofficial clamd db and I would scan archives too, how can I pass these settings using the php-clamav-scan script ?

Thank you

ClamAV cannot find the file for analysis

I am creating the file via PHP, however, I always get the message No such file or directory. ERROR.

In the ClamAV log appears this message File path check failure for /tmp/test.txt. I have AppArmor installed, but in our log we didn't see anything.

Is there any additional parameter I should pass along with the SCAN $file command?

PHP 5.6

Will this work on PHP 5.6?

Some improvements :)

Hello,

I found a few little improvements to do :

  • There's a var_dump
  • return $ping == 'PONG'; is better
  • the code doesn't seem to meet PHP Coding Standards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.