YauhenMardan

jjj

Nov 20th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "TemplateDLL.h"
  3. #include <iomanip>
  4. #include <filesystem>
  5.  
  6. #include <vector>
  7. #include "User.h"
  8.  
  9. std::vector<User> readFile(std::wstring fileName); //read file
  10. std::wifstream & operator >> (std::wifstream & wifs, User & user); //read each user
  11. void tryToOpenFile(std::wstring& logText, std::wstring filePath);
  12. void tryToCreateFile(std::wstring& logText, std::wstring filePath);
  13. void tryToReadFile(std::wstring& logText, std::wstring filePath);
  14. void tryToReadAllFiles(std::wstring& logText, std::wstring filePath);
  15.  
  16. namespace fs = std::experimental::filesystem;
  17.  
  18. extern "C"
  19. {
  20. LPCWSTR GetName()
  21. {
  22. return m_sName;
  23. }
  24.  
  25. LPCWSTR GetDescription()
  26. {
  27. return m_sDescription;
  28. }
  29.  
  30. bool RunTest(std::function<void(LPCWSTR)> pFnLog)
  31. {
  32. std::wstring logText = L"log text:\n"; //test output
  33. // test code
  34. std::wstring confFileName = L"conf.ini"; //configuration file
  35. std::vector<User> data; //data: users
  36. data = readFile(confFileName); //read conf file
  37. //impersonation
  38. HANDLE hToken; //receives a handle to a token that represents the specified user
  39. for (int i = 0; i < data.size(); i++) {
  40. std::wstring name = data[i].getUserName(); //temp variables
  41. std::wstring pass = data[i].getPassword();
  42. LogonUserW( //log on user
  43. (LPCWSTR)name.c_str(),
  44. L".", //using only the local account database
  45. (LPCWSTR)pass.c_str(),
  46. LOGON32_LOGON_INTERACTIVE,
  47. LOGON32_PROVIDER_DEFAULT,
  48. &hToken
  49. );
  50. SecureZeroMemory(&pass, sizeof(pass));
  51. logText += L"**********\n";
  52. logText += name + L"\n"; //print name of user
  53. ImpersonateLoggedOnUser(hToken); //impersonate user
  54. //try to read all files
  55. std::wstring folderFullPath = data[i].getFullPath();
  56. tryToReadAllFiles(logText, folderFullPath);
  57. ////try to read file FileToRead.txt
  58. //std::wstring fileToReadFullName = folderFullPath + L"\\FileToRead.txt";
  59. //tryToOpenFile(logText, data, fileToReadFullName);
  60. //try to create a file FileToCreate.txt
  61. std::wstring fileToCreateFullName = folderFullPath + L"\\FileToCreate.txt";
  62. tryToCreateFile(logText, fileToCreateFullName);
  63. //try to read FileToCreate.txt
  64. tryToReadFile(logText, fileToCreateFullName);
  65. RevertToSelf(); //session revertion
  66. }
  67. pFnLog(logText.c_str());
  68. return true;
  69. }
  70. }
  71.  
  72. std::vector<User> readFile(std::wstring fileName) {
  73. std::vector<User> data; //vectror of users
  74. std::wifstream fin(fileName);
  75. while (!fin.eof())
  76. {
  77. User tmp_user;
  78. fin >> tmp_user;
  79. data.push_back(tmp_user);
  80. }
  81. return data;
  82. }
  83. std::wifstream & operator >> (std::wifstream & wifs, User & user) {
  84. wifs >> user.userName >> user.domainName >> user.password >> user.fullPath;
  85. return wifs;
  86. }
  87. void tryToOpenFile(std::wstring& logText, std::wstring filePath) {
  88. std::wifstream fin;
  89. fin.open(filePath.c_str());
  90. if (fin.is_open()) {
  91. logText += L"File is opened " + filePath + L"\n";
  92. }
  93. else {
  94. logText += L"Cannot open the file " + filePath + L"\n";
  95. }
  96. fin.close();
  97. }
  98. void tryToCreateFile(std::wstring& logText, std::wstring filePath) {
  99. std::wofstream fout;
  100. fout.open(filePath);
  101. logText += L"Trying to create a file:\n";
  102. if (fout.is_open()) {
  103. logText += L"File is created " + filePath + L"\n";
  104. //try to write to file FileToCreate.txt
  105. std::wstring testText = L"Try to write text";
  106. fout << testText;
  107. logText += L"Trying to write: " + testText + L"\n";
  108. }
  109. else {
  110. logText += L"Cannot create the file " + filePath + L"\n";
  111. }
  112. fout.close();
  113. }
  114. void tryToReadFile(std::wstring& logText, std::wstring filePath) {
  115. std::wifstream fin;
  116. fin.open(filePath);
  117. logText += L"Trying to read file contents:\n";
  118. if (fin.is_open()) {
  119. logText += L"Reading the file " + filePath + L"\n" + L"File contains:\n";
  120. while (!fin.eof())
  121. {
  122. std::wstring temp;
  123. fin >> temp;
  124. logText+=temp +L" ";
  125. }
  126. logText += L"\n";
  127. }
  128. else {
  129. logText+=L"Cannot open " + filePath + L"\n";
  130. }
  131. fin.close();
  132. }
  133. void tryToReadAllFiles(std::wstring& logText, std::wstring filePath) {
  134. logText += L"Folder contains:\n";
  135. for (auto & p : fs::recursive_directory_iterator(filePath)) {
  136. logText += p.path().c_str();
  137. logText += L"\n";
  138. }
  139. logText += L"Trying to open files:\n";
  140. for (auto & p : fs::recursive_directory_iterator(filePath)) {
  141. if (!fs::is_directory(p)) {
  142. tryToOpenFile(logText, p.path().c_str());
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment