hidefile.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include "hidefile.h"
  4. #include "beacon.h"
  5. BOOL CreateHiddenDir(WCHAR *directory) {
  6. DWORD attrib;
  7. attrib = KERNEL32$GetFileAttributesW(directory);
  8. if(attrib == INVALID_FILE_ATTRIBUTES) {
  9. BeaconPrintf(CALLBACK_ERROR, "Failed to get file attribute information from directory with error code: %ld. Is the path and directory name correct?\n", KERNEL32$GetLastError());
  10. return FALSE;
  11. }
  12. attrib |= FILE_ATTRIBUTE_HIDDEN;
  13. attrib |= FILE_ATTRIBUTE_SYSTEM;
  14. if(KERNEL32$SetFileAttributesW(directory, attrib) == 0) {
  15. BeaconPrintf(CALLBACK_ERROR, "Failed to set new attribute information on the directory with error code: %ld\n", KERNEL32$GetLastError());
  16. return FALSE;
  17. }
  18. return TRUE;
  19. }
  20. BOOL CreateHiddenFile(WCHAR *file) {
  21. HANDLE hFile;
  22. FILE_BASIC_INFORMATION fileInfo;
  23. IO_STATUS_BLOCK ioStatusBlock;
  24. NtQueryInformationFile_t pNtQueryInformationFile = (NtQueryInformationFile_t)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryInformationFile");
  25. if(pNtQueryInformationFile == NULL) return 0;
  26. NtSetInformationFile_t pNtSetInformationFile = (NtSetInformationFile_t)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtSetInformationFile");
  27. if(pNtSetInformationFile == NULL) return 0;
  28. hFile = KERNEL32$CreateFileW(file, GENERIC_READ | GENERIC_WRITE | FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING, 0, NULL);
  29. if (hFile == INVALID_HANDLE_VALUE) {
  30. BeaconPrintf(CALLBACK_ERROR, "Could not open file with error code: %ld\n", KERNEL32$GetLastError());
  31. return FALSE;
  32. }
  33. if (pNtQueryInformationFile(hFile, &ioStatusBlock, &fileInfo, sizeof(FILE_BASIC_INFORMATION), FileBasicInformation) < 0) {
  34. BeaconPrintf(CALLBACK_ERROR, "Failed to get file attribute information with error code: %ld\n", KERNEL32$GetLastError());
  35. KERNEL32$CloseHandle(hFile);
  36. return FALSE;
  37. }
  38. fileInfo.FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
  39. fileInfo.FileAttributes |= FILE_ATTRIBUTE_SYSTEM;
  40. if (pNtSetInformationFile(hFile, &ioStatusBlock, &fileInfo, sizeof(FILE_BASIC_INFORMATION), FileBasicInformation) < 0) {
  41. BeaconPrintf(CALLBACK_ERROR, "Failed to set new attribute information on the file with error code: %ld\n", KERNEL32$GetLastError());
  42. KERNEL32$CloseHandle(hFile);
  43. return FALSE;
  44. }
  45. KERNEL32$CloseHandle(hFile);
  46. return TRUE;
  47. }
  48. int go(char *args, int len) {
  49. CHAR *option;
  50. WCHAR *path;
  51. BOOL res = FALSE;
  52. datap parser;
  53. BeaconDataParse(&parser, args, len);
  54. option = BeaconDataExtract(&parser, NULL);
  55. path = BeaconDataExtract(&parser, NULL);
  56. if (MSVCRT$strcmp(option, "dir") == 0) {
  57. res = CreateHiddenDir(path);
  58. if (res) BeaconPrintf(CALLBACK_OUTPUT, "[+] Successfully modified directory attributes to systemfile + hidden.\n");
  59. }
  60. else if (MSVCRT$strcmp(option, "file") == 0) {
  61. res = CreateHiddenFile(path);
  62. if (res) BeaconPrintf(CALLBACK_OUTPUT, "[+] Successfully modified file attributes to systemfile + hidden.\n");
  63. }
  64. else BeaconPrintf(CALLBACK_ERROR, "Please specify one of the following options: dir | file\n");
  65. return 0;
  66. }