Advertisement
MONaH-Rasta

/jffs/scripts/hosts-update-script.sh

Dec 8th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.69 KB | None | 0 0
  1. #!/bin/sh
  2. # This is Hosts Update Script (HUS)
  3. # Created for use in RT-N66U with [Fork] Asuswrt-Merlin 374.43 LTS releases
  4. # Default path: /jffs/scripts/hosts-update-script.sh
  5. # More info: //4pda.ru/forum/index.php?showtopic=491608&view=findpost&p=91762386
  6.  
  7.  
  8. PATH=/opt/sbin:/opt/bin:/opt/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  9.  
  10.  
  11. # 0.0.0.0 is defined as a non-routable meta-address used to designate an invalid, unknown, or non applicable target. Using 0.0.0.0 is empirically faster, possibly because there's no wait for a timeout resolution
  12. ENDPOINT_IP4="0.0.0.0"
  13. BLIST="/tmp/mnt/microsd/dnsmasq/blocking_black.list"
  14. WLIST="/tmp/mnt/microsd/dnsmasq/blocking_white.list"
  15. TMPDIR="/tmp/mnt/microsd/dnsmasq/blocking_initial.list"
  16. SRTDIR="/tmp/mnt/microsd/dnsmasq/blocking_sorted.list"
  17. TARGET="/tmp/mnt/microsd/dnsmasq/hosts.blocked"
  18.  
  19.  
  20. logger "HUS: start"
  21. while [ $(date "+%Y") -lt 2019 ]; do
  22.     sleep 5
  23. done
  24. logger "HUS: time seems synchronized now, starting hosts update"
  25. rm -f "$TARGET" "$TMPDIR" "$SRTDIR"
  26. touch "$TARGET"
  27.  
  28.  
  29. # Download and process the files needed to make the lists (enable/add more, if you want)
  30. ######## ENABLED START ########
  31.  
  32. # focus on ad related domains
  33. LISTNAME="Peter Lowe"
  34. logger "HUS: downloading hosts file $LISTNAME"
  35. wget -qO- "https://pgl.yoyo.org/as/serverlist.php?hostformat=hosts&showintro=1&mimetype=plaintext" | awk -v r="$ENDPOINT_IP4" '{sub(/^127.0.0.1/, r)} $0 ~ "^"r' > "$TMPDIR"
  36. logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  37.  
  38. # focus on malware
  39. LISTNAME="MalwareDomainList.com"
  40. logger "HUS: downloading hosts file $LISTNAME"
  41. wget -qO- "https://www.malwaredomainlist.com/hostslist/hosts.txt" | awk -v r="$ENDPOINT_IP4" '{sub(/^127.0.0.1/, r)} $0 ~ "^"r' >> "$TMPDIR"
  42. logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  43.  
  44. # focus on mobile ads
  45. LISTNAME="AdAway default blocklist"
  46. logger "HUS: downloading hosts file $LISTNAME"
  47. wget -qO- "https://adaway.org/hosts.txt" | awk -v r="$ENDPOINT_IP4" '{sub(/^127.0.0.1/, r)} $0 ~ "^"r' >> "$TMPDIR"
  48. logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  49.  
  50. # broad blocklist
  51. LISTNAME="by Dan Pollock"
  52. logger "HUS: downloading hosts file $LISTNAME"
  53. wget -qO- "https://someonewhocares.org/hosts/hosts" | awk -v r="$ENDPOINT_IP4" '{sub(/^127.0.0.1/, r)} $0 ~ "^"r' >> "$TMPDIR"
  54. logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  55.  
  56. # focus on malicious bitcoin mining sites
  57. LISTNAME="NoCoin Filter List"
  58. logger "HUS: downloading hosts file $LISTNAME"
  59. wget -qO- "https://raw.githubusercontent.com/hoshsadiq/adblock-nocoin-list/master/hosts.txt" | awk -v r="$ENDPOINT_IP4" '{sub(/^0.0.0.0/, r)} $0 ~ "^"r' >> "$TMPDIR"
  60. logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  61.  
  62. # focus on malvertising by disconnect.me
  63. LISTNAME="Malvertising list by Disconnect"
  64. logger "HUS: downloading hosts file $LISTNAME"
  65. wget -qO- "https://s3.amazonaws.com/lists.disconnect.me/simple_malvertising.txt" | grep -v -e ^# -e ^$ | awk -v r="$ENDPOINT_IP4 " '{sub(//, r)} $0 ~ "^"r' >> "$TMPDIR"
  66. logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  67.  
  68. # focus on Windows installers ads sources
  69. LISTNAME="Windows installers ads sources"
  70. logger "HUS: downloading hosts file $LISTNAME"
  71. wget -qO- "https://raw.githubusercontent.com/FadeMind/hosts.extras/master/UncheckyAds/hosts" | awk -v r="$ENDPOINT_IP4" '{sub(/^0.0.0.0/, r)} $0 ~ "^"r' >> "$TMPDIR"
  72. logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  73.  
  74. # focus on sites which have used DMCA takedowns to force removal from other blacklists
  75. LISTNAME="BarbBlock"
  76. logger "HUS: downloading hosts file $LISTNAME"
  77. wget -qO- "https://paulgb.github.io/BarbBlock/blacklists/domain-list.txt" | grep -v -e ^# -e ^$ | awk -v r="$ENDPOINT_IP4 " '{sub(//, r)} $0 ~ "^"r' >> "$TMPDIR"
  78. logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  79.  
  80. # broad blocklist
  81. LISTNAME="MVPS"
  82. logger "HUS: downloading hosts file $LISTNAME"
  83. wget -qO- "https://winhelp2002.mvps.org/hosts.txt" | awk -v r="$ENDPOINT_IP4" '{sub(/^0.0.0.0/, r)} $0 ~ "^"r' >> "$TMPDIR"
  84. logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  85.  
  86. # focus on ads and tracking domains
  87. LISTNAME="Lightswitch05's ads-and-tracking-extended"
  88. logger "HUS: downloading hosts file $LISTNAME"
  89. wget -qO- "https://www.github.developerdan.com/hosts/lists/ads-and-tracking-extended.txt" | awk -v r="$ENDPOINT_IP4" '{sub(/^0.0.0.0/, r)} $0 ~ "^"r' >> "$TMPDIR"
  90. logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  91.  
  92. ######## ENABLED FINISH ########
  93.  
  94.  
  95. ######## DISABLED START ########
  96. # focus on intrusive online consultants and callback widgets
  97. #LISTNAME=""
  98. #logger "HUS: downloading hosts file $LISTNAME"
  99. #wget -qO- "https://raw.githubusercontent.com/pafnuty/onlineConsultantBlocker/master/hosts" | awk -v r="$ENDPOINT_IP4" '{sub(/^0.0.0.0/, r)} $0 ~ "^"r' >> "$TMPDIR"
  100. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  101. #
  102. # focus on zeus botnet by abuse.ch
  103. #LISTNAME=""
  104. #logger "HUS: downloading hosts file $LISTNAME"
  105. #wget -qO- "https://zeustracker.abuse.ch/blocklist.php?download=hostfile" | awk -v r="$ENDPOINT_IP4" '{sub(/^127.0.0.1/, r)} $0 ~ "^"r' >> "$TMPDIR"
  106. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  107. #
  108. # focus on ransomware by abuse.ch
  109. #LISTNAME=""
  110. #logger "HUS: downloading hosts file $LISTNAME"
  111. #wget -qO- "https://ransomwaretracker.abuse.ch/downloads/RW_DOMBL.txt" | grep -v -e ^# -e ^$ | awk -v r="$ENDPOINT_IP4 " '{sub(//, r)} $0 ~ "^"r' >> "$TMPDIR"
  112. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  113. #
  114. # focus on malware
  115. #LISTNAME=""
  116. #logger "HUS: downloading hosts file $LISTNAME"
  117. #wget -qO- "https://mirror1.malwaredomains.com/files/justdomains" | grep -v -e ^# -e ^$ | awk -v r="$ENDPOINT_IP4 " '{sub(//, r)} $0 ~ "^"r' >> "$TMPDIR"
  118. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  119. #
  120. # generic blocklist, fewer false positives down to the low sensitivity list with more false positives
  121. #LISTNAME=""
  122. #logger "HUS: downloading hosts file $LISTNAME"
  123. #wget -qO- "https://dshield.org/feeds/suspiciousdomains_High.txt" | grep -v -e ^# -e ^$ | awk -v r="$ENDPOINT_IP4 " '{sub(//, r)} $0 ~ "^"r' >> "$TMPDIR"
  124. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  125. #
  126. # broad blocklist
  127. #LISTNAME=""
  128. #logger "HUS: downloading hosts file $LISTNAME"
  129. #wget -qO- "https://hosts-file.net/ad_servers.txt" | awk -v r="$ENDPOINT_IP4" '{sub(/^127.0.0.1/, r)} $0 ~ "^"r' >> "$TMPDIR"
  130. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  131. #
  132. # generic blocklist
  133. #LISTNAME=""
  134. #logger "HUS: downloading hosts file $LISTNAME"
  135. #wget -qO- "https://dshield.org/feeds/suspiciousdomains_Medium.txt" | grep -v -e ^# -e ^$ | awk -v r="$ENDPOINT_IP4 " '{sub(//, r)} $0 ~ "^"r' >> "$TMPDIR"
  136. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  137. #
  138. # generic blocklist
  139. #LISTNAME=""
  140. #logger "HUS: downloading hosts file $LISTNAME"
  141. #wget -qO- "https://dshield.org/feeds/suspiciousdomains_Low.txt" | grep -v -e ^# -e ^$ | awk -v r="$ENDPOINT_IP4 " '{sub(//, r)} $0 ~ "^"r' >> "$TMPDIR"
  142. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  143. #
  144. # focus on windows telemetry domains
  145. #LISTNAME=""
  146. #logger "HUS: downloading hosts file $LISTNAME"
  147. #wget -qO- "https://raw.githubusercontent.com/crazy-max/WindowsSpyBlocker/master/data/hosts/spy.txt" | awk -v r="$ENDPOINT_IP4" '{sub(/^0.0.0.0/, r)} $0 ~ "^"r' >> "$TMPDIR"
  148. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  149. #
  150. # focus on malware by disconnect.me
  151. #LISTNAME=""
  152. #logger "HUS: downloading hosts file $LISTNAME"
  153. #wget -qO- "https://s3.amazonaws.com/lists.disconnect.me/simple_malware.txt" | grep -v -e ^# -e ^$ | awk -v r="$ENDPOINT_IP4 " '{sub(//, r)} $0 ~ "^"r' >> "$TMPDIR"
  154. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  155. #
  156. # focus on malicious bitcoin mining sites
  157. #LISTNAME=""
  158. #logger "HUS: downloading hosts file $LISTNAME"
  159. #wget -qO- "https://raw.githubusercontent.com/greatis/Anti-WebMiner/master/hosts" | awk -v r="$ENDPOINT_IP4" '{sub(/^0.0.0.0/, r)} $0 ~ "^"r' >> "$TMPDIR"
  160. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  161. #
  162. # focus on dead websites (against typos in URLs)
  163. #LISTNAME=""
  164. #logger "HUS: downloading hosts file $LISTNAME"
  165. #wget -qO- "https://raw.githubusercontent.com/FadeMind/hosts.extras/master/add.Dead/hosts" | awk -v r="$ENDPOINT_IP4" '{sub(/^0.0.0.0/, r)} $0 ~ "^"r' >> "$TMPDIR"
  166. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  167. #
  168. # focus on malicious bitcoin mining sites
  169. #LISTNAME=""
  170. #logger "HUS: downloading hosts file $LISTNAME"
  171. #wget -qO- "https://gitlab.com/ZeroDot1/CoinBlockerLists/raw/master/hosts" | awk -v r="$ENDPOINT_IP4" '{sub(/^0.0.0.0/, r)} $0 ~ "^"r' >> "$TMPDIR"
  172. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  173. #
  174. # broad blocklist
  175. #LISTNAME=""
  176. #logger "HUS: downloading hosts file $LISTNAME"
  177. #wget -qO- "https://raw.githubusercontent.com/mitchellkrogza/Badd-Boyz-Hosts/master/hosts" | awk -v r="$ENDPOINT_IP4" '{sub(/^0.0.0.0/, r)} $0 ~ "^"r' >> "$TMPDIR"
  178. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  179. #
  180. # focus on 2o7 network tracking
  181. #LISTNAME=""
  182. #logger "HUS: downloading hosts file $LISTNAME"
  183. #wget -qO- "https://raw.githubusercontent.com/azet12/KADhosts/master/KADhosts.txt" | awk -v r="$ENDPOINT_IP4" '{sub(/^0.0.0.0/, r)} $0 ~ "^"r' >> "$TMPDIR"
  184. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  185. #
  186. # focus on mobile trackers
  187. #LISTNAME=""
  188. #logger "HUS: downloading hosts file $LISTNAME"
  189. #wget -qO- "https://vqs.gryzor.com/overall_trackers.txt" | awk -v r="$ENDPOINT_IP4" '{sub(/^127.0.0.1/, r)} $0 ~ "^"r' >> "$TMPDIR"
  190. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  191. #
  192. # focus on trackers
  193. #LISTNAME=""
  194. #logger "HUS: downloading hosts file $LISTNAME"
  195. #wget -qO- "https://quidsup.net/notrack/blocklist.php?download | grep -v -e ^# -e ^$ | awk -v r="$ENDPOINT_IP4 " '{sub(//, r)} $0 ~ "^"r' >> "$TMPDIR"
  196. #logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  197. ######## DISABLED FINISH ########
  198.  
  199.  
  200. # Add blacklist, if non-empty
  201. if [ -s "$BLIST" ]
  202. then
  203.     logger "HUS: adding blacklist entries"
  204.     awk -v r="$ENDPOINT_IP4" '/^[^#]/ { print r,$1 }' "$BLIST" >> "$TMPDIR"
  205.     logger "HUS: $(wc -l $TMPDIR | awk '{print $1;}')"
  206. else
  207.     logger "HUS: no blacklist found"
  208. fi
  209.  
  210. # Sort list
  211. logger "HUS: sorting list"
  212. awk '{sub(/\r$/,"");print $1,$2}' "$TMPDIR" | sort -u > "$SRTDIR"
  213. logger "HUS: $(wc -l $SRTDIR | awk '{print $1;}')"
  214.  
  215.  
  216. # Filter (if applicable)
  217. if [ -s "$WLIST" ]
  218. then
  219.     # Filter the blacklist, suppressing whitelist matches
  220.     # This is relatively slow
  221.     logger "HUS: filtering blacklist, suppressing whitelist matches"
  222.     egrep -v "^[[:space:]]*$" "$WLIST" | awk '/^[^#]/ {sub(/\r$/,"");print $1}' | grep -vf - "$SRTDIR" > "$TARGET"
  223. else
  224.     logger "HUS: no whitelist found"
  225.     cat "$SRTDIR" > "$TARGET"
  226. fi
  227.  
  228.  
  229. logger "HUS: hosts file has been successfully updated."
  230. logger "HUS: $(wc -l $TARGET)"
  231.  
  232.  
  233. # Delete files used to build list to free up the limited space
  234. rm -f "$TMPDIR" "$SRTDIR"
  235.  
  236. # Restart dnsmasq
  237. #killall -SIGHUP dnsmasq
  238. service restart_dnsmasq
  239.  
  240. logger "HUS: finish"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement