github.com/iDigitalFlame/xmt@v0.5.4/c2/wrapper/simple.go (about)

     1  // Copyright (C) 2020 - 2023 iDigitalFlame
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  //
    16  
    17  // Package wrapper contains built-in implementations of the 'c2.Wrapper'
    18  // interface, which can be used to wrap or encode data that is passed between
    19  // Sessions and C2 Servers.
    20  package wrapper
    21  
    22  import (
    23  	"encoding/base64"
    24  	"encoding/hex"
    25  	"io"
    26  
    27  	"github.com/iDigitalFlame/xmt/data"
    28  )
    29  
    30  const (
    31  	// Hex is the Hex encoding Wrapper. This wraps the binary data as hex values.
    32  	Hex = simple(0x1)
    33  	// Base64 is the Base64 Wrapper. This wraps the binary data as a Base64 byte string. This may be
    34  	// combined with the Base64 transform.
    35  	Base64 = simple(0x2)
    36  )
    37  
    38  type simple uint8
    39  
    40  func (s simple) Unwrap(r io.Reader) (io.Reader, error) {
    41  	switch s {
    42  	case Hex:
    43  		return hex.NewDecoder(r), nil
    44  	case Base64:
    45  		return base64.NewDecoder(base64.StdEncoding, r), nil
    46  	}
    47  	return r, nil
    48  }
    49  func (s simple) Wrap(w io.WriteCloser) (io.WriteCloser, error) {
    50  	switch s {
    51  	case Hex:
    52  		return data.WriteCloser(hex.NewEncoder(w)), nil
    53  	case Base64:
    54  		return base64.NewEncoder(base64.StdEncoding, w), nil
    55  	}
    56  	return w, nil
    57  }