Skip to content

Instantly share code, notes, and snippets.

@jaypeche
Created December 1, 2014 20:40
Show Gist options
  • Select an option

  • Save jaypeche/eea90ec04f41fb108def to your computer and use it in GitHub Desktop.

Select an option

Save jaypeche/eea90ec04f41fb108def to your computer and use it in GitHub Desktop.
clamav-realtime v0.0.2
/* This is clamav-realtime sources build with gcc-4.7.3
* Version 0.0.1 - 12 nov 2013
* Written by Jérôme PECHE(c), jaypeche@gmail.com
* Distributed under the terms of General Public License GPLv3
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/inotify.h>
#include <libnotify/notify.h>
#include <clamav.h>
#include "clamav-realtime.h"
/* Variables definition */
#define EVENT_SIZE (sizeof (struct inotify_event))
#define BUF_LEN (16 * (EVENT_SIZE + 16))
/* Main program */
int main()
{
/* Are you running as root ? */
if(geteuid() == 0) { printf("ERROR: This application does not need to be execute as root !\n");
printf(" For security, it should be run as user.\n");
exit(1); }
/*initialization : */
printf("Loading clamav-realtime process %d, please wait...\n", getpid());
/* Create inotify instance */
int fdinit;
fdinit = inotify_init();
if (fdinit < 0)
perror("inotify_not_init()"); /* Return error if not initialized */
/* Watching inotify events into*/
int wd;
wd = inotify_add_watch(fdinit, "/tmp", IN_CREATE );
if (wd < 0)
perror("inotify_add_watch"); /* Return error add_watch */
char buf[BUF_LEN]; /* Initialize events buffer */
int len;
/* ClamaAV initialization declaration */
int fd, ret;
unsigned int sigs = 0;
//unsigned long int size = 0;
//const char *virname;
//long double mb;
char *filename = "/tmp/eicar.zip";
char *icon = "/usr/share/pixmaps/clamav-realtime.png";
struct cl_engine *engine;
/* Filename exist ?! */
if((fd = open(filename, O_RDONLY)) == -1) {
printf("Can't open file %s\n", filename);
return 2;
}
/* Initialisation libClamAV */
if((ret = cl_init(CL_INIT_DEFAULT)) != CL_SUCCESS) {
printf("Can't initialize libclamav: %s\n", cl_strerror(ret));
return 2;
}
/* Send Loading Notification */
printf ("\a");
notify_init ("ClamAV Realtime loading...");
NotifyNotification * Loading = notify_notification_new ("ClamAV Realtime\n", "Loading...", icon);
notify_notification_show (Loading, NULL);
/* New engine Test */
if(!(engine = cl_engine_new())) {
printf("Can't create new engine\n");
return 2;
}
/* load all available databases from default directory */
if((ret = cl_load(cl_retdbdir(), engine, &sigs, CL_DB_STDOPT)) != CL_SUCCESS) {
printf("cl_load: %s\n", cl_strerror(ret));
close(fd);
cl_engine_free(engine);
return 2;
}
printf("Loaded %u signatures.\n", sigs);
/* build engine */
if((ret = cl_engine_compile(engine)) != CL_SUCCESS) {
printf("Database initialization error: %s\n", cl_strerror(ret));;
cl_engine_free(engine);
close(fd);
return 2;
} else {
printf("Engine initialization success !\n");
}
loop:
len = read( fdinit, buf, BUF_LEN ); /* read to determine the event change happens on directory */
if ( len < 0 ) {
perror( "length read error" ); /* Return length error */
}
/* Actually read return the list of change events happens.
Here, read the change event one by one and process it
accordingly.*/
int i = 0;
while (i < len)
{
struct inotify_event *event;
event = (struct inotify_event *) &buf[i];
if (event->mask & IN_CREATE)
//printf("file created %s", event->name);
/* Send Notification */
printf ("\a");
notify_init ("ClamAV Realtime");
NotifyNotification * nothreat = notify_notification_new ("ClamAV Realtime", "No threat found !", icon);
notify_notification_show (nothreat, NULL);
if (event->len)
printf("Scanning file : %s\n", event->name);
i += EVENT_SIZE + event->len;
}
goto loop; // loop not clean!
return 0;
}
/*
clamav-realtime.h
-----
This is ClamAV realtime scanner headers
*/
void init_clam();
void init_inotify();
void recursive_loop();
void scan();
void notify();
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <clamav.h>
int main(int argc, char **argv)
{
int fd, ret;
unsigned long int size = 0;
unsigned int sigs = 0;
long double mb;
const char *virname;
struct cl_engine *engine;
if(argc != 2) {
printf("Usage: %s file\n", argv[0]);
return 2;
}
if((fd = open(argv[1], O_RDONLY)) == -1) {
printf("Can't open file %s\n", argv[1]);
return 2;
}
if((ret = cl_init(CL_INIT_DEFAULT)) != CL_SUCCESS) {
printf("Can't initialize libclamav: %s\n", cl_strerror(ret));
return 2;
}
if(!(engine = cl_engine_new())) {
printf("Can't create new engine\n");
return 2;
}
/* load all available databases from default directory */
if((ret = cl_load(cl_retdbdir(), engine, &sigs, CL_DB_STDOPT)) != CL_SUCCESS) {
printf("cl_load: %s\n", cl_strerror(ret));
close(fd);
cl_engine_free(engine);
return 2;
}
printf("Loaded %u signatures.\n", sigs);
/* build engine */
if((ret = cl_engine_compile(engine)) != CL_SUCCESS) {
printf("Database initialization error: %s\n", cl_strerror(ret));;
cl_engine_free(engine);
close(fd);
return 2;
}
/* scan file descriptor */
if((ret = cl_scandesc(fd, &virname, &size, engine, CL_SCAN_STDOPT)) == CL_VIRUS) {
printf("Virus detected: %s\n", virname);
} else {
if(ret == CL_CLEAN) {
printf("No virus detected.\n");
} else {
printf("Error: %s\n", cl_strerror(ret));
cl_engine_free(engine);
close(fd);
return 2;
}
}
close(fd);
/* free memory */
cl_engine_free(engine);
/* calculate size of scanned data */
mb = size * (CL_COUNT_PRECISION / 1024) / 1024.0;
printf("Data scanned: %2.2Lf MB\n", mb);
return ret == CL_VIRUS ? 1 : 0;
}
# Makefile for clamav-realtime v0.0.2
# Including platform definitions
NAME=clamav-realtime
VERSION=0.0.2
include Makefile.defs
CC= gcc
STD= _GNU_SOURCE
OBJS= $(NAME).o
LIBS= -lnotify -lclamav
PKGCONFIG= `pkg-config --cflags --libs libnotify libclamav`
.c.o:
@echo Compiling sources...
$(CC) -c -Wall $(CFLAGS) -D$(STD) $(PKGCONFIG) $<
build: $(OBJS)
@echo Building executable...
$(CC) $(OBJS) $(LDFLAGS) $(LIBS) -o $(NAME)
all: build install clean
@echo $(NAME) was successfully installed !
install:
@echo Installing files...
install -d $(DESTDIR)/$(PREFIX)
@chmod 655 $(NAME)
@cp $(NAME) $(DESTDIR)/$(PREFIX)
install -d $(DESTDIR)/$(PIXMAPS)
@cp files/$(NAME).png $(DESTDIR)/$(PIXMAPS)
install -d $(DESTDIR)/$(APPS)
@cp files/$(NAME).desktop $(DESTDIR)/$(APPS)
install -d $(DESTDIR)/$(DOCDIR)/$(NAME)-$(VERSION)
for doc in $(DOCFILES); do \
cp $$doc $(DESTDIR)/$(DOCDIR)/$(NAME)-$(VERSION); \
bzip2 -9 $(DESTDIR)/$(DOCDIR)/$(NAME)-$(VERSION)/$$doc; \
done
clean:
@echo Cleaning sources...
rm -f $(NAME) *.o core
clobber: clean
@echo Uninstall application...
rm -f $(PREFIX)/$(NAME)
rm -f $(PIXMAPS)/$(NAME).png
rm -f $(APPS)/$(NAME).desktop
[ -e $(DOCDIR)/$(NAME)-$(VERSION) ] && rm -Rf $(DOCDIR)/$(NAME)-$(VERSION);
@echo $(NAME) was successfully uninstalled !
# Makefile.defs for clamav-realtime, Gentoo Linux specs.
OS=$(shell uname)
DESTDIR="${D}"
PREFIX=/usr/local/bin
PIXMAPS=/usr/share/pixmaps
APPS=/usr/share/applications
DOCDIR=/usr/share/doc
DOCFILES=ChangeLog Copying INSTALL README TODO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment