TvQuran
Showing posts with label Java Security. Show all posts
Showing posts with label Java Security. Show all posts

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.
read more...

Why Java From Egypt in 2.5 minutes

For most of us, the term tutorial is nothing but boring long files that contain lots of information, we often not need to meet our need,We just need a small hack of how it work. We thought to introduce the boring long tutorials in a simple form for most of us.

Here, from Egypt where one of the oldest civilizations, We provide Mini-Tutorials in one of the newest technologies.

We hope to enjoy your journey with our Mini-Tutorials. If you have an interesting tutorial want to share, please feel free to contact us at : javafromegyptblog[AT]gmail[DOT]com
read more...