Exemplo n.º 1
0
    def process_IN_MOVED_TO(self, event):

        print("-> (+) Creating filter for ", event.pathname + "...\n")

        try:
            #Adds filter for file
            utils.addFilter(event.pathname, 'filters.cfg')

            config.read('filters.cfg')
            print("Currently filtering: " + str(len(config.sections()[1:])) +
                  " mails\n\n")

            #Creates socket for filter
            config.read('filters.cfg')
            program = config.get(config.sections()[-1], 'program')
            function = config.get(config.sections()[-1], 'function')

            fd = loadFilter(program, function)

            config.set(config.sections()[-1], 'fd', fd)

            #writes configuration
            with open('filters.cfg', 'wb') as configfile:
                config.write(configfile)

        except:
            print("Error creating filter")
Exemplo n.º 2
0
  def process_IN_MODIFY(self, event):

    config.read('filters.cfg')

    hashes = []
    for section in config.sections()[1:]:
      config.remove_option(section, 'fd')
      hashes.append(config.get(section, 'hash'))

    # Adding filter for files in spam/ if needed
    for entry in os.listdir(basepath):
      if os.path.isfile(os.path.join(basepath, entry)) and entry != '.gitkeep' :
        hash_summary = utils.getHash(os.path.join(basepath, entry)).hexdigest()
        if hash_summary not in hashes:

          print("-> (+) Adding filter for " + str(entry) + "\n")
          utils.addFilter(os.path.join(basepath, entry), 'filters.cfg')
          #Creates socket for filter
          config.read('filters.cfg')
          program = config.get(config.sections()[-1], 'program')
          function = config.get(config.sections()[-1], 'function')

          fd = loadFilter(program, function)

          config.set(config.sections()[-1], 'fd', fd)

        else:
          hashes.remove(hash_summary)

         #writes configuration
        with open('filters.cfg', 'wb') as configfile:
          config.write(configfile)

    print("Currently filtering: " + str(len(bpf)) + " mails\n\n")
Exemplo n.º 3
0
 def test_add_3(self):
     print("Testing mail with wrong format")
     numCaracteres, caracteres = utils.addFilter(
         'test/file_test3', 'test/filters_test_add.cfg')
     self.assertEqual(numCaracteres, -1)
     self.assertEqual(caracteres, [])
     self.config.read('test/filters_test_add.cfg')
     self.assertTrue(len(self.config.sections()) == 1)
Exemplo n.º 4
0
 def test_add_1(self):
     print(
         "Testing mail with character's array of len < 30 and total size < 15000"
     )
     numCaracteres1, caracteres1 = utils.addFilter(
         'test/file_test1', 'test/filters_test_add.cfg')
     self.assertEqual(numCaracteres1, 14)
     self.assertEqual(caracteres1, [
         '-', '_', 't', 'e', 'r', '\n', 'g', 'I', 'C', 'W', '2', 'J', 'u',
         'B'
     ])
     self.config.read('test/filters_test_add.cfg')
     self.assertTrue(len(self.config.sections()) == 2)
Exemplo n.º 5
0
 def test_add_2(self):
     print(
         "Testing mail with character's array of len > 30 and total size > 15000"
     )
     numCaracteres, caracteres = utils.addFilter(
         'test/file_test2', 'test/filters_test_add.cfg')
     self.assertEqual(numCaracteres, 30)
     self.assertEqual(caracteres, [
         ' ', 'n', '\xfa', ' ', ' ', ' ', ' ', 'b', '/', 'V', 'x', '9', 'H',
         'v', 'j', 'f', 'w', 'E', '3', 'a', 'p', '2', 'M', 't', 'p', 'g',
         'O', 'd', '7', 'y'
     ])
     self.config.read('test/filters_test_add.cfg')
     self.assertTrue(len(self.config.sections()) == 2)
Exemplo n.º 6
0
def filter():

    if os.path.exists("results.txt"):
        os.remove("results.txt")

    #Reading configuration
    config.read('filters.cfg')

    print("\n\nSTARTING PARSE-MAIL...\n\n")

    #Updates configuration
    hashes = []
    for section in config.sections()[1:]:
        config.remove_option(section, 'fd')
        hashes.append(config.get(section, 'hash'))

    # Adding filter for files in spam/ if needed
    for entry in os.listdir(basepath):
        if os.path.isfile(os.path.join(basepath,
                                       entry)) and entry != '.gitkeep':
            hash_summary = utils.getHash(os.path.join(basepath,
                                                      entry)).hexdigest()
            if hash_summary not in hashes:
                print("-> (+) Adding filter for " + str(entry) + "\n")
                utils.addFilter(os.path.join(basepath, entry), 'filters.cfg')
            else:
                hashes.remove(hash_summary)

    # Removing filters if not in directory spam/
    config.read('filters.cfg')
    for section in config.sections()[1:]:
        if config.get(section, 'hash') in hashes:
            if os.path.exists("./filters/" + config.get(section, 'program')):
                print("-> (-) Removing filter " +
                      config.get(section, 'program') + "\n")
                os.remove("./filters/" + config.get(section, 'program'))
            config.remove_section(section)
    with open('filters.cfg', 'wb') as configfile:
        config.write(configfile)

    config.read('filters.cfg')

    print("\n**************************************************\n\n")
    print("Currently filtering " + str(len(config.sections()[1:])) +
          " mails\n\n")
    print("Binding socket to interface " + interface + "\n\n")
    print("Monitoring " + basepath + "\n\n")
    print("Press CTRL-C to exit\n\n")
    print("**************************************************\n\n")

    # Adds every filter in config
    for filter in config.sections()[1:]:
        program = config.get(filter, 'program')
        print("Load filter " + program + "\n")
        function = config.get(filter, 'function')

        fd = loadFilter(program, function)

        config.set(filter, 'fd', fd)

        with open('filters.cfg', 'wb') as configfile:
            config.write(configfile)

    print("Starting filtering...\n")

    while 1:
        for s in socket_fd:
            f = open("results.txt", "aw")
            f.write(str(socket_fd))
            f.write(str(s))
            f.write(os.read(s, 10000))
            f.close()