| 1 | package engine |
| 2 | |
| 3 | import "strings" |
| 4 | |
| 5 | const ( |
| 6 | windowsSystemSID = "S-1-5-18" |
| 7 | windowsAdministratorsSID = "S-1-5-32-544" |
| 8 | |
| 9 | // Access mask bits that allow modifying, deleting, or taking control of a |
| 10 | // cached directory or Python file on Windows: |
| 11 | // FILE_WRITE_DATA (0x2) | FILE_APPEND_DATA (0x4) | FILE_WRITE_EA (0x10) | |
| 12 | // FILE_DELETE_CHILD (0x40) | FILE_WRITE_ATTRIBUTES (0x100) | |
| 13 | // DELETE (0x10000) | WRITE_DAC (0x40000) | WRITE_OWNER (0x80000) | |
| 14 | // GENERIC_ALL (0x10000000) | GENERIC_WRITE (0x40000000). |
| 15 | windowsUntrustedWriteMask uint32 = 0x500D0156 |
| 16 | ) |
| 17 | |
| 18 | func buildPrivateWindowsSDDL(userSID, ownerSID string) string { |
| 19 | var b strings.Builder |
| 20 | b.WriteString("D:P") |
| 21 | if userSID != "" { |
| 22 | b.WriteString("(A;OICI;FA;;;") |
| 23 | b.WriteString(userSID) |
| 24 | b.WriteString(")") |
| 25 | } |
| 26 | if ownerSID != "" && |
| 27 | !strings.EqualFold(ownerSID, userSID) && |
| 28 | !strings.EqualFold(ownerSID, windowsSystemSID) && |
| 29 | !strings.EqualFold(ownerSID, windowsAdministratorsSID) { |
| 30 | b.WriteString("(A;OICI;FA;;;") |
| 31 | b.WriteString(ownerSID) |
| 32 | b.WriteString(")") |
| 33 | } |
| 34 | b.WriteString("(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)") |
| 35 | return b.String() |
| 36 | } |
| 37 | |
| 38 | func isTrustedWindowsTrusteeSID(trusteeSID, userSID, ownerSID string) bool { |
| 39 | if trusteeSID == "" { |
| 40 | return false |
| 41 | } |
| 42 | return (userSID != "" && strings.EqualFold(trusteeSID, userSID)) || |
| 43 | (ownerSID != "" && strings.EqualFold(trusteeSID, ownerSID)) || |
| 44 | strings.EqualFold(trusteeSID, windowsSystemSID) || |
| 45 | strings.EqualFold(trusteeSID, windowsAdministratorsSID) |
| 46 | } |
| 47 | |
| 48 | func hasUntrustedWindowsWriteAccess(mask uint32) bool { |
| 49 | return mask&windowsUntrustedWriteMask != 0 |
| 50 | } |
| 51 |