#include <Windows.h> #include <stdio.h> #pragma warning(disable:4996) void PasswordCheck() { char message_console[MAX_PATH]; char key_accepted[MAX_PATH]; char bad_key[MAX_PATH]; char password_store[MAX_PATH]; DWORD chars_read; strcpy(message_console, "Enter password for this malware: "); WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), message_console, strlen(message_console), NULL, NULL); ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE), password_store, MAX_PATH, &chars_read, NULL); if (!strncmp(password_store, "password ", chars_read)) { strcpy(key_accepted, "Key Accepted"); WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), key_accepted, strlen(key_accepted), NULL, NULL); } else { strcpy(bad_key, "Bad key"); WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), bad_key, strlen(bad_key), NULL, NULL); } } int main(int argc, char* argv[]) { PasswordCheck(); return 0; }
注意: 使用WriteConsole时, 参数nNumberOfCharsToWrite 要使用strlen获得要写入的字符数,不要使用sizeof,因为它会获得数组的大小。
需要使用strncmp来比较两个字符串是否相等,因为ReadConsole读取时会在字符串末尾处自动添加 。所以我们比较时要根据字符的多少来判断。