- 
                Notifications
    
You must be signed in to change notification settings  - Fork 311
 
Add new bot: time filter #1969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            mariuskarotkis
  wants to merge
  20
  commits into
  certtools:develop
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
mariuskarotkis:add_bot_time_filter
  
      
      
   
  
    
  
  
  
 
  
      
    base: develop
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Add new bot: time filter #1969
Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            20 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      0405205
              
                Add new bot: time filter
              
              
                mariuskarotkis ebd9e99
              
                Small changes
              
              
                mariuskarotkis 86056a6
              
                Update bot and add documentation
              
              
                mariuskarotkis 167fb37
              
                Add license
              
              
                mariuskarotkis 760ec91
              
                Add requirements, small fix
              
              
                mariuskarotkis b6cdad4
              
                Add license to requirements
              
              
                mariuskarotkis 0f615a8
              
                Add to utils get_timedelta
              
              
                mariuskarotkis 2e35b65
              
                Fix codestyle
              
              
                mariuskarotkis dd4e138
              
                Fix
              
              
                mariuskarotkis 5f545ee
              
                Merge branch 'develop' into add_bot_time_filter
              
              
                mariuskarotkis 9d60d5b
              
                Fix
              
              
                mariuskarotkis f111f9e
              
                Fix
              
              
                mariuskarotkis 811871b
              
                Change freezegun to time_machine
              
              
                mariuskarotkis 9d41b4a
              
                fix time-machine
              
              
                mariuskarotkis 2d8f636
              
                Fix tests
              
              
                mariuskarotkis a20be81
              
                Fix tests
              
              
                mariuskarotkis adb68ee
              
                Add description
              
              
                mariuskarotkis cd41494
              
                Remove similar function
              
              
                mariuskarotkis 8c72554
              
                Merge branch 'develop' into add_bot_time_filter
              
              
                mariuskarotkis e7427bd
              
                Merge branch 'develop' into add_bot_time_filter
              
              
                aaronkaplan File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
              Empty file.
          
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from datetime import datetime, timedelta | ||
| from dateutil import parser | ||
| from intelmq.lib.utils import get_timedelta | ||
| from intelmq.lib.bot import Bot | ||
| from datetime import timezone | ||
| 
     | 
||
| 
     | 
||
| class TimeFilterExpertBot(Bot): | ||
                
      
                  mariuskarotkis marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| search_field: str = 'time.source' | ||
                
      
                  mariuskarotkis marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| search_from: str = '1d' | ||
                
      
                  mariuskarotkis marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| not_after = None | ||
                
      
                  mariuskarotkis marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| def init(self): | ||
| self.search_field = self.search_field | ||
                
      
                  mariuskarotkis marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| if self.search_from: | ||
| timedelta_params = get_timedelta(self.search_from) | ||
| self.not_after = datetime.now(tz=timezone.utc) - timedelta(**timedelta_params) | ||
| 
     | 
||
| def process(self): | ||
| event = self.receive_message() | ||
| # time based filtering | ||
| if self.search_field in event: | ||
| try: | ||
| event_time = parser.parse(str(event.get(self.search_field))) | ||
                
      
                  mariuskarotkis marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| except ValueError: | ||
| event_time = self.not_after | ||
| self.process_message(event_time, event) | ||
| return | ||
| else: | ||
| self.process_message(event_time, event) | ||
| return | ||
| else: | ||
| # not found field | ||
| event_time = self.not_after | ||
| self.process_message(event_time, event) | ||
| return | ||
                
      
                  mariuskarotkis marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| def process_message(self, event_time, event): | ||
| event_time = event_time.replace(tzinfo=None) | ||
| self.not_after = self.not_after.replace(tzinfo=None) | ||
                
      
                  mariuskarotkis marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| if event_time < self.not_after: | ||
| self.acknowledge_message() | ||
| self.logger.debug( | ||
| f"Filtered out event with search field {self.search_field} and event time {event_time} .") | ||
| return | ||
| else: | ||
| self.send_message(event) | ||
| self.acknowledge_message() | ||
| return | ||
| 
     | 
||
                
      
                  mariuskarotkis marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| BOT = TimeFilterExpertBot | ||
              Empty file.
          
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| # -*- coding: utf-8 -*- | ||
| 
     | 
||
| import unittest | ||
| from freezegun import freeze_time | ||
| 
     | 
||
| import intelmq.lib.test as test | ||
| from intelmq.bots.experts.time_filter.expert import TimeFilterExpertBot | ||
| 
     | 
