28

I am developing a Windows Application. That requires users to register to use it... Now, I am storing my license info as a file in APpData. But deleting that file resets the trial version date. So, I am now planning to save it in registry. But, Most of the Users will not have administrative privileges (Limited Users) in Windows to Access the registry. What can I do ? Where can I save my serial number and date ?

Adriano Repetti
  • 60,141
  • 17
  • 127
  • 190
Yesudass Moses
  • 1,739
  • 2
  • 24
  • 52
  • If the user have to install the application still they need to have installation rights so Admin account is needed, thus if you store it in registry, it will be OK Also similar question is discussed here: http://stackoverflow.com/questions/3625825/how-can-i-make-my-application-have-a-30-day-trial-period – Usman Waheed Dec 19 '13 at 07:50
  • 1
    Why don't you encode the key and save it in database ? – Kurubaran Dec 19 '13 at 07:58
  • @UsmanWaheed No Administrative privileges needed to install my app,,, I dont use any registry in it.... – Yesudass Moses Dec 19 '13 at 07:59
  • @AccessDenied Its already encrypted and stored in a file in AppData folder... But the thing is, If the user deletes the file after program expiry... Program will not get the date installed information, so it will make another file with trial version... 30 days extra user will get ,,, – Yesudass Moses Dec 19 '13 at 08:01
  • @YesudassMoses I suggested some workaround in [my answer](http://stackoverflow.com/a/20676247/1207195) but...as I said there...if they really want then they'll do it, no matters how much effort you put on it (think about 10^9$ companies like Microsoft or Blue-Ray discs copy protection...) – Adriano Repetti Dec 19 '13 at 08:36

3 Answers3

71

In my opinion the point is you have to change how you manage your license. I wrote a project (hosted on git) to demonstrate some of techniques described here.

Where

If they delete license data file then trial restarts? Do not start application if file doesn't exist and create it with an install action first time it's installed.

Now you face a second problem: what if they uninstall and reinstall application? Second step is to move this file to application data folder (for example Environment.SpecialFolder.CommonApplicationData). This is just little bit more safe (because application data won't be deleted when uninstall) but it's still possible for them to manually find and delete it. If application will be installed by low privileges users there isn't much you can do (you can't try to hide license somewhere in Registry).

Now it's a game between you and crackers. They'll win, always. You'll only make life of legitimate users more hard so read cum grano salis. Where you may store license data:

  • Registry. Pro: easy to do. Cons: easy to crack and for low privileges user it's valid only for one user per time. A registry key (in a per-user base) can be somehow hidden if it has \0 in its name. Take a look to this nice post.
  • File. Pro: easy to do and IMO little bit more safe than Registry. Cons: easy to crack (but you can hide it more, see later).
  • Application itself (appending data to your executable, few words about that on this post). Pro: harder to detect. Cons: an antivirus may see this as...a virus and an application update may delete license too (of course if you don't handle this situation properly) so it'll make your code and deployment more complicated.

How to hide license in a file?
If you're going with a file (it doesn't matter where it's located) you may consider to make crackers life (little bit) harder. Two solutions come to my mind now:

  • Alternate Data Streams. File is attached to another file and they won't see it with just a search in Windows Explorer. Of course there are utilities to manage them but at least they have to explictly search for it.

  • Hide it inside application data (a bitmap, for example, using steganography). They just don't know it's license data, what's more safe? Problem is they can easy decompile your C# program to see what you do (see paragraph about Code Obfuscation).

Probably many others (fantasy here is our master) but don't forget...crackers will find it (if they really want) so you have to balance your effort.

How

Keeping your license schema you're now on a dead path. Decision you have to take is if the risk they use trial longer than allowed is higher than risk they stop to use your application because of boring protection.

Validation
If you can assume they have a network connection then you may validate license on-line (only first time they run your application) using some unique ID (even if it's about Windows 8 you may take a look to this post here on SO). Server side validation can be pretty tricky (if you want to do it in the right way), in this post is explained an example of program flow to manage that in a proper way.

Data Obfuscation/Encryption
Your license file/data is now in a safe place. Hardly crackers will find it. Now you need another step: obfuscation. If your license data is in plain text once they found your file it's too easy to change it. You have some options (ordered by increased security and complexity):

  • Obfuscate your files. If they can't understand what's inside a file with a simple text editor (or even a hex editor) then they'll need more time and effort to crack it. For example you may compress them: see this post about XML file obfuscation with compression. Note that also a simple base64 encoding will obfuscate your text files.
  • Encrypt them wit a symmetric algorithm. Even a very simple one will work well, here you're just trying to hide data. See this post for an example. I don't see a reason to prefer this method to a simpler obfuscation.
  • Encrypt them with an asymmetric algorithm. This kind of encryption is a big step in complexity and security and it'll be (very) useful only if license token is provided by a server/external entity. In this case it'll obfuscate license signed with its private key. Client application will validate signature with its public key and even if cracker will find this file (and decompile your code to read public key) they still won't be able to change it because they don't have private key.

Please note that data obfuscation/encryption can be used in conjunction with above mentioned steganography (for example to hide encrypted license file inside an image).

Code Obfuscation
If you're not using license signing with asymmetric encryption then last step is to obfuscate your code. Whatever you will do they'll be able to see your code, check your algorithm and workaround it. So sad, you're deploying instructions manual! Obfuscate with an Obfuscator if you want but what I strongly suggest is to move your license check in a less obvious place.

  • Put all your license related code in a separate DLL. Sign it (be aware that signed assemblies may be decompiled and recompiled to remove signing, there are even tools to do it almost automatically).
  • Pack it inside your executable resources (with a not so obvious name) and do not deploy DLL.
  • Handle event AppDomain.AssemblyResolve, when your DLL will be needed at run-time you'll unpack in memory and return its stream of bytes. See more about this technique in this Jeffrey Richter's post.

I like this method because they'll see there is a license check but...they won't find license code. Of course any good cracker will solve this issue in 10 minutes but you'll be (little bit more) safe from random ones.

Conclusions

To summarize a little bit this is a list of what you may do to provide a stronger license check (you can skip one or more steps, of course, but this will reduce safety):

  • Split your license check code in two assemblies (one to perform the check and manage license and the other to provide a public interface to that engine).
  • Strong sign all your assemblies.
  • Embed your License Engine assembly inside your License Interface assembly (see Code Obfuscation section).
  • Create a License server that will manage your licenses. Be careful to make it secure, to have secure connection and secure authentication (see Validation section).
  • Save license file locally in a safe location (see Where section) and encrypted with an asymmetric encryption algorithm (see Data Obfuscation section).
  • Sometimes validate license with your License Server (see Validation section).

Addendum: Software Protection Dongles
A small addendum about hardware keys (Software protection dongles). They're an invaluable tool to protect your software but you have to design your protection even more carefully. You can assume hardware itself is highly secure but weak points are its connection with computer and communication with your software.

Imagine to simply store your license into the key, a cracker may use an external USB (assuming your SPD is USB) to share same key with multiple computers. You should also store some hardware unique ID within the key but in this case weak point is connection (hardware can be emulated by a software driver). It's a pretty easy crack and this false sense of security ("I'm using Software Protection Dongle, my software is then safe") will make your application even more vulnerable (because you risk to forget other basic protections to simplify license management).

Cost vs benefits for a poor designed protection using SPD should make you consider to use a normal USB pen drive. It costs 1 $ instead of 15/20$ (or much more) for a SPD and you have same level of protection against casual crackers. Of course it won't stop a serious cracker but also a poor designed SPD won't stop it.

A true protection (assuming you're not running on a DRM enabled device) is a dongle which can also execute your code. If you can move some basic algorithms (at least to decrypt vital - and dynamic - support files) into the key then to crack your software they will need to crack hardware. For a half-decent dongle this is a very very very hard task. More carefully you design this and more code you move into the key and more you'll be safe.

In any case you should doubt about marketing campaigns: software protection with a dongle isn't easier. It can be (much) more safe but it isn't as easy as vendors say. In my opinion plug-n-play protection cost is too high compared to its benefits (benefits = how much it'll make crackers' life harder).

Community
  • 1
  • 1
Adriano Repetti
  • 60,141
  • 17
  • 127
  • 190
  • Activation server in PHP & MySQL, plus machine IDs = sorted. – AStopher Jan 04 '16 at 15:49
  • 4
    Absolutely, just implemented similar architecture relying on external Web host. Be careful about client implementation, it's always a weak point. Preparing a git with a reference/example implementation. Someday I'll publish... – Adriano Repetti Jan 04 '16 at 16:25
1

Unfortunately wherever you store licence information on a client's machine it's open to abuse (because it's their machine!).

The only secure way to do this is to have your program check in with a remote service, obviously this requires a lot of overhead.

My own approach is that if customers mess with their licence key then they should expect issues and you are under no obligation to assist. I would make sure your key contains information about the machine it's running on (to prevent simply copying the key) but otherwise keep it very simple.

When researching licencing myself I found a philosophy I tend to stick by - you drive away more potential customers with convoluted and difficult licencing setups than you lose through piracy.

My suggestion would be that you reverse your logic - instead of having allowing the removal of a licence key to restart the free trial why not force them to have a licence key to unlock the full application?

Liath
  • 9,054
  • 9
  • 48
  • 78
  • Yea... The Serial number is based on machine code,,, and it can be useful only for the machine,,, and its MD5 encrypted,,, (I used some filler strings also to it).. Users cannot just copy or decrypt the code... But he can delete the file or key... that will let the program to think its fresh installation,,, and it will create a license file for trial version for another 30 days too... thats the issue :-/ – Yesudass Moses Dec 19 '13 at 07:58
  • I would argue that in that case your program can't be expected to handle it! That's when they have to contact tech support. – Liath Dec 19 '13 at 08:01
  • Sorry, I misunderstood - editing now – Liath Dec 19 '13 at 08:02
  • 2
    @YesudassMoses, then it is rather strange that your app can run without a license. Even the trial should require a license (not generated by your app on that machine). My suggestion is that your app always crashes and disposes itself if without a license. For trial users, give them a web portal to generate trial licenses based on their machine, where you can check if multiple attempts come from a single source. – Lex Li Dec 19 '13 at 08:20
0

If you are going to write to HKEY_CURRENT_USER you won't need Administrative rights. on the other hand, writing to HKEY_LOCAL_MACHINE requires Administrative rights.

be sure when you open the key for writing to call it like this

RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\YourAppPath", true);

if that doesn't work for you, there is a trick to write to the end of the executable file itself, but that's another thing.

Amro
  • 939
  • 1
  • 8
  • 16
  • Its a good idea.... but People can delete the key from registry naa ?? – Yesudass Moses Dec 19 '13 at 08:04
  • 1
    Sure, if they know what they are doing, beside what others suggested, I suggest you use a combined method to make it as complex as possible, although Online authentication/activation would be your best bet. running the app for the first time: writes to the end of the file, creates an encrypted license file, updates a registry key with an encrypted value. any missing item of those combination should let you know that they tried to do something illegal, so shut it down on that machine. – Amro Dec 19 '13 at 08:08
  • I think @Adriano beated me to my last suggestions – Amro Dec 19 '13 at 08:13
  • also consider sending a pretagged setup file/executable that will include info about the license, rather than giving a public download link that anyone can download. – Amro Dec 19 '13 at 08:17
  • @OHDev yes I agree with that! When applicable it's pretty good place to store _hidden_ data. I always [suggest it](http://stackoverflow.com/questions/20361909/c-sharp-play-audio-in-program-on-any-computer/20362046#20362046) for _support_ files. I've more doubt about it if data is dynamic (like a license) because of antivirus. – Adriano Repetti Dec 19 '13 at 08:26
  • @Adriano, a website like LogMeIn uses that when you click add computer, it sends you a single msi file, which when installing, it already has values to link to the account you used to download, and I don't think antivirus will have issues with that, maybe when writing to the end of the file on the client side. – Amro Dec 19 '13 at 10:03
  • @YesudassMoses, this may sound a little strange, but consider lowering the price of the application, the more the price is low and logical to what the user gets, the less people will try to crack your software, it won't be worth it. To be mentioned also, make a free version with reasonable limited capabilities, this will also draw people a little away of cracking your software. – Amro Dec 19 '13 at 10:08
  • @OHDev if he can do everything server side then of course it's a good place to store that. I'm thinking about client side changes (for example to extend trial period, to change it to something else or to add features). Of course this can be managed server side too with a separate "generated" assembly (with its license token appended). As I said...only fantasy can limit us about this topic! – Adriano Repetti Dec 19 '13 at 13:07