|
1 | 1 | package log4shell |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "encoding/binary" |
4 | 6 | "os" |
5 | 7 | "testing" |
6 | 8 |
|
@@ -93,3 +95,91 @@ func TestGenerateReverseTCP(t *testing.T) { |
93 | 95 | require.Zero(t, class) |
94 | 96 | }) |
95 | 97 | } |
| 98 | + |
| 99 | +func TestGenerateReverseTCP_Fake(t *testing.T) { |
| 100 | + const ( |
| 101 | + fileNameFlag = "ReverseTCP.java" |
| 102 | + hostFlag = "${host}" |
| 103 | + portFlag = "${port}" |
| 104 | + tokenFlag = "${token}" |
| 105 | + className = "ReverseTCP\x0C" |
| 106 | + ) |
| 107 | + |
| 108 | + buf := bytes.NewBuffer(make([]byte, 0, 128)) |
| 109 | + buf.Write([]byte{0xCA, 0xFE}) |
| 110 | + buf.Write([]byte{0x00, 0x00}) |
| 111 | + |
| 112 | + size := make([]byte, 2) |
| 113 | + |
| 114 | + binary.BigEndian.PutUint16(size, uint16(len(fileNameFlag))) |
| 115 | + buf.Write(size) |
| 116 | + buf.WriteString(fileNameFlag) |
| 117 | + buf.Write([]byte{0x00, 0x00}) |
| 118 | + |
| 119 | + binary.BigEndian.PutUint16(size, uint16(len(hostFlag))) |
| 120 | + buf.Write(size) |
| 121 | + buf.WriteString(hostFlag) |
| 122 | + buf.Write([]byte{0x00, 0x00}) |
| 123 | + |
| 124 | + binary.BigEndian.PutUint16(size, uint16(len(portFlag))) |
| 125 | + buf.Write(size) |
| 126 | + buf.WriteString(portFlag) |
| 127 | + buf.Write([]byte{0x00, 0x00}) |
| 128 | + |
| 129 | + binary.BigEndian.PutUint16(size, uint16(len(tokenFlag))) |
| 130 | + buf.Write(size) |
| 131 | + buf.WriteString(tokenFlag) |
| 132 | + buf.Write([]byte{0x00, 0x00}) |
| 133 | + |
| 134 | + binary.BigEndian.PutUint16(size, uint16(len(className))) |
| 135 | + buf.Write(size) |
| 136 | + buf.WriteString(className) |
| 137 | + buf.Write([]byte{0x00, 0x00}) |
| 138 | + |
| 139 | + err := os.WriteFile("testdata/template/ReverseTCP.class", buf.Bytes(), 0600) |
| 140 | + require.NoError(t, err) |
| 141 | +} |
| 142 | + |
| 143 | +func TestGenerateReverseTCP_Fake_Compare(t *testing.T) { |
| 144 | + const ( |
| 145 | + fileNameFlag = "ReTCP.java" |
| 146 | + hostFlag = "127.0.0.1" |
| 147 | + portFlag = "9979" |
| 148 | + tokenFlag = "test" |
| 149 | + className = "ReTCP\x0C" |
| 150 | + ) |
| 151 | + |
| 152 | + buf := bytes.NewBuffer(make([]byte, 0, 128)) |
| 153 | + buf.Write([]byte{0xCA, 0xFE}) |
| 154 | + buf.Write([]byte{0x00, 0x00}) |
| 155 | + |
| 156 | + size := make([]byte, 2) |
| 157 | + |
| 158 | + binary.BigEndian.PutUint16(size, uint16(len(fileNameFlag))) |
| 159 | + buf.Write(size) |
| 160 | + buf.WriteString(fileNameFlag) |
| 161 | + buf.Write([]byte{0x00, 0x00}) |
| 162 | + |
| 163 | + binary.BigEndian.PutUint16(size, uint16(len(hostFlag))) |
| 164 | + buf.Write(size) |
| 165 | + buf.WriteString(hostFlag) |
| 166 | + buf.Write([]byte{0x00, 0x00}) |
| 167 | + |
| 168 | + binary.BigEndian.PutUint16(size, uint16(len(portFlag))) |
| 169 | + buf.Write(size) |
| 170 | + buf.WriteString(portFlag) |
| 171 | + buf.Write([]byte{0x00, 0x00}) |
| 172 | + |
| 173 | + binary.BigEndian.PutUint16(size, uint16(len(tokenFlag))) |
| 174 | + buf.Write(size) |
| 175 | + buf.WriteString(tokenFlag) |
| 176 | + buf.Write([]byte{0x00, 0x00}) |
| 177 | + |
| 178 | + binary.BigEndian.PutUint16(size, uint16(len(className)-1)) |
| 179 | + buf.Write(size) |
| 180 | + buf.WriteString(className) |
| 181 | + buf.Write([]byte{0x00, 0x00}) |
| 182 | + |
| 183 | + err := os.WriteFile("testdata/template/compare/ReTCP.class", buf.Bytes(), 0600) |
| 184 | + require.NoError(t, err) |
| 185 | +} |
0 commit comments