-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvConfig.cpp
More file actions
147 lines (118 loc) · 4.28 KB
/
envConfig.cpp
File metadata and controls
147 lines (118 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h> // Inclui funções da API do Windows
#include <sstream> // Para ostringstream
#include <ctime> // Para gerar o GUID único
#include <cstdlib> // Para a função rand()
// Função personalizada para converter números em string
template <typename T>
std::string to_string(const T& value) {
std::ostringstream oss;
oss << value;
return oss.str();
}
// Função para gerar um GUID único usando data/hora e valor aleatório
std::string generateGUID() {
srand((unsigned)time(0));
std::string guid = to_string(time(0)) + to_string(rand());
return guid;
}
// Função para comparação segura de strings, prevenindo ataques de timing
bool secure_compare(const std::string& a, const std::string& b) {
if (a.size() != b.size()) return false;
volatile int result = 0;
for (size_t i = 0; i < a.size(); ++i) {
result |= a[i] ^ b[i];
}
return result == 0;
}
// Função para zerar a memória de forma segura
void secure_zero(void* ptr, size_t len) {
volatile unsigned char* p = (volatile unsigned char*)ptr;
while (len--) *p++ = 0;
}
// Função para verificar se um debugger está presente e encerrar a aplicação se estiver
void check_debugger() {
if (IsDebuggerPresent()) {
std::cerr << "Debugger detected! Exiting..." << std::endl;
exit(1);
}
}
// Cabeçalho de identificação
void header() {
std::cout << "Bomdev Runtime Secret Management" << std::endl;
std::cout << "2024 Bomdev, Developed by Guilherme Ferreira" << std::endl << std::endl;
}
int main() {
SetConsoleTitle("Bomdev Secret Management Console");
// Verificação de debugger
check_debugger();
// Gerar um GUID único
std::string guidStr = generateGUID();
// Cabeçalho
header();
// Nome do pipe usando o GUID
bool isProduction = false;
std::string pipeName = "\\\\.\\pipe\\" + (isProduction ? guidStr : "DEFAULT") + "_BOMDEV_SECRET";
// Definir a senha (evitar hardcoding, ideal é usar um sistema de gerenciamento de segredos)
const std::string password = "cUDo56INbp9PH39lODvfP9bm0drs41nhW0dSKklpNqHQEvhxOp";
std::string pass = "";
// Loop para solicitar a senha do usuário
do {
if (pass != "")
std::cout << "\033[33m" << "Invalid password." << "\033[0m" << std::endl;
std::cout << "Enter the password: ";
std::getline(std::cin, pass);
// Limpar o console após cada tentativa de senha
system("cls"); // No Windows, use "cls" para limpar a tela
// Mostrar cabeçalho novamente
header();
} while (pass == "" || !secure_compare(pass, password));
// Zerar a senha da memória após validação
secure_zero(&pass[0], pass.size());
std::cout << "Pipe name: " << pipeName << std::endl;
// Solicitar o JSON de configuração
std::string jsonInput;
std::cout << "\033[35m" << "Enter the project config content: " << "\033[0m"; // \033[0m reseta a cor para padrão
std::getline(std::cin, jsonInput);
// Criar o named pipe
HANDLE hPipe = CreateNamedPipe(
pipeName.c_str(),
PIPE_ACCESS_OUTBOUND,
PIPE_TYPE_BYTE | PIPE_WAIT,
1,
0,
0,
0,
NULL
);
if (hPipe == INVALID_HANDLE_VALUE) {
std::cerr << "Error to create pipe: " << GetLastError() << std::endl;
return 1;
}
// Conectar ao pipe
if (!ConnectNamedPipe(hPipe, NULL)) {
std::cerr << "Error to connect pipe: " << GetLastError() << std::endl;
CloseHandle(hPipe);
return 1;
}
// Escrever o JSON no pipe
DWORD bytesWritten = 0;
if (!WriteFile(hPipe, jsonInput.c_str(), jsonInput.size(), &bytesWritten, NULL)) {
std::cerr << "Error to write to pipe: " << GetLastError() << std::endl;
CloseHandle(hPipe);
return 1;
}
// Zerar a memória que contém o JSON
secure_zero(&jsonInput[0], jsonInput.size());
// Fechar o pipe
CloseHandle(hPipe);
// Limpar o console
system("cls");
// Mostrar o cabeçalho novamente
header();
std::cout << "\033[32m" << "The operation was successfully." << "\033[0m" << std::endl << std::endl; // \033[0m reseta a cor para padrão
// Encerrar a aplicação
exit(0);
}