I'd like to know how to get hold of the kind of Operating System the jvm is running on. It has to be "secure" as well, so System.getProperty("os.name")
is not really an option because it can be trivially circumvented with the -D directive.
By "secure" I mean nontrivial to circumvent. It's for a desktop application. The user could always deobfuscate, decompile, edit and recompile the code, but that is significantly harder than passing -D to the jvm. We want to make tinkering nontrivial, not impossible (since that can't be done).
First, it's impossible to protect code from being manipulated arbitrarily by its runtime environment. But in order to make it at least hard to fool your check, the best bet is probably some sort of file system based OS fingerprinting.
File.listRoots() is your starting point; on a Unix-like system it will return a single root containing characteristic directories like /etc, /usr, etc. On Windows, it will return multiple results, but AFAIK the OS installation drive is not necessarily C: and the characteristic directories differ across Windows versions and locales - be careful not to assume that everyone runs an English version of Vista.
You could invest a lot of work into recognizing different versions of Windows and Linux, as well as BSD or MacOS - and it would probably take a lot less work to remove the check from the compiled code once it's out there.
The system properties are the only way that I know of to obtain operating system information. Even the OperatingSystemMXBean takes its values from the os.X system properties.
If someone can tinker with how your application is launched, you have bigger problems than if os.name is correct.
However, if you're worried about malicious code setting that property while your application is running, you can use the Java Security Manager to ensure that the properties are guarded safely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With