{"id":91,"date":"2009-03-06T07:13:30","date_gmt":"2009-03-06T13:13:30","guid":{"rendered":"http:\/\/www.jedge.com\/wordpress\/?p=91"},"modified":"2018-12-29T00:46:17","modified_gmt":"2018-12-29T06:46:17","slug":"auditing-folder-and-subfolder-permissions-using-cacls","status":"publish","type":"post","link":"https:\/\/www.jedge.com\/wordpress\/2009\/03\/auditing-folder-and-subfolder-permissions-using-cacls\/","title":{"rendered":"Auditing Folder (and subfolder) Permissions using CACLS"},"content":{"rendered":"<p>Updated 7\/2\/2016 &#8211; yeah, I haven&#8217;t needed to parse the shitty output from this tool in 7 years.  I never accounted for &#8220;special access&#8221; permissions not including the account with the access.<br \/>\nUpdated 12\/28\/2018 &#8211; link to the code didn&#8217;t work so I&#8217;ve fixed that.  Also updated if you receive a specific error message.  <\/p>\n<p>CACLS.exe is a great builtin Windows utility that allows you to list the permissions on a file or folder.\u00a0 This command has been used in an audit to get the permissions of the folders on an agency file server that served the &#8220;private&#8221; shares to each Domain user.\u00a0 The findings we would be looking for when examining the results are improper access to the &#8220;private&#8221; shares by other Domain users.<\/p>\n<p>For CACLS options and how to interpret the results see this <a title=\"CACLS options\" href=\"http:\/\/situsnya.wordpress.com\/2008\/08\/31\/caclsexe-display-or-modify-access-control-lists-acls-for-files-and-folders\/\" target=\"_blank\">site<\/a>.<br \/>\n<!--more--><br \/>\nThe commands that I run are as follows:<br \/>\nDirectories and Files in the folder your run CACLS<br \/>\n<kbd>c:\\&gt;for \/f \"delims=\" %a in ('dir \/b') do @cacls \"%a\" &gt;&gt; savefile.txt<\/kbd><br \/>\nAll directories, recursive, from the folder your run CACLS<br \/>\n<kbd>c:\\&gt;for \/f \"delims=\" %a in ('dir \/b \/S \/A:D') do @cacls \"%a\" &gt;&gt; savefile.txt<\/kbd><\/p>\n<p>Once results are obtained they need to be parsed so they can be analyzed.\u00a0 I have written a perl script to add the correct folder name to each permission.\u00a0 This is so they can be sorted by permission in your spreadsheet application of choice.<\/p>\n<p>&nbsp;<\/p>\n<p>Save the code and run it as follows (also download <a href=\"http:\/\/www.jedge.com\/code\/calcsparse.pl\">here<\/a>):<\/p>\n<p><pre><code>\n#!\/usr\/bin\/perl\n\n$numArgs = $#ARGV +1;\nif($numArgs &lt; 2){\n&nbsp;&nbsp;print &quot;Invalid Number of Arguments\\n&quot;;\n&nbsp;&nbsp;print &quot;caclsparse.pl &lt;filename&gt; &lt;foldername&gt;\\n&quot;;\n&nbsp;&nbsp;print &quot;The foldername is the root folder you ran CACLS.exe from.\\n\\n&quot;;\n&nbsp;&nbsp;print &quot;foldername example:&nbsp;&nbsp;\\&quot;C\\\\:\\\\\\\\Documents and Settings\\\\\\\\jedge\\&quot;\\n&quot;;\n&nbsp;&nbsp;print &quot;Folder names with spaces need to be encapsulated in quotes.\\n&quot;;\n&nbsp;&nbsp;print &quot;You need to escape the backslash twice.\\n&quot;;\n&nbsp;&nbsp;print &quot;You need to escape the colon with a backslash as well.\\n&quot;;\n&nbsp;&nbsp;exit;\n}\n\n#open the file\n$infile = &quot;$ARGV[0]&quot;;\nopen(DAT, $infile) || die(&quot;Something did not work.&nbsp;&nbsp;You can email me at james.edge(at)jedge.com\\n&quot;);\n\n#save file contents into an array\n@raw_data=&lt;DAT&gt;;\nclose(DAT);\n\nopen (OUTPUT, &#039;&gt;cacls_parse_output.csv&#039;);\n#Cycle through the entire array\nfor($count=0;$count&lt;=$#raw_data;$count++){\n&nbsp;&nbsp;#pull folder name, split it, and print it\n&nbsp;&nbsp;#the first record in each grouping is the only record with the folder name\n&nbsp;&nbsp;if(@raw_data[$count] =~ \/($ARGV[1])\/){\n&nbsp;&nbsp;&nbsp;&nbsp;chomp(@raw_data[$count]);\n&nbsp;&nbsp;&nbsp;&nbsp;$x = 0;\n&nbsp;&nbsp;&nbsp;&nbsp;while(substr(@raw_data[$count+1],$x,1) eq &quot; &quot;){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$x++;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;$folder = substr(@raw_data[$count],0,$x-1);\n&nbsp;&nbsp;&nbsp;&nbsp;$permissions = substr(@raw_data[$count],$x,length(@raw_data[$count]) - $x);\n&nbsp;&nbsp;&nbsp;&nbsp;print OUTPUT &quot;\\&quot;$folder\\&quot;,\\&quot;$permissions\\&quot;\\n&quot;;\n&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;#cycle through the permissions listed below the folder name\n&nbsp;&nbsp;&nbsp;&nbsp;for($c=$count+1;$c&lt;=$#raw_data;$c++){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$permissions = substr(@raw_data[$c],$x,length(@raw_data[$c]) - $x);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chomp($permissions);chomp($permissions);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#print until you get to the next folder item\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(@raw_data[$c] =~ \/($ARGV[1])\/){last;}\n&nbsp;&nbsp;&nbsp;&nbsp;#another problem is &quot;special access&quot; permissions\n&nbsp;&nbsp;&nbsp;&nbsp;#cycle through including the folder AND the account with access\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($permissions =~ \/special access\/){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(@raw_data[$c] =~ \/. \/){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$special_permissions = substr(@raw_data[$c],$x,length(@raw_data[$c]) - $x);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$special_permissions =~ s\/\\s+\/\/g;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print OUTPUT &quot;\\&quot;$folder\\&quot;,\\&quot;$permissions $special_permissions\\&quot;\\n&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else { print OUTPUT &quot;\\&quot;$folder\\&quot;,\\&quot;$permissions\\&quot;\\n&quot;;} #not &quot;special access&quot; so just print to file\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;}\n}\nclose(OUTPUT);\n<\/code><\/pre><\/p>\n<p><kbd>$perl caclsparse.pl savefile.txt \"C\\\\:\\\\Documents and Settings\\\\jedge\"<\/kbd><\/p>\n<p>NOTE: I run it from Linux but ActivePerl for Windows will work as well. Installing perl is outside the scope of this posting.<\/p>\n<p>Open parseresults.csv in Excel\/OO Calcs\/Gnumeric and begin analyzing the results!<\/p>\n<p>I&#8217;ve noticed that if you use double-quotes you will get the following error (cacls run on Windows 10 and output parsed on Kali 2017-04-04).<br \/>\n<pre><code>\nUnmatched ( in regex; marked by &lt;-- HERE in m\/( &lt;-- HERE C:\\Users\\us315622\\)\/ at caclsparse.pl line 28.\n<\/code><\/pre>You need to use single-quotes.<br \/>\n<kbd>$perl caclsparse.pl savefile.txt 'C\\\\:\\\\Users\\\\edge'<\/kbd><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Updated 7\/2\/2016 &#8211; yeah, I haven&#8217;t needed to parse the shitty output from this tool in 7 years. I never accounted for &#8220;special access&#8221; permissions not including the account with the access. Updated 12\/28\/2018 &#8211; link to the code didn&#8217;t work so I&#8217;ve fixed that. Also updated if you receive a specific error message. CACLS.exe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7,10],"tags":[16,94,87,69,92,93,33,26],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-scripts","category-using-the-tools","tag-audit","tag-cacls","tag-command","tag-command-line","tag-file-permissions","tag-folders","tag-perl","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":34,"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":1196,"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/posts\/91\/revisions\/1196"}],"wp:attachment":[{"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}