Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. /*
  2. Demonstrates how using SetFileSecurity does not result in a [file] ACL with ACEs inherited from parent [folder], while using SetNamedSecurityInfo does, as is proper.
  3.  
  4. Disable (comment) the `SetNamedSecurityInfo` call along with its parent `if` statement and enable (uncomment) the following `SetFileSecurity` call (and its parent `if` statement, obviously) to switch the behavior and observe different resultant ACL on the file.
  5.  
  6. The Windows application entry point in this snippet expects two command line arguments -- the file path of the file you want to set security information on, and the actual security (specified in SDDL format) information desired for the file.
  7. */
  8.  
  9. #include <windows.h>
  10. #include <shellapi.h>
  11. #include <sddl.h>
  12. #include <aclapi.h>
  13.  
  14. int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
  15. {
  16. int argc;
  17.  
  18. LPWSTR * argv = CommandLineToArgvW(lpCmdLine, &argc);
  19.  
  20. if(argv == NULL) {
  21. return -1;
  22. }
  23.  
  24. if(argc < 2) {
  25. MessageBox(NULL, L"Invalid command line.", NULL, MB_ICONERROR);
  26. return -2;
  27. }
  28.  
  29. PSECURITY_DESCRIPTOR p_sd;
  30.  
  31. if(ConvertStringSecurityDescriptorToSecurityDescriptor(argv[1], SDDL_REVISION_1, &p_sd, NULL) == 0) {
  32. return -3;
  33. }
  34.  
  35. PACL p_dacl;
  36. BOOL p_dacl_present, p_dacl_defaulted;
  37.  
  38. if(GetSecurityDescriptorDacl(p_sd, &p_dacl_present, &p_dacl, &p_dacl_defaulted) == 0) {
  39. return -5;
  40. }
  41.  
  42. if(SetNamedSecurityInfo(argv[0], SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, p_dacl, NULL) != 0) {
  43. return -6;
  44. }
  45.  
  46. /*if(SetFileSecurity(argv[0], DACL_SECURITY_INFORMATION, p_sd) == 0) {
  47. return -4;
  48. }*/
  49.  
  50. LocalFree(p_sd);
  51. LocalFree(argv);
  52.  
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement