Advertisement
The_KGB

[Python] Adobe' s Malware Classifier

Apr 6th, 2012
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 28.90 KB | None | 0 0
  1. """
  2. The BSD License
  3.  
  4. Copyright (c) 2012, Adobe Systems Incorporated
  5. All rights reserved.
  6.  
  7. """
  8. # Adobe(R) Malware Classifier
  9. # Contributor: Karthik Raman, Adobe Systems Incorporated
  10. # Dependencies: Python pefile, Python argparse
  11. # Program to classify unknown Win32 binaries (EXEs or DLLs)  into
  12. # 0 = CLEAN
  13. # 1 = DIRTY
  14. # UNKNOWN
  15. """ Results on dataset of ~130000 dirty, ~ 16000 clean files:
  16.         (False Positives, True Negatives, True Positives, rates
  17. J48     FP      TN      TP      FN      TP Rate         FP Rate         Accuracy
  18.       7683    37171   130302  3451    0.97419871      0.171289071     0.937662018
  19.  
  20. J48Graft FP      TN      TP      FN      TP Rate        FP Rate         Accuracy
  21.       6780    38074   129087  4666    0.965114801     0.151157087     0.935915166
  22.  
  23.  
  24. PART    FP      TN      TP      FN      TP Rate         FP Rate         Accuracy
  25.       7074    36492   125060  9412    0.930007734     0.162374329     0.907401791
  26.  
  27.  
  28. Ridor   FP      TN      TP      FN      TP Rate         FP Rate         Accuracy
  29.       7390    37935   114194  20930   0.845105237     0.163044677     0.843058149
  30. """    
  31.  
  32. import pefile
  33. import argparse
  34. import sys
  35.  
  36. DEBUG = 0;
  37. isDirty = 0;
  38.  
  39. # Class to extract data values from PE file and hold them as member variables
  40. class PEFile:
  41.     def __init__(self,filename):
  42.         self.pe =  pefile.PE(filename,fast_load=True)
  43.         self.filename=filename
  44.         self.DebugSize      = self.pe.OPTIONAL_HEADER.DATA_DIRECTORY[6].Size
  45.         self.ImageVersion   = ((self.pe.OPTIONAL_HEADER.MajorImageVersion*100)+self.pe.OPTIONAL_HEADER.MinorImageVersion)*1000
  46.         self.IatRVA         = self.pe.OPTIONAL_HEADER.DATA_DIRECTORY[1].VirtualAddress
  47.         self.ExportSize     = self.pe.OPTIONAL_HEADER.DATA_DIRECTORY[0].Size
  48.         self.ResourceSize   = self.pe.OPTIONAL_HEADER.DATA_DIRECTORY[2].Size
  49.         self.VirtualSize2   = self.pe.sections[1].Misc_VirtualSize
  50.         self.NumberOfSections = self.pe.FILE_HEADER.NumberOfSections       
  51.     def DataDump(self):
  52.         print('Starting dump of ' + self.filename)
  53.         print('DebugSize:   ' + str(self.DebugSize )) # DebugSize
  54.         print('ImageVersion:    ' + str(self.ImageVersion)) # ImageVersion
  55.         print('IatRVA:      ' + str(self.IatRVA)) #IatRVA
  56.         print('ExportSize:  ' + str(self.ExportSize)) # ExportSize
  57.         print('ResourceSize:    ' + str(self.ResourceSize)) # ResourceSize
  58.         print('VirtualSize2:    ' + str(self.VirtualSize2)) # VirtualSize2
  59.         print('NumberOfSections:' + str(self.NumberOfSections)) # NumberOfSections
  60.         print('Stop')
  61.  
  62. def printResult(classification):
  63.     if classification == 0:
  64.         print '0'
  65.     else:
  66.         print '1'
  67.        
  68. def runJ48():
  69.     isDirty = 0
  70.     if input.DebugSize <=0:
  71.         if input.ExportSize <= 211:
  72.             if input.ImageVersion <= 520:
  73.                 if input.VirtualSize2 <= 130:
  74.                     if input.VirtualSize2 <= 5:
  75.                         if input.ResourceSize <= 37520:
  76.                             isDirty = 1
  77.                         elif input.ResourceSize > 37520:
  78.                             if input.NumberOfSections <= 2:
  79.                                 if input.IatRVA <= 2048:
  80.                                     isDirty = 0
  81.                                 else:
  82.                                     isDirty = 1
  83.                             else:
  84.                                 isDirty = 1
  85.                     else:
  86.                         if input.VirtualSize2 <= 12:
  87.                             if input.NumberOfSections <= 3:
  88.                                 isDirty = 0
  89.                             else:
  90.                                 isDirty = 1
  91.                         else:
  92.                             isDirty = 1
  93.                 else:
  94.                     isDirty = 1
  95.             else:
  96.                 if input.ResourceSize <= 0:
  97.                     if input.ImageVersion <= 1000:
  98.                         if input.NumberOfSections <= 4:
  99.                             isDirty = 1
  100.                         else:
  101.                             if input.ExportSize <= 74:
  102.                                 if input.VirtualSize2 <= 1556:
  103.                                     isDirty = 1
  104.                                 else:
  105.                                     isDirty = 0
  106.                             else:
  107.                                 isDirty = 0
  108.                     else:
  109.                         isDirty = 1
  110.                 else:
  111.                     if input.NumberOfSections <= 2:
  112.                         if input.ImageVersion <= 3420:
  113.                             isDirty = 1
  114.                         else:
  115.                             isDirty = 0
  116.                     else:
  117.                         isDirty = 1
  118.         else:
  119.             if input.ImageVersion <= 0:
  120.                 if input.ExportSize <= 23330:
  121.                     if input.IatRVA <= 98304:
  122.                         if input.NumberOfSections <= 3:
  123.                             isDirty = 1
  124.                         else:
  125.                             if input.IatRVA <= 53872:
  126.                                 isDirty = 0
  127.                             else:
  128.                                 if input.ExportSize <= 273:
  129.                                     isDirty = 1
  130.                                 else:
  131.                                     if input.ResourceSize <= 1016:
  132.                                         isDirty = 1
  133.                                     else:
  134.                                         isDirty = 0
  135.                     else:
  136.                         isDirty = 0
  137.                 else:
  138.                     isDirty = 1
  139.             else:
  140.                 isDirty = 0
  141.     else:
  142.         if input.ResourceSize <= 545:
  143.             if input.ExportSize <= 92:
  144.                 if input.NumberOfSections <= 4:
  145.                     isDirty = 0
  146.                 else:
  147.                     isDirty = 1
  148.         else:
  149.             if input.IatRVA <= 94208:
  150.                 if input.NumberOfSections <= 5:
  151.                     if input.ExportSize <= 0:
  152.                         if input.NumberOfSections <= 4:
  153.                             if input.IatRVA <= 13504:
  154.                                 if input.ImageVersion <= 353:
  155.                                     if input.NumberOfSections <= 3:
  156.                                         if input.IatRVA <= 6144:
  157.                                             if input.IatRVA <= 2048:
  158.                                                 isDirty = 0
  159.                                             else:
  160.                                                 if input.VirtualSize2 <= 496:
  161.                                                     isDirty = 1
  162.                                                 else:
  163.                                                     isDirty = 0
  164.                                         else:
  165.                                             isDirty = 0
  166.                                     else:
  167.                                         if input.DebugSize <= 41:
  168.                                             if input.ResourceSize <= 22720:
  169.                                                 isDirty = 1
  170.                                             else:
  171.                                                 isDirty = 0
  172.                                         else:
  173.                                             isDirty = 0
  174.                                 else:
  175.                                     isDirty = 0
  176.                             else:
  177.                                 if input.ResourceSize <= 35328:
  178.                                     isDirty = 0
  179.                                 else:
  180.                                     isDirty = 1
  181.                         else:
  182.                             if input.IatRVA <= 2048:
  183.                                 isDirty = 1
  184.                             else:
  185.                                 isDirty = 0
  186.                     else:
  187.                         isDirty = 0
  188.                 else:
  189.                     if input.IatRVA <= 1054:
  190.                         if input.ExportSize <= 218:
  191.                             if input.IatRVA <= 704:
  192.                                 isDirty = 1
  193.                             else:
  194.                                 if input.NumberOfSections <= 6:
  195.                                     isDirty = 1
  196.                                 else:
  197.                                     isDirty = 0
  198.                         else:
  199.                             isDirty = 0
  200.                     else:
  201.                         isDirty = 0
  202.             else:
  203.                 if input.ExportSize <= 0:
  204.                     if input.VirtualSize2 <= 78800:
  205.                         if input.NumberOfSections <= 4:
  206.                             isDirty = 0
  207.                         else:
  208.                             if input.ImageVersion <= 2340:
  209.                                 if input.ResourceSize <= 7328:
  210.                                     isDirty = 1
  211.                                 else:
  212.                                     isDirty = 0
  213.                             else:
  214.                                 isDirty = 0
  215.                     else:
  216.                         isDirty = 1
  217.                 else:
  218.                     if input.IatRVA <= 106496:
  219.                         if input.ResourceSize <= 2800:
  220.                             isDirty = 0
  221.                         else:
  222.                             isDirty = 1
  223.                     else:
  224.                         isDirty = 0
  225.     return isDirty
  226. # Might need to add a isDirty = 0 statement if tree results in unclassified result                 
  227.  
  228. def runJ48Graft():
  229.     isDirty = 0
  230.     if input.DebugSize <=0:
  231.         if input.ExportSize <= 211:
  232.             if input.ImageVersion <= 520:
  233.                 if input.VirtualSize2 <= 130:
  234.                     if input.VirtualSize2 <= 5:
  235.                         if input.ResourceSize <= 37520:
  236.                             isDirty = 1
  237.                         elif input.ResourceSize > 37520:
  238.                             if input.NumberOfSections <= 2:
  239.                                 if input.IatRVA <= 2048:
  240.                                     if input.ExportSize <= 67.5:
  241.                                         isDirty = 0
  242.                                     else:                              
  243.                                         isDirty = 1
  244.                                 else:
  245.                                     isDirty = 1
  246.                             else:
  247.                                 isDirty = 1
  248.                     else:
  249.                         if input.VirtualSize <= 12:
  250.                             if input.NumberOfSections <= 3:
  251.                                 isDirty = 0
  252.                             else:
  253.                                 isDirty = 1
  254.                         else:
  255.                             isDirty = 1
  256.                 else:
  257.                     isDirty = 1
  258.             else:
  259.                 if input.ResourceSize <= 0:
  260.                     if input.ImageVersion <= 1000:
  261.                         if input.NumberOfSections <= 4:
  262.                             isDirty = 1
  263.                         else:
  264.                             if input.ExportSize <= 74:
  265.                                 if input.VirtualSize2 <= 1556:
  266.                                     isDirty = 1
  267.                                 else:
  268.                                     if input.IatRVA <= 5440:
  269.                                         if input.VirtualSize2 <= 126474:
  270.                                             if input.ExportSize <= 24:
  271.                                                 isDirty = 0
  272.                                             else:
  273.                                                 isDirty = 1
  274.                                         else:
  275.                                             isDirty = 1
  276.                                     else:
  277.                                         isDirty = 1
  278.                             else:
  279.                                 isDirty = 0
  280.                     else:
  281.                         isDirty = 1
  282.                 else:
  283.                     if input.NumberOfSections <= 2:
  284.                         if input.ImageVersion <= 3420:
  285.                             isDirty = 1
  286.                         else:
  287.                             isDirty = 0
  288.                     else:
  289.                         isDirty = 1
  290.         else:
  291.             if input.ImageVersion <= 0:
  292.                 if input.ExportSize <= 23330:
  293.                     if input.IatRVA <= 98304:
  294.                         if input.NumberOfSections <= 3:
  295.                             isDirty = 1
  296.                         else:
  297.                             if input.IatRVA <= 53872:
  298.                                 if input.VirtualSize2 <= 17.5:
  299.                                     isDirty = 1
  300.                                 else:
  301.                                     if input.NumberOfSections <= 10.5:
  302.                                         if input.ResourceSize <= 3103192:
  303.                                             if input.ExportSize <= 10858.5:
  304.                                                 if input.VirtualSize2 <= 116016.5:
  305.                                                     isDirty = 0
  306.                                                 else:
  307.                                                     isDirty = 1
  308.                                             else:
  309.                                                 isDirty = 0
  310.                                         else:
  311.                                             isDirty = 1
  312.                                     else:
  313.                                         isDirty = 1
  314.                             else:
  315.                                 if input.ExportSize <= 273:
  316.                                     isDirty = 1
  317.                                 else:
  318.                                     if input.ResourceSize <= 1016:
  319.                                         isDirty = 1
  320.                                     else:
  321.                                         isDirty = 0
  322.                     else:
  323.                         isDirty = 0
  324.                 else:
  325.                     isDirty = 1
  326.             else:
  327.                 if input.ExportSize <= 1006718985:
  328.                     isDirty = 0
  329.                 else:
  330.                     isDirty = 1
  331.     else:
  332.         if input.ResourceSize <= 545:
  333.             if input.ExportSize <= 92:
  334.                 if input.NumberOfSections <= 4:
  335.                     isDirty = 0
  336.                 else:
  337.                     if input.ImageVersion <= 6005:
  338.                         if input.ExportSize <= 6714:
  339.                             isDirty = 1
  340.                         else:
  341.                             isDirty = 0
  342.                     else:
  343.                         isDirty = 0                        
  344.         else:
  345.             if input.IatRVA <= 94208:
  346.                 if input.NumberOfSections <= 5:
  347.                     if input.ExportSize <= 0:
  348.                         if input.NumberOfSections <= 4:
  349.                             if input.IatRVA <= 13504:
  350.                                 if input.ImageVersion <= 353:
  351.                                     if input.NumberOfSections <= 3:
  352.                                         if input.IatRVA <= 6144:
  353.                                             if input.IatRVA <= 2048:
  354.                                                 if input.ResourceSize <= 934:
  355.                                                     isDirty = 1
  356.                                                 else:
  357.                                                     if input.VirtualSize2 <= 2728:
  358.                                                         isDirty = 0
  359.                                                     else:
  360.                                                         isDirty = 1
  361.                                             else:
  362.                                                 if input.VirtualSize2 <= 496:
  363.                                                     isDirty = 1
  364.                                                 else:
  365.                                                     isDirty = 0
  366.                                         else:
  367.                                             isDirty = 0
  368.                                     else:
  369.                                         if input.DebugSize <= 41: # debug here
  370.                                             if input.ResourceSize <= 22720:
  371.                                                 if input.IatRVA <= 2048:
  372.                                                     isDirty = 1
  373.                                                 else:
  374.                                                     if input.VirtualSize2 <= 46:
  375.                                                         isDirty = 0
  376.                                                     else:
  377.                                                         isDirty = 1
  378.                                             else:
  379.                                                     if input.VirtualSize2 <= 43030:
  380.                                                         if input.ResourceSize <= 3898348:
  381.                                                             if input.IatRVA <= 2048:
  382.                                                                 isDirty = 1
  383.                                                             else:
  384.                                                                 isDirty = 0
  385.                                                         else:
  386.                                                             isDirty = 1
  387.                                                     else:
  388.                                                         isDirty = 0
  389.                                         else:
  390.                                             isDirty = 0
  391.                                 else:
  392.                                     isDirty = 0
  393.                             else:
  394.                                 if input.ResourceSize <= 35328:
  395.                                     if input.ImageVersion <= 4005:
  396.                                         if input.NumberOfSections <= 1.5:
  397.                                             isDirty = 1
  398.                                         else:
  399.                                             isDirty = 0
  400.                                     else:
  401.                                         isDirty = 0
  402.                                 else:
  403.                                     if input.ImageVersion <= 5510:
  404.                                         if input.DebugSize <= 42:
  405.                                             if input.VirtualSize2 <= 144328:
  406.                                                 if input.NumberOfSections <= 3.5:
  407.                                                     isDirty = 0
  408.                                                 else:
  409.                                                     isDirty = 1
  410.                                             else:
  411.                                                 isDirty = 0
  412.                                         else:
  413.                                             isDirty = 0
  414.                                     else:
  415.                                         isDirty = 0                                    
  416.                         else:
  417.                             if input.IatRVA <= 2048:
  418.                                 isDirty = 1
  419.                             else:
  420.                                 isDirty = 0
  421.                     else:
  422.                         isDirty = 0
  423.                 else:
  424.                     if input.IatRVA <= 1054:
  425.                         if input.ExportSize <= 218:
  426.                             if input.IatRVA <= 704:
  427.                                 isDirty = 1
  428.                             else:
  429.                                 if input.NumberOfSections <= 6:
  430.                                     isDirty = 1
  431.                                 else:
  432.                                     isDirty = 0
  433.                         else:
  434.                             if input.ExportSize <= 1006699445:
  435.                                 if input.ImageVersion <= 5510:
  436.                                     if input.ImageVersion <= 500:
  437.                                         isDirty = 1
  438.                                     else:
  439.                                         isDirty = 0
  440.                                 else:
  441.                                     isDirty = 0
  442.                             else:
  443.                                 isDirty = 1
  444.                     else:
  445.                         isDirty = 0
  446.             else:
  447.                 if input.ExportSize <= 0:
  448.                     if input.VirtualSize2 <= 78800:
  449.                         if input.NumberOfSections <= 4:
  450.                             isDirty = 0
  451.                         else:
  452.                             if input.ImageVersion <= 2340:
  453.                                 if input.ResourceSize <= 7328:
  454.                                     isDirty = 1
  455.                                 else:
  456.                                     if input.VirtualSize2 <= 8288.5:
  457.                                         isDirty = 1
  458.                                     else:
  459.                                         if input.NumberOfSections <= 6.5:
  460.                                             isDirty = 0
  461.                                         else:
  462.                                             isDirty = 1
  463.                             else:
  464.                                 isDirty = 0
  465.                     else:
  466.                         if input.ImageVersion <= 5515:
  467.                             isDirty = 1
  468.                         else:
  469.                             isDirty = 0
  470.                 else:
  471.                     if input.IatRVA <= 106496:
  472.                         if input.ResourceSize <= 2800:
  473.                             isDirty = 0
  474.                         else:
  475.                             if input.ImageVersion <= 500:
  476.                                 if input.ResourceSize <= 5360:
  477.                                     if input.NumberOfSections <= 4.5:
  478.                                         isDirty = 0
  479.                                     else:
  480.                                         if input.VirtualSize2 <= 22564.5:
  481.                                             if input.ExportSize <= 191.5:
  482.                                                 if input.DebugSize <= 42:
  483.                                                     if input.ExportSize <= 162.5:
  484.                                                         isDirty = 0
  485.                                                     else:
  486.                                                         if input.VirtualSize2 <= 10682:
  487.                                                             isDirty = 0
  488.                                                         else:
  489.                                                             if input.ResourceSize <= 3412:
  490.                                                                 isDirty = 0
  491.                                                             else:
  492.                                                                 isDirty = 1
  493.                                                 else:
  494.                                                     isDirty = 0
  495.                                             else:
  496.                                                 isDirty = 0
  497.                                         else:
  498.                                             isDirty = 0
  499.                                 else:
  500.                                     isDirty = 0
  501.                             else:
  502.                                 isDirty = 0
  503.                     else:
  504.                         isDirty = 0
  505.     return isDirty
  506. # Might need to add a isDirty = 0 statement if tree results in unclassified result                 
  507.  
  508. def runPART():
  509.     isDirty = 0
  510.     if input.DebugSize > 0  and input.ResourceSize > 545 and input.IatRVA <= 94208 and input.NumberOfSections <= 5 and input.ExportSize > 0 and input.NumberOfSections > 3:
  511.         isDirty = 0
  512.     elif input.DebugSize <=0 and input.ImageVersion <= 4900 and input.ExportSize <= 71 and input.ImageVersion <= 520 and input.VirtualSize2 > 130 and input.IatRVA <= 24576:
  513.         isDirty = 1
  514.     elif input.DebugSize <=0 and input.ImageVersion <= 4900 and input.ExportSize <= 211 and input.ResourceSize <= 32272 and input.NumberOfSections <= 10 and input.VirtualSize2 <= 5 and input.ImageVersion <= 3420:
  515.         isDirty = 1
  516.     elif input.DebugSize > 0 and input.ResourceSize > 598 and input.VirtualSize2 <= 105028 and input.VirtualSize2 > 1 and input.ImageVersion > 5000:
  517.         isDirty = 0
  518.     elif input.IatRVA <= 0 and input.ImageVersion > 4180 and input.ResourceSize > 2484:
  519.         isDirty = 0
  520.     elif input.DebugSize <= 0 and input.NumberOfSections <= 1 and input.ResourceSize > 501:
  521.         isDirty = 0
  522.     elif input.DebugSize <= 0 and input.ExportSize <= 211 and input.NumberOfSections > 2 and input.ImageVersion > 1000 and input.ResourceSize <= 12996:
  523.         isDirty = 1
  524.     elif input.DebugSize <= 0 and input.ExportSize <= 211 and input.NumberOfSections > 2 and input.ResourceSize > 0 and input.VirtualSize2 > 1016:
  525.         isDirty = 1
  526.     elif input.NumberOfSections > 8 and input.VirtualSize2 <= 2221:
  527.         isDirty = 1
  528.     elif input.ResourceSize <= 736 and input.NumberOfSections <= 3:
  529.         isDirty = 1
  530.     elif input.NumberOfSections <= 3 and input.IatRVA > 4156:
  531.         isDirty = 0
  532.     elif input.ImageVersion <= 6000 and input.ResourceSize <= 523 and input.IatRVA > 0 and input.ExportSize <= 95:
  533.         isDirty = 1
  534.     elif input.ExportSize <= 256176 and input.DebugSize > 0 and input.ImageVersion <= 5450 and input.IatRVA > 1664 and input.ResourceSize <= 2040 and input.DebugSize <= 41:
  535.         isDirty = 0
  536.     elif input.ExportSize <= 256176 and input.ImageVersion > 5450:
  537.         isDirty = 0
  538.     elif input.ExportSize > 256176:
  539.         isDirty = 1
  540.     elif input.ImageVersion > 0 and input.ResourceSize > 298216 and input.IatRVA <= 2048:
  541.         isDirty = 1
  542.     elif input.ImageVersion > 0 and input.ExportSize > 74 and input.DebugSize > 0:
  543.         isDirty = 0
  544.     elif input.ImageVersion > 0 and input.VirtualSize2 > 4185 and input.ResourceSize <= 215376 and input.IatRVA <= 2048 and input.NumberOfSections <= 5:
  545.         isDirty = 0
  546.     elif input.ImageVersion > 1010 and input.DebugSize <= 56 and input.VirtualSize2 <= 215376:
  547.         isDirty = 0
  548.     elif input.ExportSize > 258 and input.NumberOfSection > 3 and input.DebugSize > 0:
  549.         isDirty = 0
  550.     elif input.ExportSize > 262 and input.ImageVersion > 0 and input.NumberOfSections > 7:
  551.         isDirty = 0
  552.     elif input.DebugSize > 41 and input.NumberOfSections <= 4:
  553.         isDirty = 0
  554.     elif input.ExportSize <= 262 and input.NumberOfSections > 3 and input.VirtualSize2 <= 37:
  555.         isDirty = 1
  556.     elif input.VirtualSize2 > 40 and input.ExportSize <= 262 and input.DebugSize <= 0 and input.ImageVersion <= 353 and input.ExportSize <= 142:
  557.         isDirty = 1
  558.     elif input.VirtualSize2 > 72384 and input.VirtualSize2 <= 263848:
  559.         isDirty = 1
  560.     elif input.IatRVA > 106496 and input.IatRVA <= 937984 and input.DebugSize > 0 and input.ResourceSize > 4358:
  561.         isDirty = 0
  562.     elif input.VirtualSize2 <= 64 and input.IatRVA <= 2048 and input.DebugSize <= 0 and input.ImageVersion <= 353 and input.ExportSize <= 0 and input.VirtualSize2 <= 4 and input.NumberOfSections <= 2:
  563.         isDirty = 0
  564.     elif input.DebugSize <= 0 and input.NumberOfSections <= 4 and input.IatRVA > 45548:
  565.         isDirty = 1
  566.     elif input.DebugSize > 0 and input.DebugSize <= 56 and input.IatRVA <= 94208 and input.ResourceSize <= 4096:
  567.         isDirty = 1
  568.     elif input.DebugSize <= 0 and input.IatRVA <= 98304 and input.NumberOfSections > 6 and input.ResourceSize <= 864 and input.ExportSize > 74 and input.ImageVersion > 353 and input.ExportSize <= 279:
  569.         isDirty = 0
  570.     elif input.DebugSize <= 0 and input.IatRVA <= 98304 and input.NumberOfSections <= 2 and input.ResourceSize <= 1264128:
  571.         isDirty = 1
  572.     elif input.VirtualSize2 <= 64 and input.IatRVA <= 2048 and input.DebugSize > 0:
  573.         isDirty = 0
  574.     elif input.ExportSize <= 276 and input.NumberOfSections > 5 and input.ResourceSize <= 1076:
  575.         isDirty = 0
  576.     elif input.DebugSize > 0 and input.IatRVA <= 94208 and input.ExportSize <= 82 and input.DebugSize <= 56 and input.NumberOfSections > 2 and input.ImageVersion <= 2340 and input.ResourceSize <= 118280 and input.VirtualSize2 > 5340:
  577.         isDirty = 0
  578.     elif input.DebugSize > 0 and input.ImageVersion <= 2340 and input.DebugSize <= 56 and input.NumberOfSections > 3 and input.VirtualSize2 > 360 and input.NumberOfSections <= 5:
  579.         isDirty = 1
  580.     elif input.IatRVA > 37380 and input.ImageVersion <= 0 and input.NumberOfSections <= 5 and input.VirtualSize2 > 15864:
  581.         isDirty = 0
  582.     elif input.DebugSize <= 0 and input.VirtualSize2 <= 80 and input.IatRVA <= 4096 and input.ExportSize <= 0 and input.VirtualSize2 > 4 and input.VirtualSize2 <= 21:
  583.         isDirty = 0
  584.     elif input.DebugSize <= 0:
  585.         isDirty = 1
  586.     elif input.ExportSize <= 82 and input.DebugSize <= 56 and input.NumberOfSections <= 5 and input.NumberOfSections > 2 and input.IatRVA <= 6144 and input.ImageVersion > 2340:
  587.         isDirty = 0
  588.     elif input.ImageVersion > 2340:
  589.         isDirty = 1
  590.     elif input.ResourceSize > 5528:
  591.         isDirty = 0
  592.     else:
  593.         isDirty = 1
  594.     return isDirty
  595.  
  596.    
  597. def runRidor():
  598.     isDirty = 0
  599.     #Except (DebugSize <= 14) and (ImageVersion <= 760) and (VirtualSize2 > 992) and (ExportSize <= 80.5) => isDirty = 1  (1702.0/16.0) [855.0/5.0]
  600.     if input.DebugSize <= 14 and input.ImageVersion <= 760 and input.VirtualSize2 > 992 and input.ExportSize <= 80.5:
  601.         isDirty = 1
  602. #Except (DebugSize <= 14) and (ImageVersion <= 4525) and (ExportSize <= 198.5) and (ResourceSize <= 37532) and (VirtualSize2 <= 6) and (ResourceSize <= 7348) and (ResourceSize > 1773) => isDirty = 1  (106.0/0.0) [48.0/0.0]
  603.     elif input.DebugSize <= 14 and input.ImageVersion <= 4525  and input.ExportSize <= 198.5 and input.ResourceSize <= 7348 and input.VirtualSize2 <=6 and input.ResourceSize > 1773:
  604.         isDirty = 1
  605. #Except (DebugSize <= 14) and (ImageVersion <= 4950) and (ExportSize <= 192) and (IatRVA > 256) and (VirtualSize2 > 42) and (ExportSize <= 56) and (NumberOfSections > 3.5) => isDirty = 1  (193.0/0.0) [91.0/0.0]
  606.     elif input.DebugSize <= 14 and input.ImageVersion <= 4950 and input.ExportSize <= 56 and input.IatRVA > 256 and input.VirtualSize2 > 42 and input.NumberOfSections > 3.5:
  607.         isDirty = 1
  608. #Except (DebugSize <= 14) and (ImageVersion <= 4950) and (VirtualSize2 <= 6) and (ResourceSize <= 37532) and (ResourceSize <= 17302) => isDirty = 1  (388.0/0.0) [216.0/7.0]
  609.     elif input.DebugSize <= 14 and input.ImageVersion <= 4950 and input.VirtualSize2 <= 6 and input.ResourceSize > 17302:
  610.         isDirty = 1
  611. #Except (DebugSize <= 14) and (NumberOfSections > 2.5) and (ResourceSize > 1776) and (IatRVA <= 6144) and (ExportSize <= 219.5) and (VirtualSize2 > 2410) and (VirtualSize2 <= 61224) => isDirty = 1  (238.0/0.0) [116.0/0.0]
  612.     elif input.DebugSize <= 14 and input.NumberOfSections >= 2.5 and input.ResourceSize <= 1776 and input.IatRVA <= 6144 and input.ExportSize <= 219.5 and input.VirtualSize2 > 2410 and input.VirtualSize2 <= 61224:
  613.         isDirty = 1
  614. #Except (DebugSize <= 14) and (NumberOfSections > 2.5) and (ExportSize <= 198) and (ResourceSize > 8) and (VirtualSize2 > 83) and (ResourceSize <= 976) => isDirty = 1  (151.0/2.0) [83.0/2.0]
  615.     elif input.DebugSize <= 14 and input.NumberOfSections >= 2.5 and input.ExportSize  <= 198 and input.ResourceSize > 8 and input.VirtualSize2 > 83 and input.ResourceSize <= 976:
  616.         isDirty = 1
  617. #Except (DebugSize <= 14) and (NumberOfSections > 2.5) and (ResourceSize > 1418) and (IatRVA <= 6144) and (VirtualSize2 <= 4) => isDirty = 1  (94.0/0.0) [44.0/0.0]
  618.     elif input.DebugSize <= 14 and input.NumberOfSections >= 2.5 and input.ResourceSize > 1418 and input.IatRVA > 6144 and input.VirtualSize2 <= 4:
  619.         isDirty = 1
  620. #Except (DebugSize <= 14) and (VirtualSize2 > 14) and (NumberOfSections <= 4.5) and (ResourceSize > 8) and (VirtualSize2 <= 2398) and (ResourceSize > 1550) => isDirty = 1  (84.0/0.0) [41.0/1.0]
  621.     elif input.DebugSize <= 14 and input.VirtualSize2 > 14 and input.NumberOfSections > 4.5 and input.ResourceSize > 1550 and input.VirtualSize2 <= 2398:
  622.         isDirty = 1
  623. #Except (DebugSize <= 14) and (VirtualSize2 > 14) and (NumberOfSections <= 4.5) and (ExportSize <= 138.5) and (ImageVersion > 1005) => isDirty = 1  (37.0/0.0) [17.0/0.0]
  624.     elif input.DebugSize <= 14 and input.VirtualSize2 > 14 and input.NumberOfSections > 4.5 and input.ExportSize > 138.5 and input.ImageVersion > 1005:
  625.         isDirty = 1
  626. #Except (ImageVersion <= 5005) and (DebugSize <= 14) and (VirtualSize2 > 14) and (NumberOfSections <= 4.5) => isDirty = 1  (182.0/20.0) [88.0/6.0]
  627.     elif input.ImageVersion <= 5005 and input.DebugSize <= 14 and input.VirtualSize2 > 14 and input.NumberOfSections <= 4.5:
  628.         isDirty = 1
  629. #Except (ImageVersion <= 5005) and (DebugSize <= 14) and (ImageVersion <= 5) and (NumberOfSections > 3.5) and (ExportSize <= 164.5) and (IatRVA <= 73728) and (ResourceSize <= 8722) => isDirty = 1  (47.0/0.0) [18.0/2.0]
  630.     elif input.ImageVersion <= 5005 and input.DebugSize <= 14 and input.ImageVersion <=5 and input.NumberOfSections > 3.5 and input.ExportSize <= 164.5 and input.IatRVA <= 73728 and input.ResourceSize <= 8722:
  631.         isDirty = 1
  632. #Except (ImageVersion <= 5005) and (DebugSize <= 14) and (ResourceSize > 21108) and (ResourceSize <= 37272) and (ImageVersion <= 760) => isDirty = 1  (51.0/0.0) [30.0/3.0]
  633.     elif input.ImageVersion <= 5005 and input.DebugSize <= 14 and input.ResourceSize > 21108 and input.ResourceSize <= 37272 and input.ImageVersion <= 760:
  634.         isDirty = 1
  635. #Except (NumberOfSections > 4.5) and (ExportSize <= 25.5) and (ImageVersion > 1505) and (ResourceSize <= 1020) => isDirty = 1  (51.0/0.0) [30.0/2.0]
  636.     elif input.NumberOfSections > 4.5 and input.ExportSize <= 25.5 and input.ImageVersion > 1505 and input.ResourceSize <= 1020:
  637.         isDirty = 1
  638. # Except (ImageVersion <= 1500) and (NumberOfSections > 5.5) and (ExportSize <= 101) and (ResourceSize <= 3168) => isDirty = 1  (16.0/0.0) [8.0/1.0]
  639.     elif input.ImageVersion <= 1500 and input.NumberOfSections > 5.5 and input.ExportSize <= 101 and input.ResourceSize <= 3168:
  640.         isDirty = 1
  641. #Except (ImageVersion <= 3025) and (DebugSize <= 14) and (ResourceSize > 1182) and (VirtualSize2 > 164) and (ExportSize <= 330.5) => isDirty = 1  (32.0/7.0) [20.0/4.0]
  642.     elif input.ImageVersion <= 3025 and input.DebugSize <= 14 and input.ResourceSize > 1182 and input.VirtualSize2 > 164 and input.ExportSize <= 330.5:
  643.         isDirty = 1
  644. # Except (ImageVersion <= 1010) and (ResourceSize > 2352) and (VirtualSize2 > 39914) and (VirtualSize2 <= 153258) and (VirtualSize2 > 115254) => isDirty = 1  (19.0/0.0) [8.0/2.0]
  645.     elif input.ImageVersion <= 1010 and input.ResourceSize > 2352 and input.VirtualSize2 > 115254 and input.VirtualSize2 <= 153258:
  646.         isDirty = 1
  647. #Except (ImageVersion <= 1500) and (NumberOfSections > 5.5) and (ImageVersion <= 500) and (ExportSize <= 164) and (IatRVA <= 2048) => isDirty = 1  (7.0/0.0) [3.0/0.0]
  648.     elif input.ImageVersion <= 1500 and input.NumberOfSections > 5.5 and input.ImageVersion <= 500 and input.ExportSize <= 164 and input.IatRVA <= 2048:
  649.         isDirty = 1
  650. # Except (ImageVersion <= 1010) and (ResourceSize <= 474) and (IatRVA > 26624) and (VirtualSize2 > 1802) and (IatRVA <= 221348) => isDirty = 1  (15.0/0.0) [5.0/2.0]
  651.     elif input.ImageVersion <= 1010 and input.ResourceSize <= 474 and input.IatRVA > 26624 and input.VirtualSize2 > 1802 and input.IatRVA <= 221348:
  652.         isDirty = 1
  653. # Except (ImageVersion <= 2500) and (DebugSize <= 14) and (ResourceSize > 4320) and (ResourceSize <= 389246) and (ResourceSize > 78678) and (NumberOfSections <= 4) and (ResourceSize <= 120928) => isDirty = 1  (7.0/0.0) [3.0/1.0]
  654.     elif input.ImageVersion <= 2500 and input.DebugSize <= 14 and input.ResourceSize > 78678 and input.ResourceSize <= 120928 and input.NumberOfSections <= 4:
  655.         isDirty = 1
  656. # Except (ImageVersion <= 5005) and (ExportSize <= 25.5) and (NumberOfSections > 3.5) and (ResourceSize > 35814) and (VirtualSize2 > 215352) => isDirty = 1  (5.0/0.0) [1.0/0.0]
  657.     elif input.ImageVersion <= 5005 and input.ExportSize <= 25.5 and input.NumberOfSections > 3.5 and input.ResourceSize > 35814 and input.VirtualSize2 > 215352:
  658.         isDirty = 1
  659. # Except (ImageVersion <= 4005) and (IatRVA <= 2560) and (NumberOfSections > 3.5) and (ImageVersion <= 500) and (ResourceSize > 648) and (ResourceSize <= 62291) => isDirty = 1  (9.0/0.0) [4.0/1.0]
  660.     elif input.ImageVersion <= 500 and input.IatRVA <= 2560 and input.NumberOfSections > 3.5 and input.ResourceSize > 648 and input.ResourceSize <= 62291:
  661.         isDirty = 1
  662. # Except (ExportSize <= 25.5) and (NumberOfSections > 4.5) and (VirtualSize2 > 50765) and (ResourceSize <= 741012) and (ResourceSize > 2512) => isDirty = 1  (13.0/0.0) [6.0/0.0]
  663.     elif input.ExportSize <= 25.5 and input.NumberOfSections > 4.5 and input.VirtualSize2 > 50765 and input.ResourceSize <= 741012 and input.ResourceSize > 2512:
  664.         isDirty = 1
  665. # Except (ImageVersion <= 1010) and (ExportSize <= 25.5) and (VirtualSize2 > 63) and (VirtualSize2 <= 3448) and (ResourceSize > 2032) and (VirtualSize2 > 1200) and (VirtualSize2 <= 3278) => isDirty = 1  (7.0/0.0) [4.0/2.0]
  666.     elif input.ImageVersion <= 1010 and input.ExportSize <= 25.5 and input.VirtualSize2 <= 3278 and input.VirtualSize2 > 1200 and input.ResourceSize > 2032:
  667.         isDirty = 1
  668. #Except (ResourceSize <= 474) and (ExportSize <= 76) and (VirtualSize2 <= 1556) and (IatRVA <= 2368) => isDirty = 1  (13.0/0.0) [2.0/0.0]
  669.     elif input.ResourceSize <= 474 and input.ExportSize <= 76 and input.VirtualSize2 <= 1556 and input.IatRVA <= 2368:
  670.         isDirty = 1
  671. # Except (ImageVersion <= 1500) and (VirtualSize2 <= 6) and (IatRVA > 2048) => isDirty = 1  (8.0/0.0) [4.0/1.0]
  672.     elif input.ImageVersion <= 1500 and input.VirtualSize2 <= 6 and input.IatRVA > 2048:
  673.         isDirty = 1
  674.     else:
  675.         isDirty = 0
  676.     return isDirty
  677.  
  678. # Each algo once; chain results together with equal weight
  679. # IO routine: supply input file in CMD line; output is MALWARE or CLEAN or UNKNOWN
  680. # opts: -i=input file; -n=algorithm number (0 for all)| -h = help
  681. # Possibilities for features in future versions:
  682. # v2: Display verbose - display the rules that were invoked while classifying this file as malware
  683. # v3: Allow rules to be updated (assisted learning occurs)
  684.  
  685. parser = argparse.ArgumentParser(description='Classify an unknown binary as MALWARE or CLEAN.')
  686. parser.add_argument('-f', metavar='filename', help='The name of the input file')
  687. parser.add_argument('-n', metavar='model', help='The ordinal for model classifier: 0=all (default) | 1=J48 | 2=J48Graft | 3=PART | 4=Ridor')
  688. parser.add_argument('-v', nargs='?', metavar='verbose', help='Dump the PE data being processed', const='verbose')
  689.  
  690. args = parser.parse_args()
  691.  
  692. if not args.f:
  693.     parser.print_help()
  694.     sys.exit(0)
  695.  
  696. input = PEFile(args.f)
  697.  
  698. # All variables accessible as values of the 'input' object
  699. if(args.v):
  700.     input.DataDump()
  701.  
  702. # Test input args
  703. if not args.n:
  704.     args.n = 0
  705. args.n = int(args.n)
  706. if args.n < 0 or args.n > 4:
  707.     parser.print_help()
  708.     sys.exit(0)
  709.    
  710. # Options 0: Run all models
  711. if(args.n == 0):
  712.     if DEBUG:
  713.         print 'Processing all...'
  714.     result1 = runJ48()
  715.     result2 = runJ48Graft()
  716.     result3 = runPART()
  717.     result4 = runRidor()
  718.     if ((result1 == result2) and (result2 == result3) and (result3 == result4)):
  719.         printResult(result1)
  720.     else:
  721.         print 'UNKNOWN'
  722.    
  723. # Options 1:  Run J48  
  724. if(args.n == 1):
  725.     if DEBUG:
  726.         print 'Processing J48...'
  727.     printResult(runJ48())
  728.    
  729. # Options 2:  Run J48
  730. if(args.n == 2):
  731.     if DEBUG:
  732.         print 'Processing J48Graft...'
  733.     printResult(runJ48Graft()) 
  734.    
  735. # Option 3:  Run PART
  736. if(args.n == 3):
  737.     if DEBUG:
  738.         print 'Processing PART...'
  739.     printResult(runPART())
  740.  
  741. # Option 4:  Run Ridor
  742. if(args.n == 4):
  743.     if DEBUG:
  744.         print 'Processing Ridor...'
  745.     printResult(runRidor())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement