Sunday, 18 September 2016

Java API to detect virus in a file using ClamAv

Many of the programmers were stucks in file scanning for virus ,me also stucked in that about 2 weeks.
Actually it's not in the code we stuck...The installation of antivirus and running the service in the pc were we stuck and the lack of proper documentation of installation and running the service.

ClamAv is the only antivirus engine available as an open source till now...

On this post, i will help you guys to install clamAv in windows and use clamAv service for scanning files for virus using java

1) Download ClamAv from https://www.clamav.net/downloads
download proper .msi file for your windows version. current version for windows 64 bit (clamav-0.99.2-x64.msi)Double click and install clamAv.

2) Download ClamAv virus database from the same page
or download main.cvd from the direct link http://database.clamav.net/main.cvd
and daily.cvd from the direct link http://database.clamav.net/daily.cvd
put these two files where you install clamAv (default location will be c drive--> program files--> clamAv)

3) They will provide sample files for clamd.conf and freshclam.conf
studying these files and creating config file will be the next challenge for you..So i will give you the config files just put these file in the clamAv root directory(where clamAv is installed)

a) clamd.conf ( https://drive.google.com/file/d/0B1n0939T4he7Z2xnME0tZFdMazg/view?usp=sharing )
b) freshclam.conf ( https://drive.google.com/file/d/0B1n0939T4he7c3YtWEZ2a3FaOWM/view?usp=sharing )
Edit the clamd.config file

DatabaseDirectory-->path of virus database files(main.cvd and daily.cvd)

TCPAddr-->your currenct ip address

4) Now run cmd as administrator
Change cmd to clamAv directory
 then run command--> clamd.exe install
That's it!!!!!!!!!This will start service of clamAv in your system

5) Now scan the file using java

Download these third party jars
a) org.apache.commons.logging-1.1.1.jar
b) commons-io-2.0.jar
c) libclamav-1.0.jar ( https://drive.google.com/file/d/0B1n0939T4he7d1JyYnE2RlB1OGc/view?usp=sharing )
1)
ClamAVFileScan.java 

import java.io.FileInputStream;
import java.io.InputStream;

import net.taldius.clamav.ClamAVScanner;
import net.taldius.clamav.ClamAVScannerFactory;

/**
 * Class to scan files using ClamAV antivirus APIs.
 *
 * @author Aneesh T.G
 *
 */
public class ClamAVFileScan {
private ClamAVScanner scanner;
public static void main(String args[]){
boolean scanResult=false;
ClamAVFileScan clamAVFileScan=new ClamAVFileScan();
try {
clamAVFileScan.initScanner();
scanResult=clamAVFileScan.fileScanner("C:\\Users\\Aneesh.T.G\\Desktop\\eicar.zip");

if(scanResult){
System.out.println("No virus found");
}
else{
System.out.println("Warning!!!! Virus found");
}
} catch (Exception e) {
e.printStackTrace();
}

}

       /**
        * Method to initialize clamAV scanner
        */
       public void initScanner(){
           
              ClamAVScannerFactory.setClamdHost("192.168.0.7"); // Host ip where 'clamd' process is running
              ClamAVScannerFactory.setClamdPort(3310); // Port on which 'clamd' process is listening
              ClamAVScannerFactory.setConnectionTimeout(20);// Connection time out to connect 'clamd' process
             
              this.scanner = ClamAVScannerFactory.getScanner();
       }

        /**
        * Method to scans files to check whether file is virus infected
        *
        * @param fileInputStream
        * @return
        * @throws Exception
        */
       public boolean fileScanner(String fileInputStream) throws Exception {

              boolean resScan = false;
              if (fileInputStream != null) {
              InputStream file=new FileInputStream(fileInputStream);
                     resScan = scanner.performScan(file);
                   
              } else {

                     throw new Exception();
              }
              return resScan;
       }

}

6) Create a test virus file or download eicar.zip and check...Happy coding!!!! :)

Friday, 16 September 2016

How to create log using java

Jar files required

1)datedFileAppender-1.0.2 (https://drive.google.com/file/d/0B1n0939T4he7ZGpBeW5NdzNXUEk/view?usp=sharing)
2)log4j-1.2.17 (https://drive.google.com/file/d/0B1n0939T4he7b2d0RFJxUXFxMzQ/view?usp=sharing)

Create a property file named log4j.properties within the project folder (outside the src)..copy paste the below content in that file..

log4j.properties file

# Root logger option


log4j.rootLogger=INFO, file, stdout
#log4j.logger.org.quartz=DEBUG
#comment

# Direct log messages to a log file
log4j.appender.file=biz.minaret.log4j.DatedFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.Prefix=logfile.
#log4j.appender.T.MaxFileSize=100MB
#log4j.appender.T.MaxBackupIndex=7
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to stdout(to show in the console)
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


LogFile.java

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author Aneesh.T.G
 */

public class LogFile {

 static Logger logger = Logger.getLogger(LogFile.class.getName());
 public static void main(String args[]) {
     PropertyConfigurator.configure("./log4j.properties");
  logger.info("This is my first log statement");
  logger.error("Sorry, something wrong");

 }
}

This will create a 'logs' folder in project directory inside that a dated log file will create