Last active
February 10, 2022 14:03
-
-
Save miromannino/04be6a64ea0b5f4d4254bb321e09d628 to your computer and use it in GitHub Desktop.
Convert a SID to String with Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>SIDConvertToString</groupId> | |
<artifactId>SIDConvertToString</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<build> | |
<sourceDirectory>src</sourceDirectory> | |
<plugins> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.1</version> | |
<configuration> | |
<source>1.6</source> | |
<target>1.6</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<dependencies> | |
<dependency> | |
<groupId>net.java.dev.jna</groupId> | |
<artifactId>jna</artifactId> | |
<version>4.1.0</version> | |
</dependency> | |
<dependency> | |
<groupId>net.java.dev.jna</groupId> | |
<artifactId>jna-platform</artifactId> | |
<version>4.1.0</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SIDConverter { | |
public static String convertSidToStringSid(byte[] sid) { | |
int offset, size; | |
// sid[0] is the Revision, we allow only version 1, because it's the | |
// only that exists right now. | |
if (sid[0] != 1) | |
throw new IllegalArgumentException("SID revision must be 1"); | |
StringBuilder stringSidBuilder = new StringBuilder("S-1-"); | |
// The next byte specifies the numbers of sub authorities (number of | |
// dashes minus two) | |
int subAuthorityCount = sid[1] & 0xFF; | |
// IdentifierAuthority (6 bytes starting from the second) (big endian) | |
long identifierAuthority = 0; | |
offset = 2; | |
size = 6; | |
for (int i = 0; i < size; i++) { | |
identifierAuthority |= (long) (sid[offset + i] & 0xFF) << (8 * (size - 1 - i)); | |
// The & 0xFF is necessary because byte is signed in Java | |
} | |
if (identifierAuthority < Math.pow(2, 32)) { | |
stringSidBuilder.append(Long.toString(identifierAuthority)); | |
} else { | |
stringSidBuilder.append("0x").append( | |
Long.toHexString(identifierAuthority).toUpperCase()); | |
} | |
// Iterate all the SubAuthority (little-endian) | |
offset = 8; | |
size = 4; // 32-bits (4 bytes) for each SubAuthority | |
for (int i = 0; i < subAuthorityCount; i++, offset += size) { | |
long subAuthority = 0; | |
for (int j = 0; j < size; j++) { | |
subAuthority |= (long) (sid[offset + j] & 0xFF) << (8 * j); | |
// The & 0xFF is necessary because byte is signed in Java | |
} | |
stringSidBuilder.append("-").append(subAuthority); | |
} | |
return stringSidBuilder.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.*; | |
import org.junit.Assume; | |
import org.junit.Test; | |
import com.sun.jna.platform.win32.Advapi32Util; | |
import com.sun.jna.platform.win32.Advapi32Util.Account; | |
import com.sun.jna.platform.win32.WinNT.PSID; | |
public class TestSIDConverter { | |
private static final String accountNameTest = "[email protected]"; | |
private static void generateCombinations(byte[] sid, int offset) throws Exception { | |
String convertedSid, convertedSid2; | |
if (offset >= sid.length) { | |
convertedSid = Advapi32Util.convertSidToStringSid(new PSID(sid)); | |
convertedSid2 = SIDConverter.convertSidToStringSid(sid); | |
if (!convertedSid.equals(convertedSid2)) { | |
throw new Exception("Conversion Error: " | |
+ convertedSid2 + " instead of " + convertedSid); | |
} | |
return; | |
} | |
for (int i = 0; i <= 255; i += 255/3) { | |
sid[offset] = (byte)(i & 0xFF); | |
generateCombinations(sid, offset+1); | |
} | |
} | |
@Test | |
public void testAccount() { | |
Assume.assumeNotNull(accountNameTest); | |
Account account = Advapi32Util.getAccountByName(accountNameTest); | |
assertEquals(account.sidString, SIDConverter.convertSidToStringSid(account.sid)); | |
} | |
@Test | |
public void testCombinations() throws Exception { | |
byte[] sidTemplate = new byte[28]; | |
sidTemplate[0] = 1; | |
generateCombinations(sidTemplate, 1); | |
} | |
} |
Greate.
This can work in .net core in linux version to convert objectSid to string value.
Thanks:)
The License is: Do Whatever You Want With It 2.0
aka I don't care, I hope it helps you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @miromannino,
Thank you very much for this very cool gist! 😄
Would you be so kind to tell me under what license it is published?
Again, thanks!
Cheers