Guest User

DetectSystemD2

a guest
Sep 27th, 2017
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. import os
  3.  
  4. class UniqueList(list):
  5.    
  6.     def appendunique(self,item):
  7.         if item not in self:
  8.             self.append(item)
  9.             return True
  10.         return False
  11.  
  12.  
  13. def ReadFile(filename):
  14.     f = open(filename,'r');
  15.     return f.read();
  16.  
  17. def ReadFileLines(filename):
  18.     lines = []
  19.     with open(filename,'r') as f:
  20.         for line in f:
  21.             lines.append(line);
  22.     return lines
  23.  
  24. def WriteFile(filename,text):
  25.     f = open(filename,'w')
  26.     f.writelines(text)
  27.  
  28. def IsInList(DetectorList,ComparisonLine):
  29.     for Item in DetectorList:
  30.         if Item in ComparisonLine:
  31.             return Item
  32.  
  33.     return ""
  34.  
  35. def ConstantSystemdScan(Collector,lines):
  36.    
  37.     Iter = 0
  38.    
  39.     RepeatLoops = 0
  40.     RestartLoop = True
  41.    
  42.    
  43.     while RestartLoop == True:
  44.         Iter = 0
  45.         if RepeatLoops > 0:
  46.             print 'Rescanning list x'+str(RepeatLoops)
  47.         RestartLoop = False
  48.         while Iter < len(lines):
  49.             Check = IsInList(Collector[0],lines[Iter])
  50.            
  51.             if Check != "":
  52.                 Temp = lines[Iter].split(" ");
  53.                 Title = "["+Temp[0]+"]";
  54.                
  55.                 if Collector[0].appendunique(Title):
  56.                    
  57.                     RestartLoop = True
  58.                     Collector[1].append(Title+" via "+Check+"\n");
  59.                    
  60.                     print Title + ' detected as related to systemd via ' + Check
  61.                    
  62.                 else:
  63.                     Iter = Iter + 1
  64.             else:
  65.                 Iter = Iter + 1
  66.        
  67.         RepeatLoops = RepeatLoops + 1
  68.        
  69.     return Collector
  70.  
  71. def LoadBlackList(BlackList):
  72.     Collector = []
  73.    
  74.     DetectionList = UniqueList();
  75.     WriteOutList = []
  76.    
  77.     for line in BlackList:
  78.         Temp = line.replace("\n","")
  79.         Title = "["+Temp+"]";
  80.         DetectionList.appendunique(Title)
  81.         WriteOutList.append(Title + " via itself\n");
  82.    
  83.     Collector.append(DetectionList);
  84.     Collector.append(WriteOutList);
  85.     return Collector;
  86.  
  87. while True:
  88.     LocalDirectory =os.path.realpath(os.path.dirname(__file__))
  89.    
  90.     Garbage = ReadFileLines(LocalDirectory+"/HasDependencies.txt");
  91.    
  92.     BlackList = ReadFileLines(LocalDirectory+"/Blacklist.txt");
  93.    
  94.     Collector = LoadBlackList(BlackList);
  95.    
  96.     Collector2 = ConstantSystemdScan(Collector,Garbage)
  97.    
  98.     WriteFile(LocalDirectory+"/InfectedWithSystemd.txt",Collector2[1])
  99.    
  100.     exit()
RAW Paste Data