Giter Club home page Giter Club logo

dos's Introduction

Goで始める毎分100万回DoS攻撃


Goってなに?

  • Googleがつくったすごい言語
  • Python並に読みやすい、C並に速い center

なにがつくれる?

  • Webアプリ
    • YouTubeはGoで書かれている
  • ソフトウェア
    • Windows, Linux, Macを同じコードで書ける
    • Android, iOSを同じコードで書ける
  • PaaS
    • Docker, KubernetesはGoで書かれている

なんでGo?

並列処理が簡単に書ける!!!!!

並列化をやってみたい

  • 目標1:6秒の直列処理を3秒でやる
  • 目標2:==DoS攻撃==をする center

6秒の直列処理を3秒でやる

center


6秒の直列処理

  • 6s.go

     func main() {
     	log.Print("-- start")
    
     	log.Print("1s")
     	time.Sleep(1 * time.Second)
     	log.Print("done")
    
     	log.Print("2s")
     	time.Sleep(2 * time.Second)
     	log.Print("done")
    
     	log.Print("3s")
     	time.Sleep(3 * time.Second)
     	log.Print("done")
    
     	log.Print("-- finish")
     }

3秒の並列処理

  • 3s.go
func main() {
	log.Print("-- start")

	finished := make(chan bool)

	tasks := []func(){
		func() {
			log.Print("1s")
			time.Sleep(1 * time.Second)
			log.Print("done")
			finished <- true
		},
		func() {
			log.Print("2s")
			time.Sleep(2 * time.Second)
			log.Print("done")
			finished <- true
		},
		func() {
			log.Print("3s")
			time.Sleep(3 * time.Second)
			log.Print("done")
			finished <- true
		},
	}

	for _, task := range tasks {
		go task()
	}

	for i := 0; i < len(tasks); i++ {
		<-finished
	}

	log.Print("-- finish")
}
  • 3skai.go
func main() {
	log.Print("-- start")

	tasks := []func(){
		sleep(1),
		sleep(2),
		sleep(3),
	}

	var wg sync.WaitGroup

	for _, task := range tasks {
		wg.Add(1)
		go func(f func()) {
			defer wg.Done()
			f()
		}(task)
	}
	wg.Wait()

	log.Print("-- finish")
}

func sleep(sec int) func() {
	return func() {
		log.Printf("%ds", sec)
		time.Sleep(time.Duration(sec) * time.Second)
		log.Printf("%d done", sec)
	}
}

攻撃されるサーバをローカルに立てる

  • server.go

     package main
    
     import (
         "fmt"
         "net/http"
     )
    
     func handler(w http.ResponseWriter, r *http.Request) {
         fmt.Fprintf(w, "Hello, World")
     }
    
     func main() {
     http.HandleFunc("/", handler)
         http.ListenAndServe(":8080", nil)
     }

    たったこれだけ!!!


DoS攻撃する

  • dos.go

     package main
    
     import (
     	"log"
     	"net/http"
     	"sync"
     	"time"
     )
    
     func main() {
     	url := "http://localhost:8080"
    
     	maxConnection := make(chan bool, 100)
     	var wg sync.WaitGroup
    
     	count := 0
     	start := time.Now()
     	for maxRequest := 0; maxRequest < 10000; maxRequest++ {
     		wg.Add(1)
     		maxConnection <- true
     		go func() {
     			defer wg.Done()
    
     			resp, err := http.Get(url)
     			if err != nil {
     				return
     			}
     			defer resp.Body.Close()
    
     			count++
     			<- maxConnection
     		}()
     	}
     	wg.Wait()
     	end := time.Now()
     	log.Printf("%d 回のリクエストに成功しました!\n", count)
     	log.Printf("%f 秒処理に時間がかかりました!\n", (end.Sub(start)).Seconds())
     }

dos's People

Contributors

miraichino avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.