14 Commits

Author SHA1 Message Date
ca110us
3ef7191ecf Merge pull request #2 from ndrpnt/main
Make Free() return Go error instead of C error code
2022-04-22 15:11:54 +08:00
ca110us
817805f598 Merge pull request #1 from ndrpnt/fix-scanmap
Fix ScanMapCB success return value
2022-04-22 15:06:47 +08:00
ndrpnt
cfcfbd4360 Make Free() return Go error instead of C error code 2022-04-21 11:10:10 +02:00
ndrpnt
69c0d2a4e1 Fix ScanMapCB success return value 2022-04-20 16:13:14 +02:00
ca110us
690024f64a modify .gitattributes 2022-03-30 15:00:44 +08:00
ca110us
d8621d034e add .gitattributes 2022-03-30 14:59:59 +08:00
ca110us
946c4b64ba modify README 2022-03-30 14:39:03 +08:00
ca110us
4fee3e0db2 move prepare.sh 2022-03-30 14:21:30 +08:00
ca110us
23c4b7a54c add Static build 2022-03-30 14:12:40 +08:00
ca110us
7c10d28065 add LDFLAGS 2022-03-24 11:38:25 +08:00
ca110us
4ecab4cd20 modify README 2022-03-24 10:16:14 +08:00
edboffical
ca818e199a fix some mistake 2022-03-23 22:20:59 +08:00
edboffical
0a5f669442 modify example & README 2022-03-23 21:30:38 +08:00
edboffical
b56ed1c270 modify README 2022-03-23 21:22:45 +08:00
7 changed files with 268 additions and 27 deletions

1
.gitattributes vendored Normal file
View File

@@ -0,0 +1 @@
*.yar linguist-vendored

View File

@@ -1,4 +1,5 @@
# go-clamav # go-clamav
[![GoDoc](https://pkg.go.dev/badge/github.com/ca110us/go-clamav?status.svg)](https://pkg.go.dev/github.com/ca110us/go-clamav?tab=doc)
go-clamav is go wrapper for [libclamav](https://docs.clamav.net/manual/Development/libclamav.html) go-clamav is go wrapper for [libclamav](https://docs.clamav.net/manual/Development/libclamav.html)
@@ -14,8 +15,6 @@ apt-get update && apt-get install -y \
libncurses5-dev libpcre2-dev libssl-dev libxml2-dev zlib1g-dev libncurses5-dev libpcre2-dev libssl-dev libxml2-dev zlib1g-dev
python3 -m pip install --user cmake / apt-get install cmake python3 -m pip install --user cmake / apt-get install cmake
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
``` ```
Download the source from the clamav [downloads page](https://www.clamav.net/downloads) Download the source from the clamav [downloads page](https://www.clamav.net/downloads)
@@ -35,7 +34,86 @@ sudo cmake --build . --target install
For other Linux distributions, see [clamav documentation](https://docs.clamav.net/manual/Installing/Installing-from-source-Unix.html) For other Linux distributions, see [clamav documentation](https://docs.clamav.net/manual/Installing/Installing-from-source-Unix.html)
## Quick Start ## Quick Start
Refer to the `example` directory ### Dynamic linking
```bash
$ cd example && cat main.go
```
```go
package main
import (
"fmt"
clamav "github.com/ca110us/go-clamav"
)
func main() {
// new clamav instance
c := new(clamav.Clamav)
err := c.Init(clamav.SCAN_OPTIONS{
General: 0,
Parse: clamav.CL_SCAN_PARSE_ARCHIVE | clamav.CL_SCAN_PARSE_ELF,
Heuristic: 0,
Mail: 0,
Dev: 0,
})
if err != nil {
panic(err)
}
// free clamav memory
defer c.Free()
// load db
signo, err := c.LoadDB("./db", uint(clamav.CL_DB_DIRECTORY))
if err != nil {
panic(err)
}
fmt.Println("db load succeed:", signo)
// compile engine
err = c.CompileEngine()
if err != nil {
panic(err)
}
c.EngineSetNum(clamav.CL_ENGINE_MAX_SCANSIZE, 1024*1024*40)
c.EngineSetNum(clamav.CL_ENGINE_MAX_SCANTIME, 9000)
// fmt.Println(c.EngineGetNum(clamav.CL_ENGINE_MAX_SCANSIZE))
// scan
scanned, virusName, ret := c.ScanFile("./test_file/nmap")
fmt.Println(scanned, virusName, ret)
}
```
```bash
$ CGO_LDFLAGS="-L/usr/local/lib -lclamav" go run main.go
db load succeed: 9263
209 YARA.Unix_Packer_UpxDetail.UNOFFICIAL Virus(es) detected
```
If the `libclamav.so` file is not found, try it:
```bash
$ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib CGO_LDFLAGS="-L/usr/local/lib -lclamav" go run main.go
db load succeed: 9263
209 YARA.Unix_Packer_UpxDetail.UNOFFICIAL Virus(es) detected
```
### Static build
```bash
$ sudo bash ./prepare.sh
$ SRCDIR=$(pwd)
$ export CGO_CFLAGS="-g -Wall -I${SRCDIR}/clamav-mussels-cookbook/mussels/install/include"
$ export CGO_LDFLAGS="-L${SRCDIR}/clamav-mussels-cookbook/mussels/install/lib -lclamav_static -lbz2_static -lclammspack_static -lclamunrar_iface_static -lclamunrar_static -lcrypto -ljson-c -lpcre2-8 -lpcre2-posix -lssl -lxml2 -lz -lm -ldl -lstdc++"
$ CGO_ENABLED=1 go build --ldflags '--extldflags "-static -fpic"' main.go
```
## Reference ## Reference
[mirtchovski/clamav](https://github.com/mirtchovski/clamav) [mirtchovski/clamav](https://github.com/mirtchovski/clamav)

View File

@@ -1,4 +1,5 @@
# go-clamav # go-clamav
[![GoDoc](https://pkg.go.dev/badge/github.com/ca110us/go-clamav?status.svg)](https://pkg.go.dev/github.com/ca110us/go-clamav?tab=doc)
go-clamav 是 go 语言对 [libclamav](https://docs.clamav.net/manual/Development/libclamav.html) 的封装 go-clamav 是 go 语言对 [libclamav](https://docs.clamav.net/manual/Development/libclamav.html) 的封装
@@ -14,11 +15,9 @@ apt-get update && apt-get install -y \
libncurses5-dev libpcre2-dev libssl-dev libxml2-dev zlib1g-dev libncurses5-dev libpcre2-dev libssl-dev libxml2-dev zlib1g-dev
python3 -m pip install --user cmake / apt-get install cmake python3 -m pip install --user cmake / apt-get install cmake
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
``` ```
Download the source from the clamav [downloads page](https://www.clamav.net/downloads) 从 clamav 官方下载源码 [downloads page](https://www.clamav.net/downloads)
```bash ```bash
tar xzf clamav-[ver].tar.gz tar xzf clamav-[ver].tar.gz
@@ -35,7 +34,85 @@ sudo cmake --build . --target install
其他 Linux 发行版参照 [clamav documentation](https://docs.clamav.net/manual/Installing/Installing-from-source-Unix.html) 其他 Linux 发行版参照 [clamav documentation](https://docs.clamav.net/manual/Installing/Installing-from-source-Unix.html)
## 快速开始 ## 快速开始
参考 `example` 目录 ### 动态链接
```bash
$ cd example && cat main.go
```
```go
package main
import (
"fmt"
clamav "github.com/ca110us/go-clamav"
)
func main() {
// new clamav instance
c := new(clamav.Clamav)
err := c.Init(clamav.SCAN_OPTIONS{
General: 0,
Parse: clamav.CL_SCAN_PARSE_ARCHIVE | clamav.CL_SCAN_PARSE_ELF,
Heuristic: 0,
Mail: 0,
Dev: 0,
})
if err != nil {
panic(err)
}
// free clamav memory
defer c.Free()
// load db
signo, err := c.LoadDB("./db", uint(clamav.CL_DB_DIRECTORY))
if err != nil {
panic(err)
}
fmt.Println("db load succeed:", signo)
// compile engine
err = c.CompileEngine()
if err != nil {
panic(err)
}
c.EngineSetNum(clamav.CL_ENGINE_MAX_SCANSIZE, 1024*1024*40)
c.EngineSetNum(clamav.CL_ENGINE_MAX_SCANTIME, 9000)
// fmt.Println(c.EngineGetNum(clamav.CL_ENGINE_MAX_SCANSIZE))
// scan
scanned, virusName, ret := c.ScanFile("./test_file/nmap")
fmt.Println(scanned, virusName, ret)
}
```
```bash
$ CGO_LDFLAGS="-L/usr/local/lib -lclamav" go run main.go
db load succeed: 9263
209 YARA.Unix_Packer_UpxDetail.UNOFFICIAL Virus(es) detected
```
如果找不到 `libclamav.so` 文件,尝试如下:
```bash
$ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib CGO_LDFLAGS="-L/usr/local/lib -lclamav" go run main.go
db load succeed: 9263
209 YARA.Unix_Packer_UpxDetail.UNOFFICIAL Virus(es) detected
```
### 静态编译
```bash
$ sudo bash ./prepare.sh
$ SRCDIR=$(pwd)
$ export CGO_CFLAGS="-g -Wall -I${SRCDIR}/clamav-mussels-cookbook/mussels/install/include"
$ export CGO_LDFLAGS="-L${SRCDIR}/clamav-mussels-cookbook/mussels/install/lib -lclamav_static -lbz2_static -lclammspack_static -lclamunrar_iface_static -lclamunrar_static -lcrypto -ljson-c -lpcre2-8 -lpcre2-posix -lssl -lxml2 -lz -lm -ldl -lstdc++"
$ CGO_ENABLED=1 go build --ldflags '--extldflags "-static -fpic"' main.go
```
## 参考 ## 参考
[mirtchovski/clamav](https://github.com/mirtchovski/clamav) [mirtchovski/clamav](https://github.com/mirtchovski/clamav)

View File

@@ -5,9 +5,6 @@
package goclamav package goclamav
/* /*
#cgo CFLAGS: -g -Wall
#cgo LDFLAGS: -lclamav
#include <clamav.h> #include <clamav.h>
#include <stdlib.h> #include <stdlib.h>
*/ */
@@ -149,7 +146,7 @@ func (c *Clamav) CompileEngine() error {
return nil return nil
} }
// SetNum sets a number in the specified field of the engine configuration. // EngineSetNum sets a number in the specified field of the engine configuration.
// Certain fields accept only 32-bit numbers, silently truncating the higher bits // Certain fields accept only 32-bit numbers, silently truncating the higher bits
// of the engine config. See dat.go for more information. // of the engine config. See dat.go for more information.
func (c *Clamav) EngineSetNum(field EngineField, num uint64) error { func (c *Clamav) EngineSetNum(field EngineField, num uint64) error {
@@ -162,7 +159,7 @@ func (c *Clamav) EngineSetNum(field EngineField, num uint64) error {
return nil return nil
} }
// GetNum acquires a number from the specified field of the engine configuration. Tests show that // EngineGetNum acquires a number from the specified field of the engine configuration. Tests show that
// the ClamAV library will not overflow 32-bit fields, so a GetNum on a 32-bit field can safely be // the ClamAV library will not overflow 32-bit fields, so a GetNum on a 32-bit field can safely be
// cast to uint32. // cast to uint32.
func (c *Clamav) EngineGetNum(field EngineField) (uint64, error) { func (c *Clamav) EngineGetNum(field EngineField) (uint64, error) {
@@ -179,11 +176,15 @@ func (c *Clamav) EngineGetNum(field EngineField) (uint64, error) {
// Free the memory allocated to clamav instance, Free should be called // Free the memory allocated to clamav instance, Free should be called
// when the engine is no longer in use. // when the engine is no longer in use.
func (c *Clamav) Free() int { func (c *Clamav) Free() error {
return int(C.cl_engine_free((*C.struct_cl_engine)(c.engine))) ret := ErrorCode(C.cl_engine_free((*C.struct_cl_engine)(c.engine)))
if ret == CL_SUCCESS {
return nil
}
return Strerr(ret)
} }
// ScanMapCb scans custom data // ScanMapCB scans custom data
func (c *Clamav) ScanMapCB(fmap *Fmap, fileName string, context interface{}) (uint, string, error) { func (c *Clamav) ScanMapCB(fmap *Fmap, fileName string, context interface{}) (uint, string, error) {
var scanned C.ulong var scanned C.ulong
var virusName *C.char var virusName *C.char
@@ -201,7 +202,7 @@ func (c *Clamav) ScanMapCB(fmap *Fmap, fileName string, context interface{}) (ui
defer CloseMemory(fmap) defer CloseMemory(fmap)
// clean // clean
if ret == CL_SUCCESS { if ret == CL_SUCCESS {
return 0, "", nil return uint(scanned), "", nil
} }
// virus // virus
if ret == CL_VIRUS { if ret == CL_VIRUS {
@@ -235,7 +236,7 @@ func (c *Clamav) ScanFile(path string) (uint, string, error) {
return 0, "", Strerr(ret) return 0, "", Strerr(ret)
} }
// ScanFileCb scans a single file for viruses using the ClamAV databases and using callbacks from // ScanFileCB scans a single file for viruses using the ClamAV databases and using callbacks from
// ClamAV to read/resolve file data. The callbacks can be used to scan files in memory, to scan multiple // ClamAV to read/resolve file data. The callbacks can be used to scan files in memory, to scan multiple
// files inside archives, etc. The function returns the number of bytes // files inside archives, etc. The function returns the number of bytes
// read from the file (if found), the virus name and an error code. // read from the file (if found), the virus name and an error code.

View File

@@ -1,9 +1,6 @@
package goclamav package goclamav
/* /*
#cgo CFLAGS: -g -Wall
#cgo LDFLAGS: -lclamav
#include <clamav.h> #include <clamav.h>
#include <stdlib.h> #include <stdlib.h>
*/ */

View File

@@ -38,14 +38,10 @@ func main() {
} }
c.EngineSetNum(clamav.CL_ENGINE_MAX_SCANSIZE, 1024*1024*40) c.EngineSetNum(clamav.CL_ENGINE_MAX_SCANSIZE, 1024*1024*40)
c.EngineSetNum(clamav.CL_ENGINE_PCRE_MAX_FILESIZE, 1024*1024*20)
c.EngineSetNum(clamav.CL_ENGINE_MAX_SCANTIME, 9000) c.EngineSetNum(clamav.CL_ENGINE_MAX_SCANTIME, 9000)
c.EngineSetNum(clamav.CL_ENGINE_PCRE_MATCH_LIMIT, 1000) // fmt.Println(c.EngineGetNum(clamav.CL_ENGINE_MAX_SCANSIZE))
c.EngineSetNum(clamav.CL_ENGINE_PCRE_RECMATCH_LIMIT, 500)
// fmt.Println(c.EngineGetNum(clamav.CL_ENGINE_PCRE_RECMATCH_LIMIT))
// scan // scan
scanned, msg, err := c.ScanFile("./test_file/nmap") scanned, virusName, ret := c.ScanFile("./test_file/nmap")
fmt.Println(scanned, msg, err) fmt.Println(scanned, virusName, ret)
} }

91
example/prepare.sh Normal file
View File

@@ -0,0 +1,91 @@
#!/bin/bash
# make static lib for clamav deps
apt-get update
apt-get install -y python3-pip
apt-get install -y build-essential clang llvm
apt-get install -y libstdc++6 libstdc++-6-dev
apt-get install -y flex bison python3-dev pkg-config ninja-build
python3 -m pip install mussels
wget https://cmake.org/files/v3.21/cmake-3.21.5-linux-x86_64.tar.gz
tar -zxvf cmake-3.21.5-linux-x86_64.tar.gz -C /usr/local/
ln -s /usr/local/cmake-3.21.5-linux-x86_64/bin/cmake /usr/bin/cmake
rm -rf cmake-3.21.5-linux-x86_64.tar.gz
git clone --depth 1 https://github.com/ca110us/clamav-mussels-cookbook.git
cd clamav-mussels-cookbook
rm -rf mussels/* &> /dev/null
mkdir mussels &> /dev/null
msl build libclamav_deps -t host-static -w mussels/work -i mussels/install
cd -
# make get clamav source code
git clone https://github.com/Cisco-Talos/clamav.git
cd clamav
git checkout clamav-0.104.0
cd -
# libclamav
cd clamav
rm -rf ./build/* &> /dev/null
mkdir build &> /dev/null
cd -
export CLAMAV_DEPENDENCIES="$(pwd)/clamav-mussels-cookbook/mussels/install/"
cd clamav/build
cmake .. -G Ninja \
-DCMAKE_BUILD_TYPE="Release" \
-DJSONC_INCLUDE_DIR="$CLAMAV_DEPENDENCIES/include/json-c" \
-DJSONC_LIBRARY="$CLAMAV_DEPENDENCIES/lib/libjson-c.a" \
-DBZIP2_INCLUDE_DIR="$CLAMAV_DEPENDENCIES/include" \
-DBZIP2_LIBRARY_RELEASE="$CLAMAV_DEPENDENCIES/lib/libbz2_static.a" \
-DOPENSSL_ROOT_DIR="$CLAMAV_DEPENDENCIES" \
-DOPENSSL_INCLUDE_DIR="$CLAMAV_DEPENDENCIES/include" \
-DOPENSSL_CRYPTO_LIBRARY="$CLAMAV_DEPENDENCIES/lib/libcrypto.a" \
-DOPENSSL_SSL_LIBRARY="$CLAMAV_DEPENDENCIES/lib/libssl.a" \
-DLIBXML2_INCLUDE_DIR="$CLAMAV_DEPENDENCIES/include/libxml2" \
-DLIBXML2_LIBRARY="$CLAMAV_DEPENDENCIES/lib/libxml2.a" \
-DPCRE2_INCLUDE_DIR="$CLAMAV_DEPENDENCIES/include" \
-DPCRE2_LIBRARY="$CLAMAV_DEPENDENCIES/lib/libpcre2-8.a" \
-DZLIB_INCLUDE_DIR="$CLAMAV_DEPENDENCIES/include" \
-DZLIB_LIBRARY="$CLAMAV_DEPENDENCIES/lib/libz.a" \
-DENABLE_JSON_SHARED=OFF \
-DENABLE_STATIC_LIB=ON \
-DENABLE_SYSTEMD=OFF \
-DENABLE_TESTS=OFF \
-DENABLE_LIBCLAMAV_ONLY=ON \
-DENABLE_UNRAR=ON \
-DENABLE_SHARED_LIB=OFF \
-DDATABASE_DIRECTORY=/var/lib/clamav \
-DCMAKE_INSTALL_PREFIX=install
cmake --build .
cd -
rm -rf ./lib/*
mkdir lib &> /dev/null
cp clamav/build/libclamav/libclamav_static.a ./lib
cp clamav/build/libclammspack/libclammspack_static.a ./lib
cp clamav/build/libclamunrar/libclamunrar_static.a ./lib
cp clamav/build/libclamunrar_iface/libclamunrar_iface_static.a ./lib
cp "$CLAMAV_DEPENDENCIES/lib/libbz2_static.a" ./lib
cp "$CLAMAV_DEPENDENCIES/lib/libjson-c.a" ./lib
cp "$CLAMAV_DEPENDENCIES/lib/libcrypto.a" ./lib
cp "$CLAMAV_DEPENDENCIES/lib/libssl.a" ./lib
cp "$CLAMAV_DEPENDENCIES/lib/libxml2.a" ./lib
cp "$CLAMAV_DEPENDENCIES/lib/libpcre2-8.a" ./lib
cp "$CLAMAV_DEPENDENCIES/lib/libz.a" ./lib
rm -rf ./include/*
mkdir include &> /dev/null
cp clamav/build/*.h ./include
cp clamav/libclamav/clamav.h ./include
cp ./include/* clamav-mussels-cookbook/mussels/install/include/
cp ./lib/* clamav-mussels-cookbook/mussels/install/lib/