Advertisement
quark_zju

reject_malicious_dns_ips.rb

Jan 21st, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env ruby
  2.  
  3. # Generate iptables command to drop malicious (injected) DNS responses from you-know-who servers.
  4. # nslookup www.twitter.com 8.8.8.8 # should works
  5.  
  6. require 'ipaddr'
  7.  
  8. iptables_template = "iptables -I INPUT -p udp -m udp --sport 53 -m u32 --u32" \
  9.                     "'0 & 0x0F000000 = 0x05000000 && 22 & 0xFFFF@16 = %s' -j DROP"
  10. malicious_ips = DATA.lines.select { |line| line =~ /^(\d+\.){3}\d+$/ }
  11. u32_values = malicious_ips.map { |ip| "0x%x" % IPAddr.new(ip).to_i }
  12. command = u32_values.each_slice(10).map { |u32s| iptables_template % (u32s * ',') }
  13.  
  14. puts command
  15.  
  16. __END__
  17. # Malicious IPs can be obtained from www.jiankongmf.com
  18. 159.106.121.75
  19. 203.98.7.65
  20. 243.185.187.39
  21. 37.61.54.158
  22. 46.82.174.68
  23. 59.24.3.173
  24. 78.16.49.15
  25. 8.7.198.45
  26. 93.46.8.89
  27. 202.106.1.2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement