github.com/iDigitalFlame/xmt@v0.5.4/examples/example_groups.go (about)

     1  //go:build !implant
     2  // +build !implant
     3  
     4  // Copyright (C) 2020 - 2023 iDigitalFlame
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    18  //
    19  
    20  package main
    21  
    22  import (
    23  	"os"
    24  	"strconv"
    25  	"time"
    26  
    27  	"github.com/PurpleSec/logx"
    28  	"github.com/iDigitalFlame/xmt/c2"
    29  	"github.com/iDigitalFlame/xmt/c2/cfg"
    30  	// Uncomment to enable profiling.
    31  	// "net/http"
    32  	// _ "net/http/pprof"
    33  )
    34  
    35  func exampleGroups() {
    36  	// This example shows off the multi-Profile group!
    37  	//
    38  	// The client process will reconnect to any listener that first
    39  	// accepts it in a 'RoundRobin' fashion.
    40  	//
    41  	// Try it by running both server and client then using iptables
    42  	// to filter out the listener ports (8085, 8086, 8087) randomally.
    43  
    44  	// Create initial config
    45  	c := cfg.Pack(
    46  		cfg.Host("127.0.0.1:8085"),
    47  		cfg.ConnectTCP,
    48  		cfg.Sleep(time.Second*5),
    49  		cfg.Jitter(0),
    50  	)
    51  
    52  	// Add a Group!
    53  	c.AddGroup(
    54  		cfg.Host("127.0.0.1:8086"),
    55  		cfg.ConnectTCP,
    56  		cfg.Sleep(time.Second*10),
    57  		cfg.Jitter(50),
    58  	)
    59  
    60  	// Add a Group!
    61  	c.AddGroup(
    62  		cfg.Host("127.0.0.1:8087"),
    63  		cfg.ConnectTCP,
    64  		cfg.Sleep(time.Second*5),
    65  		cfg.Jitter(0),
    66  	)
    67  
    68  	// Use the last valid group, unless an error happens.
    69  	c.Add(cfg.SelectorLastValid)
    70  
    71  	if len(os.Args) == 1 {
    72  		server(c)
    73  		return
    74  	}
    75  	client(c)
    76  }
    77  func server(c cfg.Config) {
    78  	// Uncomment to enable profiling.
    79  	// go http.ListenAndServe("localhost:9090", nil)
    80  
    81  	s := c2.NewServer(logx.Console(logx.Trace))
    82  
    83  	// Loop over the config Groups.
    84  	for i := 0; i < c.Groups(); i++ {
    85  		// Get group at section and Build it.
    86  		// We're ignoring the error since we KNOW what the result is.
    87  		var (
    88  			v    = c.Group(i)
    89  			p, _ = v.Build()
    90  		)
    91  		// Create a Listener with the name "tcp<offset>".
    92  		if _, err := s.Listen("", "tcp"+strconv.Itoa(i+1), p); err != nil {
    93  			panic(err)
    94  		}
    95  	}
    96  	// Block
    97  	s.Wait()
    98  }
    99  func client(c cfg.Config) {
   100  	// Uncomment to enable profiling.
   101  	// go http.ListenAndServe("localhost:9091", nil)
   102  
   103  	var (
   104  		p, _   = c.Build()
   105  		s, err = c2.Connect(logx.Console(logx.Trace), p)
   106  	)
   107  	if err != nil {
   108  		panic(err)
   109  	}
   110  
   111  	// Block
   112  	s.Wait()
   113  }