Advertisement
Guest User

code

a guest
Jul 20th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from datetime import datetime
  3. import sys
  4. import os
  5. from os import listdir
  6. import re
  7.  
  8. def show_help():
  9. message='''
  10. ********************************************************
  11. * Simpler - A simple simplifier ;) *
  12. * Version 1.0 *
  13. ********************************************************
  14. Usage: python3 simpler.py [options]
  15.  
  16. Options:
  17. -h/--help : This help
  18. -s : Statistics
  19. -l : List the attackers IP
  20. -p : ping an attacker IP
  21. '''
  22. print(message)
  23.  
  24. def show_header():
  25. print('''***********************************************
  26. _ _
  27. ___(_)_ __ ___ _ __ | | ___ _ __ _ __ _ _
  28. / __| | '_ ` _ \| '_ \| |/ _ \ '__| '_ \| | | |
  29. \__ \ | | | | | | |_) | | __/ |_ | |_) | |_| |
  30. |___/_|_| |_| |_| .__/|_|\___|_(_)| .__/ \__, |
  31. |_| |_| |___/
  32. @ironhackers.es
  33.  
  34. ***********************************************
  35. ''')
  36.  
  37. def show_statistics():
  38. path = '/home/pepper/Web/Logs/'
  39. print('Statistics\n-----------')
  40. listed_files = listdir(path)
  41. count = len(listed_files)
  42. print('Number of Attackers: ' + str(count))
  43. level_1 = 0
  44. dat = datetime(1, 1, 1)
  45. ip_list = []
  46. reks = []
  47. ip = ''
  48. req = ''
  49. rek = ''
  50. for i in listed_files:
  51. f = open(path + i, 'r')
  52. lines = f.readlines()
  53. level2, rek = get_max_level(lines)
  54. fecha, requ = date_to_num(lines)
  55. ip = i.split('.')[0] + '.' + i.split('.')[1] + '.' + i.split('.')[2] + '.' + i.split('.')[3]
  56. if fecha > dat:
  57. dat = fecha
  58. req = requ
  59. ip2 = i.split('.')[0] + '.' + i.split('.')[1] + '.' + i.split('.')[2] + '.' + i.split('.')[3]
  60. if int(level2) > int(level_1):
  61. level_1 = level2
  62. ip_list = [ip]
  63. reks=[rek]
  64. elif int(level2) == int(level_1):
  65. ip_list.append(ip)
  66. reks.append(rek)
  67. f.close()
  68.  
  69. print('Most Risky:')
  70. if len(ip_list) > 1:
  71. print('More than 1 ip found')
  72. cont = 0
  73. for i in ip_list:
  74. print(' ' + i + ' - Attack Level : ' + level_1 + ' Request: ' + reks[cont])
  75. cont = cont + 1
  76.  
  77. print('Most Recent: ' + ip2 + ' --> ' + str(dat) + ' ' + req)
  78.  
  79. def list_ip():
  80. print('Attackers\n-----------')
  81. path = '/home/pepper/Web/Logs/'
  82. listed_files = listdir(path)
  83. for i in listed_files:
  84. f = open(path + i,'r')
  85. lines = f.readlines()
  86. level,req = get_max_level(lines)
  87. print(i.split('.')[0] + '.' + i.split('.')[1] + '.' + i.split('.')[2] + '.' + i.split('.')[3] + ' - Attack Level : ' + level)
  88. f.close()
  89.  
  90. def date_to_num(lines):
  91. dat = datetime(1,1,1)
  92. ip = ''
  93. req=''
  94. for i in lines:
  95. if 'Level' in i:
  96. fecha=(i.split(' ')[6] + ' ' + i.split(' ')[7]).split('\n')[0]
  97. regex = '(\d+)-(.*)-(\d+)(.*)'
  98. logEx=re.match(regex, fecha).groups()
  99. mes = to_dict(logEx[1])
  100. fecha = logEx[0] + '-' + mes + '-' + logEx[2] + ' ' + logEx[3]
  101. fecha = datetime.strptime(fecha, '%Y-%m-%d %H:%M:%S')
  102. if fecha > dat:
  103. dat = fecha
  104. req = i.split(' ')[8] + ' ' + i.split(' ')[9] + ' ' + i.split(' ')[10]
  105. return dat, req
  106.  
  107. def to_dict(name):
  108. month_dict = {'Jan':'01','Feb':'02','Mar':'03','Apr':'04', 'May':'05', 'Jun':'06','Jul':'07','Aug':'08','Sep':'09','Oct':'10','Nov':'11','Dec':'12'}
  109. return month_dict[name]
  110.  
  111. def get_max_level(lines):
  112. level=0
  113. for j in lines:
  114. if 'Level' in j:
  115. if int(j.split(' ')[4]) > int(level):
  116. level = j.split(' ')[4]
  117. req=j.split(' ')[8] + ' ' + j.split(' ')[9] + ' ' + j.split(' ')[10]
  118. return level, req
  119.  
  120. def exec_ping():
  121. forbidden = ['&', ';', '-', '`', '||', '|']
  122. command = input('Enter an IP: ')
  123. for i in forbidden:
  124. if i in command:
  125. print('Got you')
  126. exit()
  127. os.system('ping ' + command)
  128.  
  129. if __name__ == '__main__':
  130. show_header()
  131. if len(sys.argv) != 2:
  132. show_help()
  133. exit()
  134. if sys.argv[1] == '-h' or sys.argv[1] == '--help':
  135. show_help()
  136. exit()
  137. elif sys.argv[1] == '-s':
  138. show_statistics()
  139. exit()
  140. elif sys.argv[1] == '-l':
  141. list_ip()
  142. exit()
  143. elif sys.argv[1] == '-p':
  144. exec_ping()
  145. exit()
  146. else:
  147. show_help()
  148. exit()
  149. www-data@jarvis:/var/www/Admin-Utilities$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement