find files containing a specific strings like encrypted malware


SUBMITTED BY: alemotta

DATE: Aug. 22, 2017, 3:02 p.m.

UPDATED: Aug. 22, 2017, 3:03 p.m.

FORMAT: Text only

SIZE: 2.4 kB

HITS: 376

  1. # find files containing a specific strings like encrypted malware
  2. #Import os module
  3. import os
  4. import fnmatch
  5. # Ask the user to enter string to search
  6. search_path = input("Enter directory path to search : ")
  7. file_type = input("File Type : ")
  8. search_str = input("Enter the search string : ")
  9. result_txt_file = input("Output file : ")
  10. def sessionSsave(nSess):
  11. sessFile = open(result_txt_file,"a+")
  12. #sessnum = sessFile.readlines()
  13. #numer = len(sessnum)
  14. sessFile.write(str(nSess))
  15. sessFile.close()
  16. try:
  17. # Append a directory separator if not already present
  18. if not (search_path.endswith("/") or search_path.endswith("\\") ):
  19. search_path = search_path + "/"
  20. # If path does not exist, set search path to current directory
  21. if not os.path.exists(search_path):
  22. search_path ="."
  23. i = 1
  24. # Repeat for each file in the directory
  25. #for fname in os.walk(search_path):
  26. for root, dirs, files in os.walk(search_path):
  27. for filename in fnmatch.filter(files, file_type):
  28. # Open file for reading
  29. fo = open(os.path.join(root, filename), encoding="ISO-8859-1")
  30. #print (i)
  31. #i +=1
  32. # Read the first line from the file
  33. line = fo.readline()
  34. # Initialize counter for line number
  35. line_no = 1
  36. try:
  37. # Loop until EOF
  38. while line != '' :
  39. # Search for string in line
  40. index = line.find(search_str)
  41. if ( index != -1) :
  42. print(filename, "[", line_no, ",", index, "] ", line, sep="")
  43. txtline =str(root)+"/"+str( filename)+ "["+ str(line_no) + ","+ str(index) + "] "+ str(line)
  44. sessionSsave( txtline)
  45. # Read next line
  46. line = fo.readline()
  47. # Increment line counter
  48. line_no += 1
  49. except Exception as e:
  50. print ("eroor message" + str(e))
  51. # Close the files
  52. fo.close()
  53. except Exception as e:
  54. print ("eroor message" + str(e))

comments powered by Disqus