TvQuran

Saturday, December 5, 2009

How to secure Java Applications by using VBScript to get a Unique Computer Serial Number

Did you tried many times to get a unique computer ID to use it in securing your java application?!
Do you want to know the easy way to get that ID in just 5 minutes?!!
Yes you can, We will learn how to get basic hardware serial numbers at windows platform and you will find the complete code at the end of the tutorial.

before we start any one may ask why I even want to get this number?
Well, consider that scenario :
you have developed some application and want to distribute it for some of money. We here have a point, how we will be sure that if I sold it for someone, he will not redistribute it, So we will use that Unique ID to give him a copy that will run only on his PC. 
Make sense !, So lets dance.
We will use the VBScript codes on the fly to get the system info like CPU information or Motherboard information Like this:






















Here is the Java source code to get the Unique ID divided on to two parts.
First Part of Java Code:
  • We here open temp File to put the script on it, We use temp folder on C:\ Drive to execute the script and then delete it.












Second Part of Java Code:
  • We here execute the ".vbs" file after writing the script on it and then we retrieve the results and close the file.


















Take on your mind that:
  • When you put your VBScript Code as String you must convert all special characters(e.g the " will be \" ). 
For Example:
If the VBScript is
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
The String will be
String vbs="Set objReg = GetObject(\"winmgmts:{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\default:StdRegProv\")";
Here is the full source code with sample VBScript:
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class VBSUtil {

    private VBSUtil() {
    }
    static String result = "";

    private static String getMotherboardSN() {
        try {
            File f = new File("C:\\");
            File file = File.createTempFile("realhowto", ".vbs", f);
            file.deleteOnExit();
            FileWriter fw = new java.io.FileWriter(file);
            String vbs = "On Error Resume Next\n" + "strComputer = \".\"\n" + "Set objWMIService = GetObject(\"winmgmts:\\\\\" & strComputer &  \"\\root\\cimv2\")\n" + "Set colItems = objWMIService.ExecQuery(\"Select * from  Win32_Processor\")\n" + "For Each objItem in colItems\n" + " Wscript.Echo \"Processor Id: \" & objItem.ProcessorId\n" + "Next\n" + "strComputer = \".\"\n" + "Set objWMIService = GetObject(\"winmgmts:\" _\n" + "  & \"{impersonationLevel=impersonate}!\\\\\" & strComputer &  \"\\root\\cimv2\")\n" + "Set colBIOS = objWMIService.ExecQuery _\n" + "(\"Select * from Win32_BIOS\")\n" + "For each objBIOS in colBIOS\n" + "  Wscript.Echo \"Release Date: \" & objBIOS.ReleaseDate\n" + "next";
            fw.write(vbs);
            fw.close();
            Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                result += line;
            }
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.trim();
    }
}


Thanks for your time and hope this was helpful. For any questions, Please feel free to comment the post.
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

20 comments: on "How to secure Java Applications by using VBScript to get a Unique Computer Serial Number"

Anonymous said...

thanks

Its very good topic and really useful because i search for it before
At All

Special thanks for u

Islam El-Hosary said...

Dear Reader,
you are welcome and wait the coming soon :)
Hope to enjoy your time here.

Unknown said...

Thank you Islam for your efforts, nice starting topic.I suggest the next article shows how it done on a linux machine

Keep moving forward ;)

Yours,
Usama Fath

Islam El-Hosary said...

Dear Usama,
You are welcome and I'm very happy for your comment as I really trust your view of point :)
I'll try to find how to do something like that on Linux soon isA.
Hope to enjoy your time here.

A7med Baraka said...

Great Article Eng Islam
waiting your new ;) Go On

Anonymous said...

thx brother
good job

u r the best

Islam El-Hosary said...

Thnx A7med
pleased to hear that and wait the new stuff ;)

Islam El-Hosary said...

@ Anonymous
Thnx man, I'll do my best to get you the best :)

Ibrahim Elgendy said...

Thank you Islam for your effort and i wait for the next topic

Mona Abdel -Rahman said...

Thanks Islam, a very good topic.

Islam El-Hosary said...

@*
Thnx dears for your support :)

fifi said...

gamd ya smsm masha2aAllah we rabna ywafa2k wemestanyn el gdeeeed mnk

Anonymous said...

مشكووووووووووووووووووووووووووووووووووووووووووووووووووووووووور

Islam El-Hosary said...

@Fifi
Thanks for your support :)
@Anonymous
You are welcome

Beso said...

Sorry for being late Islam.I was sick
I hoped that i was the first one comment on your topic.
This is topic is very good.I was searching how to get the serial Number of the Computer but you bring this to me. :)
Thanx Islam.

Islam El-Hosary said...

@Beso
You shouldn't be sorry at all, you are always welcome.
We are very happy that you liked it and we will do our best to get you the best
Thanks for your support, beso.

Anonymous said...

thanx alot
please, can U post full source code Or complete example

Islam El-Hosary said...

@Anonymous
You can find The Full Source Code at the end of the topic Just search for this at the end {Here is the full source code with sample VBScript:} you can call the "getMotherboardSN()" static method whenever you want, and for sure you may customize the VBScript code.
If you still need any help please feel free to contact us here or at : javafromegyptblog[AT]gmail[DOT]com
Thanks for your consideration ... hope to enjoy the blog

Anonymous said...

thanks its work

Islam El-Hosary said...

@Anonymous
Thanks for your support ..

Post a Comment