||
| EXAMPLE_INPUT_DROP = { | ||
| "__type": "Event", | ||
| "feed.accuracy": 90.0, | ||
| "feed.name": "Feodo Tracker IPs", | ||
| "feed.provider": "abuse.ch", | ||
| "feed.url": "https://feodotracker.abuse.ch/downloads/ipblocklist.csv", | ||
| "time.observation": "2020-10-13T06:14:49+00:00", | ||
| "raw": "dGVzdA==", | ||
| "extra.firstseen": "2020-10-11T02:10:59+00:00", | ||
| "source.port": 447, | ||
| "extra.lastonline": "2020-08-13T00:00:00+00:00", | ||
| "malware.name": "trickbot", | ||
| "time.source": "2020-10-13T00:00:00+00:00" | ||
| } | ||
| EXAMPLE_INPUT_PASS = { | ||
| "__type": "Event", | ||
| "feed.accuracy": 90.0, | ||
| "feed.name": "Feodo Tracker IPs", | ||
| "feed.provider": "abuse.ch", | ||
| "feed.url": "https://feodotracker.abuse.ch/downloads/ipblocklist.csv", | ||
| "time.observation": "2020-10-13T06:14:49+00:00", | ||
| "raw": "dGVzdA==", | ||
| "extra.firstseen": "2020-10-11T02:10:59+00:00", | ||
| "source.port": 447, | ||
| "extra.lastonline1": "2020-09-13T00:00:00+00:00", | ||
| "malware.name": "trickbot", | ||
| "time.source": "2020-10-13T00:00:00+00:00" | ||
| } | ||
| EXAMPLE_INPUT_PASS_2 = { | ||
| "__type": "Event", | ||
| "feed.accuracy": 90.0, | ||
| "feed.name": "Feodo Tracker IPs", | ||
| "feed.provider": "abuse.ch", | ||
| "feed.url": "https://feodotracker.abuse.ch/downloads/ipblocklist.csv", | ||
| "time.observation": "2020-10-13T06:14:49+00:00", | ||
| "raw": "dGVzdA==", | ||
| "extra.firstseen": "2020-10-11T02:10:59+00:00", | ||
| "source.port": 447, | ||
| "extra.lastonline": "", | ||
| "malware.name": "trickbot", | ||
| "time.source": "2020-10-13T00:00:00+00:00" | ||
| } | ||
| EXAMPLE_INPUT_PASS_3 = { | ||
| "__type": "Event", | ||
| "feed.accuracy": 90.0, | ||
| "feed.name": "Feodo Tracker IPs", | ||
| "feed.provider": "abuse.ch", | ||
| "feed.url": "https://feodotracker.abuse.ch/downloads/ipblocklist.csv", | ||
| "time.observation": "2020-10-13T06:14:49+00:00", | ||
| "raw": "dGVzdA==", | ||
| "extra.firstseen": "2020-10-11T02:10:59+00:00", | ||
| "source.port": 447, | ||
| "extra.lastonline": "2020-09-13", | ||
| "malware.name": "trickbot", | ||
| "time.source": "2020-10-13T00:00:00+00:00" | ||
| } | ||
| 
     | 
||
| 
     | 
||
| class TestFilterExpertBot(test.BotTestCase, unittest.TestCase): | ||
| """ | ||
| A TestCase for TimeFilterExpertBot handling Reports. | ||
| """ | ||
| 
     | 
||
| @classmethod | ||
| def set_bot(cls): | ||
| cls.bot_reference = TimeFilterExpertBot | ||
| cls.input_message = EXAMPLE_INPUT_DROP | ||
| cls.sysconfig = { | ||
| 'search_field': 'extra.lastonline', | ||
| 'search_from': "1d" | ||
| } | ||
| 
     | 
||
| @freeze_time("2021-05-05") | ||
| def test_expert_drop(self): | ||
| self.run_bot() | ||
| self.assertOutputQueueLen(0) | ||
| 
     | 
||
| @freeze_time("2020-09-09") | ||
| def test_expert_pass(self): | ||
| self.input_message = EXAMPLE_INPUT_PASS | ||
| self.run_bot() | ||
| self.assertOutputQueueLen(1) | ||
| 
     | 
||
| @freeze_time("2020-09-09") | ||
| def test_expert_pass_2(self): | ||
| self.input_message = EXAMPLE_INPUT_PASS_2 | ||
| self.run_bot() | ||
| self.assertOutputQueueLen(1) | ||
| 
     | 
||
| @freeze_time("2020-09-09") | ||
| def test_expert_pass_3(self): | ||
| self.input_message = EXAMPLE_INPUT_PASS_3 | ||
| self.run_bot() | ||
| self.assertOutputQueueLen(1) | ||
| 
     | 
||
| 
     | 
||
| if __name__ == '__main__': # pragma: no cover | ||
| unittest.main() | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.