Advertisement
Guest User

Untitled

a guest
Sep 12th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. #/bin/env/python3
  2. #Easy as PIE - Process Information Enumeration
  3. import psutil,hashlib,sys
  4. from uuid import uuid4
  5.  
  6. def hash_file(file):
  7. # uuid is used to generate a random number
  8. salt = uuid4().hex
  9. hashed = hashlib.sha256()
  10. with open(file, 'rb') as ofile:
  11. buf = ofile.read()
  12. hashed.update(buf)
  13. return hashed.hexdigest()
  14.  
  15. def check_file(hashed_file, new_file):
  16. # print("recalled hash: ",hashed_password)
  17. # print("user pass: ",user_password)
  18. password, salt = hashed_file.split(':')
  19. hashed = hashlib.sha256(salt.encode() + new_file.encode()).hexdigest()
  20.  
  21. return password == hashed
  22.  
  23. def format_neat_output(proclist):
  24.  
  25. for proc in proclist: #iterate each tuple
  26. ex_states = ("LISTEN",None)
  27. if not proc[9]:
  28. raddr = "N/A"
  29. rport = "N/A"
  30. else:
  31. raddr = proc[9][0]
  32. rport = proc[9][1]
  33.  
  34. print(
  35.  
  36. """Process ID -> {pid}
  37. ***************************
  38. Name ---------------------> {proc_name}
  39. File Path ================> {file_path}
  40. File Sha256 Hash ---------> {hash}
  41. Working Dir ==============> {cwd}
  42. Status -------------------> {status}
  43. Running Under User =======> {perms}
  44. Socket State -------------> {state}
  45. Listening Address ========> {laddr}
  46. Local Port ---------------> {lport}
  47. Remote Address ===========> {raddr}
  48. Remote Port --------------> {rport}
  49. """.format(pid=proc[0],proc_name=proc[1],file_path=proc[2],hash=proc[3],cwd=proc[5],status=proc[4],
  50. perms=proc[6],laddr=proc[8][0],lport=proc[8][1],raddr=raddr,rport=rport,state=proc[10]))
  51. if len(proc[11]) != 0:
  52. children = proc[11]
  53. for child in children:
  54. print(
  55. """ \tChild ID -> {pid}
  56. ***************************
  57. Name ---------------------> {ch_name}
  58. File Path=================> {ch_exe}
  59. File Sha256 Hash ---------> {hash}
  60. """.format(pid=child[0],ch_name=child[1],ch_exe=child[2],hash=child[3]))
  61.  
  62.  
  63. # print("""
  64. # {pid} {status} {perms} {state} {laddr} {lport} {raddr} {rport} {file_path}
  65. # """.format(pid=proc[0],proc_name=proc[1],file_path=proc[2],hash=proc[3],status=proc[4],
  66. # perms=proc[5],laddr=proc[7][0],lport=proc[7][1],raddr=raddr,rport=rport,state=proc[9]))
  67. # sys.exit(0)
  68.  
  69. def baseline_procs():
  70. conn_list = psutil.net_connections() #Return a tuple
  71. net_list = [] #maps a pid to its process information
  72. family_names = []
  73. #the proc_tup tuple contains objects related to the current process
  74. # a series of proc_tup tuples are in the larger net_tup tuple
  75.  
  76. for processes in conn_list:
  77. #iterate over all the found network processes to extract their
  78. #process IDs
  79. # The following are the mappings
  80. # Below are the mappings for the Parent Tuple
  81. # 0 - process id
  82. # 1 - pid name
  83. # 2 - path to the process's file
  84. # 3 - hash of the file in #3
  85. # 4 - the status of the process
  86. # 5 - the process working dir
  87. # 6 - process's running permissions
  88. # 7 - any file the process has open
  89. # 8 - tuple - (listening addr,lport)
  90. # 9 - tuple - (remote addr,rport)
  91. # 10 - the state of the connection
  92. # 11 - children processes tuple in tuple with children details ((child1 name1, child1 exe-path1),(2,2).....)
  93. #The Following is the mapping for any children tuples
  94. # 0 - child pid
  95. # 1 - chile process name
  96. # 2 - path to child process exe
  97. # 3 - hash of #2
  98. ppid = processes[6] #processes[6] is the pid value
  99. ppid_object = psutil.Process(ppid)
  100. children_list = []
  101. children = ppid_object.children()
  102. if children:
  103. for child in children:
  104. children_list.append((child.pid,child.name(),child.exe(),hash_file(child.exe())))
  105.  
  106. proc_tup = (ppid,ppid_object.name(),ppid_object.exe(),hash_file(ppid_object.exe()),ppid_object.status(),
  107. ppid_object.cwd(),ppid_object.username(),ppid_object.open_files(),processes[3],
  108. processes[4],processes[5],children_list)
  109. #Load the tuple with as much pertinent info about the process
  110. #and associated netowkr connections
  111.  
  112.  
  113. net_list.append(proc_tup) #append the tuple to the list
  114. return net_list
  115. if __name__ == '__main__':
  116. format_neat_output(baseline_procs())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement