Git Product home page Git Product logo

envysec's Introduction

secEnvyronment

Content table:

  • Introducing.
  • Tutorial.
  • Developer documentation.

Introducing:

This software is used to simplify ClamAV usage and automatize detect verification using Metadefender service.

Scope:

  • Reduce number of false-positive detection;
  • Simplify user interface;
  • Provide less overloaded detection reports.

Usage cases:

  • Manual or scheduled virus-scanning;
  • Simple IP-abuse scanning.

Limitations:

  • Number of Metadefender scans are limited by one's API key. Free API key is limited to 10 scans per day.

Tutorial:

Installation guide is described in INSTALL.md file. Application does not require any special workspace preparations.

To get help use:

python3 envy_sec.py --help
python3 envy_sec.py -h

To perform file scan:

python3 envy_sec.py -S path/to/file/to/be/scann.ed
python envy_sec.py --scan-file C:\PathTo\File\ToBeScann.ed

To perform IP scan:

python3 envy_sec.py --scan-ip 8.8.8.8
python3 envy_sec.py -I 8.8.8.0\24

To perform ClamAV signatures update:

python3 envy_sec.py --update
python3 envy_sec.py -U

To send multiple arguments use space:

python3 envy_sec.py -I 8.8.8.8 9.9.9.9

Commands also might be combined:

python3 envy_sec.py --update -I 8.8.8.8 9.9.9.9 -F ./eicar.virus /some/another/file

Command execution priority:

  1. Update;
  2. Scan IP;
  3. Scan file;

Developer documentation:

This section describes how exactly does secEnvyronment works (or does not).

Briefly, how does it work:

  • User invoke secEnvyronment scan,
  • secEnvyronment open ClamAV subprocess,
  • and put all ClamAV output in queue,
  • in parallel, thread (work_thread) checks for new lines in queue,
  • and if thread finds something, it sends it to Metadefender,
  • then prints out approved or denied result.
                Approve or deny ClamAV detection,
                         output results.
             ________________________________________
            |                                        |
            |                                        |
  (1) secEnvyronment     work_thread ----------> Metadefender
            |                 |        call with
 subprocess |                 |      received from
    popen() |                 |        queue args
            |     put in      |
         ClamAV ----------> queue

             The main principal work scheme (1 - begin)

Debug it! Or troubleshooting.

All envysec stages provides some logs. Prefered way to debug is to analyse logs.

If log analyse is not enough, try to check Metadefender API availability, for example:

# For *nix:
curl 'https://api.metadefender.com/v4/ip/1.2.3.4' -H 'apikey: SOME_API_KEY'

Or

# For Windows:
$uri = 'https://api.metadefender.com/v4/ip/1.2.3.4'

$headers = @{}
$headers.Add('apikey','SOME_API_KEY')

$result = Invoke-WebRequest -Uri $uri -Headers $headers
Write-Output $result.content

All possible error codes are listed in Metadefender Errors.

Metadefender Errors:

  1. HTTP code is HTTP status code, used to control HTTP session status (like if server work or not, connection refused and etc.);
  2. Response code placed in response JSON, used to control Metadefender session status (like data validity, API key limits and etc.);
  3. Description is a response code explanation; (like why this happened);
Metadefender code Category HTTP code
1 Generic
400000 Generic error 400
400001 The caching strategy is not recognized 400
400002 The limit strategy is not supported 400
400003 The limit type is not supported 400
400004 The query parameters are not valid 400
2 Payload Validation
400020 Header is not valid 400
400021 Body parsing failed 400
400022 Payload validation has failed 400
400023 Headers are not correct 400
400024 Headers are missing 400
400025 Payload is missing or empty 400
400026 Hash in the URL doesn't match the hash value in the body 400
400027 Offset should be a positive integer 400
400028 "Limit should be a positive integer less than 10.000" 400
3 Routing Errors
400040 The requested path is not valid 400
400041 The version is required 400
400042 The version does not exist 400
400043 The requested path does not exist 400
400044 Method does not exist 400
400045 The route was not properly set up 400
400046 The requested route does not exist 400
400047 This route is available only on development environments 400
4 Hash Errors
400060 The hash field in the body is required 400
400061 The hash field is not an array 400
400062 The hash field is empty 400
400063 Exceeded maximum allowed 400
400064 The hash value is not valid 400
400065 The header include_scan_details has to be either 0 or 1 400
400066 The header file_metadata has to be either 0 or 1 400
400067 Hash update failed 400
5 Top Hash Errors
400080 The amount must be lower than 10.000 400
400081 Type must be one of clean / infected 400
400082 Period must be one of day / week / month 400
400083 Threshold must be one of 1, 2, 3, 4, 5, 6 400
6 appinfo
400100 The fields os_info.device_identity are required 400
7 Top Detection
400120 The header x-exclude-empty-file-id has to be either 0 or 1 400
400121 The header x-exclude-data has to be either 0 or 1 400
400122 The header x-threshold must be one of 3, 4, 5, 6 400
400123 Packages should be one of m1, m4, m8, m12, m16, m20, m30 400
400124 Number of hashes must be one of 10, 100, 1.000, 10.000 400
8 Upload Errors
400140 The file upload has failed 400
400141 The header x-force-scan has to be either 0 or 1 400
400142 The header x-sample-sharing has to be either 0 or 1 400
400143 Private scanning is not enabled for the provided API key 400
400144 Exceeded maximum file size allowed; maximum allowed is 200MB 400
400145 Request body is empty; please send a binary file 400
400146 Provided download URL is not valid or inaccessible 400
400147 Rescan failed. Requested file is missing from our servers. 400
400148 Requested file is a private one and cannot be rescanned 400
400149 Could not update the rescan count 400
400150 The file_ids field array in body is required 400
400151 The file_ids field is not an array 400
400152 The file_ids field is empty 400
400153 Exceeded maximum allowed 400
400154 Exceeded maximum allowed files in archive 400
9 API Key Info
400160 The API key you are trying to add already exists 400
400161 The API key could not be removed 400
400162 The API key was not updated 400
400163 The body is invalid 400
400164 No valid operation type 400
400165 No API key specified 400
400166 Please provide a valid email address 400
400167 Please provide a valid body 400
400168 Please choose another nickname, as this one contains profanities 400
10 IP Scan
400180 Invalid format of input. Provide IPv4 or IPv6. 400
400181 The ip_addresses field in body is required 400
400182 The ip_addresses field is not an array 400
400183 The ip_addresses field is empty 400
400184 Exceeded maximum allowed 400
400185 The address is not a routable IP 400
400186 No response 400
400187 Invalid response 400
11 Stats
400200 The number of days requested must be a positive integer 400
400201 Invalid objectId 400
400202 Invalid date 400
400203 Invalid outbreak report filter 400
12 Status
400210 Parameter type must be one of hashLookup / uploadFile / ipScan 400
13 Salesforce
400250 Salesforce connectivity error 401
400251 There is no record 401
14 Feed
400260 You are allowed to query up to 30 days in the past 400
400261 Invalid category. Please use: A, D, E, G, M, N, O, P, T, Z 400
15 Authentication
401000 Authentication has failed 401
401001 Authentication strategy is invalid 401
401002 Authentication strategy is not implemented 401
401003 Authorization strategy is not supported for this endpoint 401
401004 Authentication token has expired 401
401005 Authentication token is invalid 401
401006 Invalid API key 401
16 Forbidden
403000 Access forbidden 403
403001 Requested resource doesn't match your API key 403
403002 Your IP is blocked because of abuse 403
403003 Insufficient Privileges 403
17 Not Found
404000 Endpoint was not found 404
404001 Entity was not found 404
404002 There are no entries found 404
404003 The hash was not found 404
404004 The data_id was not found 404
404005 The hash information was not found 404
404006 There is no data for the selected date 404
404007 Requested file ID does not exist in our records 404
404008 The API key was not found 404
18 Payload Acceptance
406000 Content-Type header and payload has to be JSON 406
406001 Payload empty 406
19 Request Timeout
408000 Request timeout. It has reached the 60 seconds limit. 408
20 Rate Limiting
429000 API key limit exceeded; retry after the limit is reset 429
429001 Your request has been throttled; maximum 10 requests per minute per user 429
429002 Too many connections; try again later 429
21 Service Unavailable
503000 External service is not reachable 503
503001 External service is not reachable 503

