github.com/iDigitalFlame/xmt@v0.5.1/c2/task/v_assembly.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 task
    21  
    22  import (
    23  	"io"
    24  
    25  	"github.com/iDigitalFlame/xmt/com"
    26  	"github.com/iDigitalFlame/xmt/data"
    27  )
    28  
    29  // AssemblyFile will create a Tasklet that will instruct the client to run
    30  // shellcode from a file source on the local (server - the one calling this
    31  // function) machine.
    32  //
    33  // This will attempt to read the file and will return an error if it fails.
    34  //
    35  // C2 Details:
    36  //
    37  //	ID: TvAssembly
    38  //
    39  //	Input:
    40  //	    Assembly struct {
    41  //	        bool            // Wait
    42  //	        int64           // Timeout
    43  //	        bool            // Filter Status
    44  //	        Filter struct { // Filter
    45  //	            uint32      // PID
    46  //	            bool        // Fallback
    47  //	            uint8       // Session
    48  //	            uint8       // Elevated
    49  //	            []string    // Exclude
    50  //	            []string    // Include
    51  //	        }
    52  //	        []byte          // Assembly Data
    53  //	    }
    54  //	Output:
    55  //	    uint64              // Handle
    56  //	    uint32              // PID
    57  //	    int32               // Exit Code
    58  func AssemblyFile(s string) (Assembly, error) {
    59  	b, err := data.ReadFile(s)
    60  	if err != nil {
    61  		return Assembly{}, err
    62  	}
    63  	return Assembly{Data: b}, nil
    64  }
    65  
    66  // Packet will take the configured Assembly options and will return a Packet
    67  // and any errors that may occur during building.
    68  //
    69  // This allows the Assembly struct to fulfil the 'Tasklet' interface.
    70  //
    71  // C2 Details:
    72  //
    73  //	ID: TvAssembly
    74  //
    75  //	Input:
    76  //	    Assembly struct {
    77  //	        bool            // Wait
    78  //	        int64           // Timeout
    79  //	        bool            // Filter Status
    80  //	        Filter struct { // Filter
    81  //	            uint32      // PID
    82  //	            bool        // Fallback
    83  //	            uint8       // Session
    84  //	            uint8       // Elevated
    85  //	            []string    // Exclude
    86  //	            []string    // Include
    87  //	        }
    88  //	        []byte          // Assembly Data
    89  //	    }
    90  //	Output:
    91  //	    uint64              // Handle
    92  //	    uint32              // PID
    93  //	    int32               // Exit Code
    94  func (a Assembly) Packet() (*com.Packet, error) {
    95  	n := &com.Packet{ID: TvAssembly}
    96  	a.MarshalStream(n)
    97  	return n, nil
    98  }
    99  
   100  // AssemblyReader will create a Tasklet that will instruct the client to run
   101  // shellcode from the contents of the supplied Reader.
   102  //
   103  // This will attempt to read from the Reader and will return an error if it
   104  // fails.
   105  //
   106  // C2 Details:
   107  //
   108  //	ID: TvAssembly
   109  //
   110  //	Input:
   111  //	    Assembly struct {
   112  //	        bool            // Wait
   113  //	        int64           // Timeout
   114  //	        bool            // Filter Status
   115  //	        Filter struct { // Filter
   116  //	            uint32      // PID
   117  //	            bool        // Fallback
   118  //	            uint8       // Session
   119  //	            uint8       // Elevated
   120  //	            []string    // Exclude
   121  //	            []string    // Include
   122  //	        }
   123  //	        []byte          // Assembly Data
   124  //	    }
   125  //	Output:
   126  //	    uint64              // Handle
   127  //	    uint32              // PID
   128  //	    int32               // Exit Code
   129  func AssemblyReader(r io.Reader) (Assembly, error) {
   130  	b, err := data.ReadAll(r)
   131  	if err != nil {
   132  		return Assembly{}, err
   133  	}
   134  	return Assembly{Data: b}, nil
   135  }
   136  
   137  // MarshalStream writes the data for this Code thread to the supplied Writer.
   138  func (a Assembly) MarshalStream(w data.Writer) error {
   139  	if err := w.WriteBool(a.Wait); err != nil {
   140  		return err
   141  	}
   142  	if err := w.WriteInt64(int64(a.Timeout)); err != nil {
   143  		return err
   144  	}
   145  	if err := a.Filter.MarshalStream(w); err != nil {
   146  		return err
   147  	}
   148  	return w.WriteBytes(a.Data)
   149  }