Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # OpenVAS automation script.
  4. # Mariusz B. / mgeeky, '17
  5. # v0.1
  6. #
  7.  
  8. trap ctrl_c INT
  9.  
  10. # --- CONFIGURATION ---
  11.  
  12. USER=<USERNAME>
  13. PASS=<PASSWORD>
  14. HOST=127.0.0.1
  15. PORT=9390
  16.  
  17. # Must be one of the below defined targets
  18. SCAN_PROFILE=""
  19. #SCAN_PROFILE="Full and fast ultimate"
  20.  
  21. FORMAT="PDF"
  22.  
  23. # A valid "alive_test" parameter
  24. # Defines how it is determined if the targets are alive
  25. # Currently, valid values are the following:
  26. # Scan Config Default
  27. # ICMP, TCP-ACK Service & ARP Ping
  28. # TCP-ACK Service & ARP Ping
  29. # ICMP & ARP Ping
  30. # ICMP & TCP-ACK Service Ping
  31. # ARP Ping
  32. # TCP-ACK Service Ping
  33. # TCP-SYN Service Ping
  34. # ICMP Ping
  35. # Consider Alive
  36. ALIVE_TEST='ICMP, TCP-ACK Service & ARP Ping'
  37.  
  38. # --- END OF CONFIGURATION ---
  39.  
  40. targets=(
  41. "Discovery"
  42. "Full and fast"
  43. "Full and fast ultimate"
  44. "Full and very deep"
  45. "Full and very deep ultimate"
  46. "Host Discovery"
  47. "System Discovery"
  48. )
  49.  
  50. formats=(
  51. "ARF"
  52. "CPE"
  53. "HTML"
  54. "ITG"
  55. "NBE"
  56. "PDF"
  57. "TXT"
  58. "XML"
  59. )
  60.  
  61. able_to_clean=1
  62.  
  63. function usage {
  64. echo
  65. echo -ne "Usage: openvas-automate.sh <host>"
  66. echo
  67. echo -ne "\n host\t- IP address or domain name of the host target."
  68. echo
  69. echo
  70. }
  71.  
  72. function omp_cmd {
  73. cmd="omp -u $USER -w $PASS -h $HOST -p $PORT $@"
  74. #>&2 echo "DBG: OMP cmd: \"$cmd\""
  75. $cmd 2>&1
  76. }
  77.  
  78. function omp_cmd_xml {
  79. omp_cmd "--xml='$@'"
  80. }
  81.  
  82. function end {
  83. echo "[>] Performing cleanup"
  84.  
  85. if [ $able_to_clean -eq 1 ]; then
  86. omp_cmd -D $task_id
  87. omp_cmd -X '<delete_target target_id="'$target_id'"/>'
  88. fi
  89. exit 1
  90. }
  91.  
  92. function ctrl_c() {
  93. echo "[?] CTRL-C trapped."
  94. exit 1
  95. end
  96. }
  97.  
  98. echo
  99. echo " :: OpenVAS automation script."
  100. echo " mgeeky, 0.1"
  101. echo
  102.  
  103. if [ -z "$SCAN_PROFILE" ]; then
  104. echo "[>] Please select scan type:"
  105. echo -e "\t1. Discovery"
  106. echo -e "\t2. Full and fast"
  107. echo -e "\t3. Full and fast ultimate"
  108. echo -e "\t4. Full and very deep"
  109. echo -e "\t5. Full and very deep ultimate"
  110. echo -e "\t6. Host Discovery"
  111. echo -e "\t7. System Discovery"
  112. echo -e "\t9. Exit"
  113. echo ""
  114. echo "--------------------------------"
  115.  
  116. read -p "Please select an option: " m
  117.  
  118. if [ $m -eq 9 ]; then exit 0;
  119. elif [ $m -eq 1 ]; then SCAN_PROFILE="Discovery"
  120. elif [ $m -eq 2 ]; then SCAN_PROFILE="Full and fast"
  121. elif [ $m -eq 3 ]; then SCAN_PROFILE="Full and fast ultimate"
  122. elif [ $m -eq 4 ]; then SCAN_PROFILE="Full and very deep"
  123. elif [ $m -eq 5 ]; then SCAN_PROFILE="Full and very deep ultimate"
  124. elif [ $m -eq 6 ]; then SCAN_PROFILE="Host Discovery"
  125. elif [ $m -eq 7 ]; then SCAN_PROFILE="System Discovery"
  126. else echo "[!] Unknown profile selected" && exit 1
  127. fi
  128. echo
  129. fi
  130.  
  131. found=0
  132.  
  133. for i in "${targets[@]}"
  134. do
  135. if [ "$i" == "$SCAN_PROFILE" ]; then
  136. found=1
  137. break
  138. fi
  139. done
  140.  
  141. scan_profile_id=$(omp_cmd -g | grep "$SCAN_PROFILE" | cut -d' ' -f1)
  142. if [ $found -eq 0 ] || [ -z "$scan_profile_id" ]; then
  143. echo "[!] You've selected unknown SCAN_PROFILE. Please change it in script's settings."
  144. exit 1
  145. fi
  146.  
  147. found=0
  148.  
  149. for i in "${formats[@]}"
  150. do
  151. if [ "$i" == "$FORMAT" ]; then
  152. found=1
  153. break
  154. fi
  155. done
  156.  
  157. format_id=$(omp_cmd -F | grep "$FORMAT" | cut -d' ' -f1)
  158.  
  159. if [ $found -eq 0 ] || [ -z $format_id ]; then
  160. echo "[!] You've selected unknown FORMAT. Please change it in script's settings."
  161. exit 1
  162. fi
  163.  
  164. if [ -z "$1" ]; then
  165. usage
  166. exit 1
  167. fi
  168.  
  169. TARGET="$1"
  170. host "$TARGET" 2>&1 > /dev/null
  171.  
  172. if [ $? -ne 0 ]; then
  173. echo "[!] Specified target host seems to be unavailable!"
  174. read -p "Are you sure you want to continue [Y/n]? " -n 1 -r
  175. echo
  176. if [[ $REPLY =~ ^[Yy]$ ]]
  177. then
  178. echo > /dev/null
  179. else
  180. exit 1
  181. fi
  182. fi
  183.  
  184. echo "[+] Tasked: '$SCAN_PROFILE' scan against '$TARGET' "
  185.  
  186. target_id=$(omp_cmd -T | grep "$TARGET" | cut -d' ' -f1)
  187.  
  188. out=""
  189. if [ -z "$target_id" ]; then
  190.  
  191. echo "[>] Creating a target..."
  192. out=$(omp -u $USER -w $PASS -h $HOST -p $PORT --xml=\
  193. "<create_target>\
  194. <name>${TARGET}</name><hosts>$TARGET</hosts>\
  195. <alive_tests>$ALIVE_TEST</alive_tests>\
  196. </create_target>")
  197. target_id=$(echo "$out" | pcregrep -o1 'id="([^"]+)"')
  198.  
  199. else
  200. echo "[>] Reusing target..."
  201. fi
  202.  
  203. if [ -z "$target_id" ]; then
  204. echo "[!] Something went wrong, couldn't acquire target's ID! Output:"
  205. echo $out
  206. exit 1
  207. else
  208. echo "[+] Target's id: $target_id"
  209. fi
  210.  
  211. echo "[>] Creating a task..."
  212. task_id=$(omp_cmd -C -n "$TARGET" --target=$target_id --config=$scan_profile_id)
  213.  
  214. if [ $? -ne 0 ]; then
  215. echo "[!] Could not create a task."
  216. end
  217. fi
  218.  
  219. echo "[+] Task created successfully, id: '$task_id'"
  220.  
  221. echo "[>] Starting the task..."
  222. report_id=$(omp_cmd -S $task_id)
  223.  
  224. if [ $? -ne 0 ]; then
  225. echo "[!] Could not start a task."
  226. end
  227. fi
  228.  
  229. able_to_clean=0
  230.  
  231. echo "[+] Task started. Report id: $report_id"
  232. echo "[.] Awaiting for it to finish. This will take a long while..."
  233. echo
  234.  
  235. aborted=0
  236. while true; do
  237. RET=$(omp_cmd -G)
  238. if [ $? -ne 0 ]; then
  239. echo '[!] Querying jobs failed.';
  240. end
  241. fi
  242.  
  243. RET=$(echo -n "$RET" | grep -m1 "$task_id" | tr '\n' ' ')
  244. out=$(echo "$RET" | tr '\n' ' ')
  245. echo -ne "$out\r"
  246. if [ `echo "$RET" | grep -m1 -i "fail"` ]; then
  247. echo '[!] Failed getting running jobs list'
  248. end
  249. fi
  250. echo "$RET" | grep -m1 -i -E "done|Stopped"
  251. if [ $? -ne 1 ]; then
  252. aborted=1
  253. break
  254. fi
  255. sleep 1
  256.  
  257. done
  258.  
  259. if [ $aborted -eq 0 ]; then
  260. echo "[+] Job done, generating report..."
  261.  
  262. FILENAME=${TARGET// /_}
  263. FILENAME="openvas_${FILENAME//[^a-zA-Z0-9_\.\-]/}_$(date +%s)"
  264.  
  265. out=$(omp_cmd --get-report $report_id --format $format_id > $FILENAME.$FORMAT )
  266.  
  267. if [ $? -ne 0 ]; then
  268. echo '[!] Failed getting report.';
  269. echo "[!] Output: $out"
  270. #end
  271. fi
  272.  
  273. echo "[+] Scanning done."
  274. else
  275. echo "[?] Scan monitoring has been aborted. You're on your own now."
  276. fi
  277. #end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement