badips.ksh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. ########################################################################
  2. ###
  3. ##
  4. #
  5. # Author: Stéphane HUC
  6. # mail: devs@stephane-huc.net
  7. # gpg:fingerprint: CE2C CF7C AB68 0329 0D20 5F49 6135 D440 4D44 BD58
  8. #
  9. # License: BSD Simplified 2 Clauses
  10. #
  11. # Github: https://framagit.org/BlackLists/BlockZones.git
  12. #
  13. # Date: 2017/07/16
  14. #
  15. ##
  16. ###
  17. ########################################################################
  18. ###
  19. ##
  20. # For *BSD: ksh, ksh93...
  21. ##
  22. ###
  23. ########################################################################
  24. list="badips"
  25. FILES[0]="${DIR_SRC}/${list}_ipv4"
  26. FILES[1]="${DIR_SRC}/${list}_ipv6"
  27. ########################################################################
  28. ###
  29. ##
  30. # Functions
  31. ##
  32. ###
  33. ########################################################################
  34. . "${DIR_INC}/commons_functions.ksh"
  35. # Create uniq list file by datas into array blocklist
  36. mng_blocklists() {
  37. display_mssg "#" "### Attempt to read datas into array blocklist"
  38. count="${#blocklists[@]}"
  39. if [ "${count}" -gt 0 ]; then
  40. for url in "${blocklists[@]}"; do
  41. ndd="$(echo "${url}" | awk -F'/' '{ print $3 }')"
  42. file="$(echo "${url##*/}" | sed -e 's#\?#_#g;s#=#_#g;s#php#txt#g;s#\&#_#g')";
  43. name="${ndd}_${file}"
  44. filename="${DIR_DL}/${name}"
  45. #printf "file: %s \n" "${file}"
  46. # define seconds before new dl
  47. case "${ndd}" in
  48. "feeds.dshield.org") seconds=259200 ;; # 3 days
  49. "lists.blocklist.de") seconds=172800 ;; # 2 days
  50. "myip.ms") seconds=864000 ;; # 10 days
  51. "ransomwaretracker.abuse.ch") seconds=2592000 ;; # 30 days
  52. #"sslbl.abuse.ch") seconds=900 ;; # 15 min.
  53. #"www.openbl.org") seconds=172800 ;; # 2 days
  54. "www.spamhaus.org") seconds=3600;; # 1 hours
  55. #*) seconds=86400;;
  56. esac
  57. if [ -f "${filename}" ]; then
  58. # get file seconds stat
  59. file_seconds=$(stat -f "%m" -t "%s" "${filename}")
  60. # calcul diff time in seconds
  61. diff_sec=$(echo "${today} - ${file_seconds}" | bc)
  62. #unset file_seconds
  63. if [ ${diff_sec} -gt ${seconds} ]; then download; fi
  64. else
  65. download
  66. fi
  67. case "${ndd}" in
  68. "www.openbl.org")
  69. uncompress
  70. filename="${filename%.gz}"
  71. ;;
  72. esac
  73. purge_files
  74. make_uniq_list
  75. unset filename
  76. done
  77. else
  78. display_mssg "KO" "It seems not have datas!"
  79. byebye
  80. fi
  81. unset count
  82. }
  83. purge_files() {
  84. display_mssg "hb" "===> Attempt to transform downloaded file: ${filename}"
  85. # /^$/d <= empty line
  86. # /^#/d <= line starting with #
  87. # /^\s*$/d <= remove spaces'n tabs; /^[[:space:]]*$/d
  88. # s/ \+//g <= remove all spaces
  89. # s/ \+/ /g <= replace more spaces by only one
  90. case "${ndd}" in
  91. "danger.rulez.sk") # delete line ^#, and keep only adr ip
  92. #sed -i -e "/^#/d;s/^\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)\t.*/\1/g;s/ \+//g" "${filename}"
  93. sed -i -e "/^#/d;s/\(.*\)#\(.*\)/\1/g" "${filename}"
  94. ;;
  95. "feeds.dshield.org") # delete empty lines, and ^#
  96. #sed -i -e "/^$/d;s#^Start\(.*\)#\#Start\1#g;/^#/d;s#^\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)\t.*#\1#g;s/ \+//g" "${filename}"
  97. sed -i -e "/^$/d;s#^Start\(.*\)#\#Start\1#g;/^#/d;s/ \+//g" "${filename}"
  98. rm -f "${filename}_awk"
  99. while read -r line; do
  100. echo "${line}" | awk '{print $1"/"$3}' >> "${filename}_awk"
  101. done < "${filename}"
  102. filename="${filename}_awk"
  103. #sed -i -e "s#\(.*\)\/$#\1#g" "${filename}"
  104. ;;
  105. "malc0de.com") # delete empty lines, and ^//
  106. sed -i -e "/^$/d;/^\/\//d;s/ \+//g" "${filename}"
  107. ;;
  108. "myip.ms") # delete empty lines, spaces, and keep only adr ip
  109. sed -i -e "/^$/d;/^#/d;s/\(.*\)#\(.*\)/\1/g;s/ \+//g" "${filename}"
  110. ;;
  111. "sslbl.abuse.ch") # delete empty lines, ^#, ...
  112. #sed -i -e "/^$/d;/^#/d;s#^\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)\,.*#\1#g;s/ \+//g" "${filename}"
  113. sed -i -e "/^$/d;/^#/d;s#\(.*\),\(.*\),\(.*\)#\1#g;s/ \+//g" "${filename}"
  114. ;;
  115. "www.spamhaus.org") # delete empty lines, ...
  116. sed -i -e "/^\;/d;s/ \+//g;s#\(.*\) \; SBL\(.*\)#\1#g" "${filename}"
  117. ;;
  118. *) # delete empty lines, ^#, keep all others :p
  119. sed -i -e "/^$/d;/^#/d;s/ \+//g" "${filename}"
  120. ;;
  121. esac
  122. }
  123. transformer() {
  124. #for f in "${DIR_SRC}/${list}_ipv4" "${DIR_SRC}/${list}_ipv6"; do
  125. for f in "${FILES[@]}"; do
  126. display_mssg "hb" "===> Attempt to transform uniq list '${f}' to badips files"
  127. if [ -f "${f}" ]; then
  128. output="$(basename "${f}")"
  129. if mv "${f}" "${DIR_LISTS}/${output}"; then
  130. display_mssg "OK" "The file '${DIR_LISTS}/${output}' seems to be build!"
  131. fi
  132. build_sums
  133. unset output
  134. else
  135. display_mssg "KO" "It seems no file: '${f}' !"
  136. fi
  137. done
  138. }