envysec's People

Contributors

alex-bv avatar

Watchers

 avatar

envysec's Issues

Sqlite3 initial database creation error.

Bug overview:

sqlite3 initial database creation crashes scan.

System info:

OS: Debian 10
Python version: 3.7.3
envysec version: latest/test

Crash report:

Terminal output:

Traceback (most recent call last):
  File "envysec.py", line 496, in <module>
    envy_cli = ConsoleInterface() # class will initialize Metadefender and ClamAV automatically.
  File "envysec.py", line 66, in __init__
    self.exclude_db = sql_management.ExcludeDB(database = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'modules', 'exclude.db'))
  File "/home/user/envysec/modules/sql_management.py", line 310, in __init__
    DBManager.__init__(self, logging_level, database)
  File "/home/user/envysec/modules/sql_management.py", line 43, in __init__
    elif self.create_db() is False:
  File "/home/user/envysec/modules/sql_management.py", line 177, in create_db
    db_cursor.execute(__create_command(structure)) # SQL
  File "/home/user/envysec/modules/sql_management.py", line 160, in __create_command
    if table is not structure[-1]:
KeyError: -1

Log:

01.01.1970 12:00:00 >> secEnvyronment - DEBUG: Initialize Application...
01.01.1970 12:00:00 >> secEnvyronment - INFO: Parsing arguments...
01.01.1970 12:00:00 >> secEnvyronment - DEBUG: ...parsing succeed.
01.01.1970 12:00:00 >> secEnvyronment - INFO: Initialize Command Line Interface (CLI).
01.01.1970 12:00:00 >> EnvySec CLI - DEBUG: init: Initializing class...
01.01.1970 12:00:00 >> EnvySec CLI - DEBUG: init: Trying to get default settings...
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: init: Initializing class...
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: init: Reading settings...
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: init: Setting successfully read.
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: init: Verifying settings...
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: settings: starting settings...
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: settings: return settings.
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: settings: Trying to return new settings...
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: settings: Current settins would be returned.
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: settings: AttributeError args: ("'Envyronment_Settings' object has no attribute '_Envyronment_Settings__new_settings'",).
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: __check_metadefender_api: starting __check_metadefender_api...
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: __check_metadefender_api: Check API keys length.
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: __check_metadefender_api: API key length is valid.
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: __check_metadefender_api: Check API key chars.
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: __check_metadefender_api: API key validated.
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: init: Settings verification complete.
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: clam_config: starting clam_config...
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: clam_config: empty clam_conf;
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: clam_config: resolving ClamAV paths...
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: clam_config: detecting OS...
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: clam_config: POSIX (Linux) OS detected.
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: clam_config: ClamAV scanner detected;
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: clam_config: ClamAV scanner path: /bin/clamscan
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: clam_config: Path priority: 3
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: clam_config: ClamAV updater detected;
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: clam_config: ClamAV updater path: /bin/freshclam
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: clam_config: Path priority: 3
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: clam_config: ClamAV scanner detected;
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: clam_config: ClamAV scanner path: /usr/bin/clamscan
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: clam_config: Path priority: 1
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: clam_config: ClamAV updater detected;
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: clam_config: ClamAV updater path: /usr/bin/freshclam
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: clam_config: Path priority: 1
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: clam_config: ClamAV paths resolved.
01.01.1970 12:00:00 >> ClamAV - DEBUG: init: Initializing class...
01.01.1970 12:00:00 >> ClamAV - DEBUG: init: Class initialized.
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: settings: starting settings...
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: settings: return settings.
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: settings: Trying to return new settings...
01.01.1970 12:00:00 >> envySec SettingsEdit - INFO: settings: Current settins would be returned.
01.01.1970 12:00:00 >> envySec SettingsEdit - DEBUG: settings: AttributeError args: ("'Envyronment_Settings' object has no attribute '_Envyronment_Settings__new_settings'",).
01.01.1970 12:00:00 >> Metadefender - DEBUG: init: Initializing class...
01.01.1970 12:00:00 >> Metadefender - DEBUG: init: Metadefender API key length is OK.
01.01.1970 12:00:00 >> Metadefender - DEBUG: init: Class initialized.
01.01.1970 12:00:00 >> EnvySec CLI - DEBUG: init: Trying to find exclude database...
01.01.1970 12:00:00 >> DBManager - DEBUG: init: Initializing class...
01.01.1970 12:00:00 >> DBManager - DEBUG: init: Checking database existence...
01.01.1970 12:00:00 >> DBManager - DEBUG: create_db: Initialize create_db...
01.01.1970 12:00:00 >> DBManager - DEBUG: create_db: Checking database existence.
01.01.1970 12:00:00 >> DBManager - WARNING: create_db: Database is not found!
01.01.1970 12:00:00 >> DBManager - DEBUG: create_db: Trying to create database:
01.01.1970 12:00:00 >> DBManager - DEBUG: create_db: Connecting...
01.01.1970 12:00:00 >> DBManager - DEBUG: create_db: Init cursor...
01.01.1970 12:00:00 >> DBManager - DEBUG: create_db: Creating table...
01.01.1970 12:00:00 >> DBManager - DEBUG: create_db:__create_command: Creating command, received structure: {'Exclusion': ['Path', 'Date'], 'Statistic': ['Found', 'Date', 'TotalReports']}

Steps to reproduce:

  1. Remove modules/exclude.db;
  2. Run any command, for example: python3 envysec.py -I 1.2.3.4

Affected versions:

  • latest/master;
  • latest/test.

Initial settings error

Bug overview:

envysec fails to register Metadefender API key on first run.

System info:

OS: Windows 10
Python version: 3.9.1
envysec version: latest/test

Crash report:

Terminal output:

Input Metadefender API key: <...>
Traceback (most recent call last):
  File "C:\Users\User\Desktop\envysec\modules\envy_settings.py", line 35, in __init__
    with open(path, 'r') as settings_f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\User\\Desktop\\envysec\\settings.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\envysec\envysec.py", line 536, in <module>
    envy_cli = ConsoleInterface() # class will initialize Metadefender and ClamAV automatically.
  File "C:\Users\User\Desktop\envysec\envysec.py", line 56, in __init__
    self.envy_conf = envy_settings.Envyronment_Settings(path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'settings.json'), logging_level = logging_level)
  File "C:\Users\User\Desktop\envysec\modules\envy_settings.py", line 69, in __init__
    if self.__write_new_settings(settings = self.__new_settings, path = path) is False:
TypeError: __write_new_settings() got an unexpected keyword argument 'settings'

Log:

01.01.1970 12:00:00 - [DEBUG] - secEnvyronment - (envysec.py).(457) - Initialize Application...
01.01.1970 12:00:00 - [INFO] - secEnvyronment - (envysec.py).(528) - Parsing arguments...
01.01.1970 12:00:00 - [DEBUG] - secEnvyronment - (envysec.py).(457) - Initialize Application...
01.01.1970 12:00:00 - [INFO] - secEnvyronment - (envysec.py).(528) - Parsing arguments...
01.01.1970 12:00:00 - [DEBUG] - secEnvyronment - (envysec.py).(530) - ...parsing succeed.
01.01.1970 12:00:00 - [INFO] - secEnvyronment - (envysec.py).(535) - Initialize Command Line Interface (CLI).
01.01.1970 12:00:00 - [DEBUG] - EnvySec CLI - (envysec.py).init(52) - Initializing class...
01.01.1970 12:00:00 - [DEBUG] - EnvySec CLI - (envysec.py).init(55) - Trying to get default settings...
01.01.1970 12:00:00 - [DEBUG] - envySec SettingsEdit - (envy_settings.py).init(31) - Initializing class...
01.01.1970 12:00:00 - [DEBUG] - envySec SettingsEdit - (envy_settings.py).init(34) - Reading settings...
01.01.1970 12:00:00 - [WARNING] - envySec SettingsEdit - (envy_settings.py).init(57) - Failed to open C:\Users\Dev\Desktop\envysec\settings.json. File not found.
01.01.1970 12:00:00 - [DEBUG] - envySec SettingsEdit - (envy_settings.py).init(58) - FileNotFoundError args: (2, 'No such file or directory')
01.01.1970 12:00:00 - [INFO] - envySec SettingsEdit - (envy_settings.py).init(60) - Creating new settings.
01.01.1970 12:00:00 - [DEBUG] - envySec SettingsEdit - (envy_settings.py).init(61) - Gathering settings.
01.01.1970 12:00:00 - [DEBUG] - envySec SettingsEdit - (envy_settings.py).register_metadefender_api(188) - Starting register_metadefender_api...
01.01.1970 12:00:00 - [INFO] - envySec SettingsEdit - (envy_settings.py).register_metadefender_api(189) - Requesting API key...
01.01.1970 12:00:00 - [DEBUG] - envySec SettingsEdit - (envy_settings.py).register_metadefender_api(192) - Checking API key...
01.01.1970 12:00:00 - [DEBUG] - envySec SettingsEdit - (envy_settings.py).__check_metadefender_api(206) - Starting __check_metadefender_api...
01.01.1970 12:00:00 - [INFO] - envySec SettingsEdit - (envy_settings.py).__check_metadefender_api(207) - Check API keys length.
01.01.1970 12:00:00 - [DEBUG] - envySec SettingsEdit - (envy_settings.py).__check_metadefender_api(214) - API key length is valid.
01.01.1970 12:00:00 - [INFO] - envySec SettingsEdit - (envy_settings.py).__check_metadefender_api(215) - Check API key chars.
01.01.1970 12:00:00 - [INFO] - envySec SettingsEdit - (envy_settings.py).__check_metadefender_api(221) - API key validated.
01.01.1970 12:00:00 - [DEBUG] - envySec SettingsEdit - (envy_settings.py).init(64) - Successfully done gathering.

Steps to reproduce:

  1. Remove settings.json;
  2. Run any command, for example: python3 envysec.py -I 1.2.3.4

Affected versions:

  • latest/test.

Scan report requesting issue

Bug overview:

Scan report requesting issue makes file scan (raw files, not hash) infinite.

Reproduce steps:

  1. Run: python3 envysec.py -F some_unknown_file.ext, where some_unknown_file.ext is unknown file (with unique hash).
  2. Wait until ClamAV scan complete.
  3. Check metadefender scan requests.

Code proposal:

Push latest metadefender-framework.

Affected versions:

Confirmed:

  • 0.3d;
  • 0.4;
  • 0.4a.
    Currently, both master and test branch affected.

ClamAV scan thread won't start

Bug overview:

ClamAV scan thread won't start, envysec process can't be finished.

Code proposal:

Now:

        self.ClamLog.debug('__call_proc: Creating thread.')
        work_thread = threading.Thread(target = work, args = args, daemon = True)
        self.ClamLog.debug('__call_proc: Starting thread.')
        work_thread.join() # Use 'join' to prevent endless scan
        self.ClamLog.debug('__call_proc: Work thread Initialized.')

To be:

        self.ClamLog.debug('__call_proc: Creating thread.')
        work_thread = threading.Thread(target = work, args = args, daemon = True)
        self.ClamLog.debug('__call_proc: Starting thread.')
        work_thread.start()
        self.ClamLog.debug('__call_proc: Work thread Initialized.')

Affected methods:

Currently, confirmed:

  1. File scan;
    In branch:
  • master;
  • test.

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.