U++ framework
Do not panic. Ask here before giving up.

Home » Developing U++ » UppHub » Protect packages - split code encryption,client and server
Protect packages - split code encryption,client and server [message #40222] Sat, 06 July 2013 15:26 Go to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Hi,

I splitted protect package in 3 parts :

Protect         Contains code encryption, can be used standalone
ProtectClient   Client side of network protection
ProtectServer   Server side of network protection


You can use Protect package stand-alone, providing an encryption key provider which can be, for example, an hardware key.
No more dependency from mysql or sqlite

ProtectClient has also SQL dependency removed

The only changes needed in user code are include files;

<Protect/ProtectClient.h> becomes <ProtectClient/ProtectClient.h>
<Protect/ProtectServer.h> becomes <ProtectServer/ProtectServer.h>
<Protect/Protect.h> remains the same.

This package(s) still depend on obsoleted web package found on svn repository; I'm on the way of removing this dependence.

Ciao

Max
Re: Protect packages - split code encryption,client and server [message #40230 is a reply to message #40222] Sun, 07 July 2013 23:29 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
ProtectXXX packages now don't depend anymore on obsoleted Web package
Re: Protect packages - split code encryption,client and server [message #40419 is a reply to message #40222] Wed, 31 July 2013 17:07 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
Where might I find this new version?

I tried using the protect package in my program (the version from the stable upp release).
I use MSVC10 on Windows XP
The program would crash every time on PROTECT_END_FUNC; but obfuscate seems to work. (yes, the keys match)

My businesspartner has a windows7 machine and on his computer the whole program refuses to start. No messages.

[Updated on: Wed, 31 July 2013 17:10]

Report message to a moderator

Re: Protect packages - split code encryption,client and server [message #40420 is a reply to message #40419] Wed, 31 July 2013 17:13 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Hi,

I tested the protect package ONLY on MSC9.
Somebody report it working on MSC10 too, but I'm not sure.

The package, code encryption part, is STRONGLY compiler dependent, so there's no guarantee at all that it'll work on new compiler versions, due to core-rearranges by optimizer.

I tried all my possible to work around this with MSC9 and GCC. but some code path may break it too.
AND, it will surely NOT work on MSC64 bit due of lacking of inline assembly support.

You can try to insert/remove some code inside your protected function to see if something changes; if you've a SHORT non-working testcase I can try (not immediately) to check what's happening there.

Anyways, the new package is on SVN and should be on nighty builds. I'm using it in a commercial app without problems.

Ciao

Max

[Updated on: Wed, 31 July 2013 17:14]

Report message to a moderator

Re: Protect packages - split code encryption,client and server [message #40421 is a reply to message #40420] Wed, 31 July 2013 18:16 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
Could things like pipeline length of the processor be an issue?
I read somewhere that modern processors are not fond of self modifying code. Might trigger virusscanners also.

The protected function is pretty wild with a lot of dependensies, but I'll see what I can do.

(and where can I get the new version for testing?)

[Updated on: Wed, 31 July 2013 18:17]

Report message to a moderator

Re: Protect packages - split code encryption,client and server [message #40422 is a reply to message #40421] Wed, 31 July 2013 18:23 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Alboni wrote on Wed, 31 July 2013 18:16

Could things like pipeline length of the processor be an issue?


maybe, but I doubt it.... my protected functions runs reliably in 12 customers in windows varying from winxp to windows7) and some 200+ people tested it with no problems at all.
On my 4x2 core notebook it runs quite well too.

It may depend on some code-path rearrangements by optimizer... I had some problems because of it on the beginning.

Last version is on svn or on nighty builds.

Ciao

Max
Re: Protect packages - split code encryption,client and server [message #40423 is a reply to message #40222] Wed, 31 July 2013 20:46 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
Ok, I managed to make an example that crashes on closing the app.

Re: Protect packages - split code encryption,client and server [message #40424 is a reply to message #40423] Wed, 31 July 2013 22:29 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Ok, got it, with a simple solution... I don't remember why, but it was because of some compiler workaround, but you NEED a return BEFORE the PROTECT_END_FUNC :

PROTECT_START_FUNC(Decrypt)
....
.....
return;
PROTECT_END_FUNC



I can't hard code it because function may or may not return a value, so you have to put yourself.

Tested with your sample code, don't crash anymore.

On next weeks I'll see if I can find a better solution.

Ciao

Max

[Updated on: Wed, 31 July 2013 22:30]

Report message to a moderator

Re: Protect packages - split code encryption,client and server [message #40425 is a reply to message #40222] Wed, 31 July 2013 23:01 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
Thanks! I'll try it out!

So I can't encrypt a part of a function?

This return doesn't apply to obfuscate I presume?
Re: Protect packages - split code encryption,client and server [message #40426 is a reply to message #40425] Wed, 31 July 2013 23:04 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
The macro is done to be able to encrypt parts, but the bug prevents it for now.
So, for the meantime just put the return, I'll see if I can fix it.
Re: Protect packages - split code encryption,client and server [message #40427 is a reply to message #40425] Wed, 31 July 2013 23:06 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Alboni wrote on Wed, 31 July 2013 23:01


This return doesn't apply to obfuscate I presume?


Nope, this one is working correctly. And that's weird, because obfuscate do a re-encription on exit, so it should be easier to trigger the bug.
Re: Protect packages - split code encryption,client and server [message #40428 is a reply to message #40222] Wed, 31 July 2013 23:38 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
It works on my machine now! Smile
But the version I send to my collegue only had obfuscate in it, and it didn't work on his (Win7) computer. Are there any rules to obfuscate, like can I do....

void function()
{
	......code
	.........
	OBFUSCATE_START_FUNC;
	........
	.. secret stuff
	........	
	OBFUSCATE_END_FUNC;
	........
	more code.......
}

Re: Protect packages - split code encryption,client and server [message #40429 is a reply to message #40428] Wed, 31 July 2013 23:44 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
uhm.... I don't remember.
The obfuscate stuff decrypts on enter, executes and re-encrypts the code on exit. It should be safe, but you MUST NOT have a return in the middle of the encrypted code, otherwise on next call it'll crash.

so, this should be ok :
code
OBFUSCATE_START_FUNC
secret
<NO RETURNS HERE !!!>
OBFUSCATE_END_FUNC
code
return


but NOT :
code
OBFUSCATE_START_FUNC
secret
return                <--- WRONG!
OBFUSCATE_END_FUNC
code
return


(this is clearly stated in bazaar doc page...)

I've just tested it on my virtualbox windows7 and it works well, but I'm using MSC9. IIRC I had some problems with MSC10....

[Updated on: Wed, 31 July 2013 23:45]

Report message to a moderator

Re: Protect packages - split code encryption,client and server [message #40431 is a reply to message #40222] Thu, 01 August 2013 10:22 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
Sadly it still doesn't work on my collegues pc.
Re: Protect packages - split code encryption,client and server [message #40432 is a reply to message #40431] Thu, 01 August 2013 10:26 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Really weird.... maybe he did something with security on his machine ? My app NEEDS to overwrite code, it uses a function that makes code writeabile before decripting it.
I don't know if in windows 7 there's a way to disallow this.
Ask him to disable antivirus first.....

Max
Re: Protect packages - split code encryption,client and server [message #40434 is a reply to message #40222] Thu, 01 August 2013 13:59 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
That didn't work. Maybe he needs admin rights?

[Updated on: Thu, 01 August 2013 14:06]

Report message to a moderator

Re: Protect packages - split code encryption,client and server [message #40435 is a reply to message #40434] Thu, 01 August 2013 14:02 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Nope, on my machines it doesn't.
Could you please send your colleague a small sample to see if it crashes too ?
Re: Protect packages - split code encryption,client and server [message #40436 is a reply to message #40222] Thu, 01 August 2013 14:06 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
Does it execute code in the data segment?
Turning on data execution protection on my XP didn't make it stop working.
Re: Protect packages - split code encryption,client and server [message #40437 is a reply to message #40436] Thu, 01 August 2013 14:10 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Alboni wrote on Thu, 01 August 2013 14:06

Does it execute code in the data segment?
Turning on data execution protection on my XP didn't make it stop working.


Nope, it decripts the code inside code segment, so is the opposite : it reads and write inside code segment.
This is allowed by this call :

bool res = VirtualProtect(start, size, access ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ, &oldProt);


I don't know if it is possible to block it on windows7.
Never had such problems before.....
Re: Protect packages - split code encryption,client and server [message #40438 is a reply to message #40222] Thu, 01 August 2013 14:13 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
I'll make a popup if the call fails with the extended error info
and try again with my program
Re: Protect packages - split code encryption,client and server [message #40439 is a reply to message #40438] Thu, 01 August 2013 14:57 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
No popup so VirtualProtect does not fail or doesn't get called.
Re: Protect packages - split code encryption,client and server [message #40440 is a reply to message #40439] Thu, 01 August 2013 15:41 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
VirtualProtect returns 0 when failing, so you can't see by app crash. You could log the return value. You see the call inside Protect.cpp file.
Re: Protect packages - split code encryption,client and server [message #40441 is a reply to message #40222] Thu, 01 August 2013 16:25 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
Yeah, I did this:

bool PROTECT_WRITE_ACCESS(byte *start, size_t size, bool access)
{
	dword oldProt;
	bool res = VirtualProtect(start, size, access ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ, &oldProt);
	if (!res)
	{
		Exclamation(::Format("VirtualProtect fail %d (size=%d, access=%d)",(int)GetLastError(), (int)size, (int)access));
	}
	
	return res;
}


but no popup appeared.

The testapp I sent you with the "return" modification applied did run btw. But my big app failed silently on my collegues computer, (works on mine) the non encrypted version works on both.
Re: Protect packages - split code encryption,client and server [message #40458 is a reply to message #40441] Mon, 05 August 2013 01:39 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Hi,

I guess I've find the problem.
On next days I'll check it out and submit the fixed code.

Ciao

Max
Re: Protect packages - split code encryption,client and server [message #40462 is a reply to message #40222] Mon, 05 August 2013 10:36 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
Waiting with anticipation Smile
Thank you...
Re: Protect packages - split code encryption,client and server [message #40475 is a reply to message #40462] Tue, 06 August 2013 19:26 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Could you please test replacing Protect.h with attached file ?

(please test BOTH PROTECT and OBFUSCATE, on your friend's machine too. You DON'T need anymore the RETURN of former posts)
  • Attachment: Protect.h
    (Size: 8.55KB, Downloaded 523 times)

[Updated on: Tue, 06 August 2013 19:28]

Report message to a moderator

Re: Protect packages - split code encryption,client and server [message #40478 is a reply to message #40222] Wed, 07 August 2013 02:44 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
I tested briefly on my computer: (xp) on the stable release version.
*Encrypt indeed works without return now.
*Obfuscate now crashes the program. It didn't before.


I will report on the other computer tomorrow.

Re: Protect packages - split code encryption,client and server [message #40479 is a reply to message #40222] Wed, 07 August 2013 03:31 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
I inspected the lines that were altered.

a-b+c sometimes gets interpreted as (a-b)+c and sometimes as a-(b+c) wich yelds a different result.

So I tried using ( ) on this line: (274)
	PROTECT_OBFUSCATE(__startPtr, __endPtr - __startPtr + 2, __keyPtr, 16); \


The version below doesn't crash on my pc, but I don't know if this is what was intended.
	PROTECT_OBFUSCATE(__startPtr, __endPtr - (__startPtr + 2), __keyPtr, 16); \


If I do the same on Encrypt it does crash, so that suggest not.
In any case is it helpful to use ( ) to not let the compiler decide.
Re: Protect packages - split code encryption,client and server [message #40480 is a reply to message #40479] Wed, 07 August 2013 09:33 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Alboni wrote on Wed, 07 August 2013 03:31

I inspected the lines that were altered.

a-b+c sometimes gets interpreted as (a-b)+c and sometimes as a-(b+c) wich yelds a different result.



Nope, + operator associates left to right, so it's always (a - b) + c, which is the intended behaviour.
I forgot to take the 2 bytes of a jmp instruction, when decrypting on windows, at least for PROTECT.
I'll check again OBFUSCATE.....
Re: Protect packages - split code encryption,client and server [message #40481 is a reply to message #40480] Wed, 07 August 2013 09:56 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Please recheck now with attached file.... it should be ok.

Let me know your results on ALL of your tests, I'll wait for it before updating svn.

Ciao

Max
  • Attachment: Protect.h
    (Size: 8.56KB, Downloaded 891 times)
Re: Protect packages - split code encryption,client and server [message #40482 is a reply to message #40222] Wed, 07 August 2013 13:01 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
(I have not recompiled EncryptDecrypt during the tests, I assumed this was not necessary. Let me know if I thought wrong)

encrypt and obfuscate now both run fine on my computer.

Obfuscate gives a compiler warning:
warning C4102: '__end' : unreferenced label



It still doesn't work on my collgues's pc.


I changed protectEncrypt so that it doesn't alter the executable filetime.

[Updated on: Wed, 07 August 2013 13:02]

Report message to a moderator

Re: Protect packages - split code encryption,client and server [message #40483 is a reply to message #40482] Wed, 07 August 2013 13:07 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Alboni wrote on Wed, 07 August 2013 13:01



It still doesn't work on my collgues's pc.




Well... I ran out of ideas about it.... Did you compile the code and sent him the compiled app, or did him compile by himself ?

Does he have the ability of running code inside debugger and give you a backtrace ? (I don't remember well, but IIRC windows has some kind of just-in-time debugger that can do it...).

Did he run the small testcase, or the complete app ?
If the problem is just on latter, could you make him test a small sample app with just the DoMain() and maybe a DoObfuscate() calls ?
Re: Protect packages - split code encryption,client and server [message #40486 is a reply to message #40483] Wed, 07 August 2013 18:49 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
And.... maybe a dumb question, but do the app run on your friend's PC WITHOUT the PROTECT flag (so without the protection code embedded) ?
Re: Protect packages - split code encryption,client and server [message #40487 is a reply to message #40222] Wed, 07 August 2013 22:00 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
I send him the .exe of the complete app (5.2 Mb exe)

Yes it runs absolutely without the protect flag. That's the funny thing. I would not steal your time like that. The software is being used by several clients on various systems.

If I send the testapp I sent earlier, it runs ok for both encrypt and obfuscate. I think there may be some upp elements that do initialisations in a different order perhaps. Then the problem might go away if I create them on the heap.

Or maybe my protected function is too big and a longer jump instruction is generated? I want to hide the whole logic that checks the licence and starts the main window.

Debugging on the other computer:
There was a popup with lots of times FREEFREEFREEFREE in it.

There was a crash in a filehandling routine because the filenamestring contained garbage. It would normally be empty is the functions failed. None of this happens with the protect flag off.

I will try to build a new example that will display the same problem as the big app.


Re: Protect packages - split code encryption,client and server [message #40488 is a reply to message #40487] Wed, 07 August 2013 23:18 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
So, we can try this, but it mus be done on your friend's machine.

1) Remove PROTECT_START_FUNC and END_FUNC macros from your protected function and replace them with the code in Protect.h (remove the backslashes, of course), so you can step inside macros with debugger.

2) Build the app, but do NOT run the ProtectEncrypt on it. Step up to protected function beginning, note the code range of the function, dump it on a file. Name it as UNENCRYPTED.BIN. The difficult part is to find the end of he cunction inside binary code, but you can search for PROTECT_END_MARKER byte sequence.

3) Run ProtectEncrypt on app, then do the same as before. Beware to stop BEFORE the call to Decrypt function.
Store the code area inside ENCRYPTED.BIN file. Take care it has the SAME length as former one.

4) Without exiting debugger, step OVER the decrypt function call, and re-save the binary code inside DECRYPTED.BIN file.
As before, the file should have same length as 2 former files.

5) You can send me the 3 binary files, if you trust. Otherwise, compare the UNENCRYPTED.BIN file with the DECRYPTED.BIN file. They should be identical, besides the marker (PROTECT_START_MARKER and PROTECT_END_MARKER which gets overwritten by ProtectEncrypt.
If there are other differences besides markers, try to locate them.... if they're near end marker, the decrypt routine is missing some parts.

You could also check if ProtectEncrypt do its job on the whole code between both markers, by comparing UNENCRYPTED and ENCRYPDET files. That could give some hints too.

[Updated on: Wed, 07 August 2013 23:23]

Report message to a moderator

Re: Protect packages - split code encryption,client and server [message #40489 is a reply to message #40488] Wed, 07 August 2013 23:21 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Btw, the FREEFREEFREE is a marker of unallocated area used by upp..... I suspect there are some memory problems somewhere, but not sure.

Which are the differences between your machine and your friend's ? OS, 32 or 64 bit, kind of processor.... ?
Re: Protect packages - split code encryption,client and server [message #40490 is a reply to message #40222] Wed, 07 August 2013 23:43 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
Mine: (on wich it all runs)
Intel Pentium 4 3.2Ghz, 2GB ram, Windows XP home (up to date)
keyboard from 1991 Smile

His:
Intel Core i-3 laptop 8GB ram, Windows 7, 64 bit

Compiler: upp version and compiler are the same on both machines:
Upp 5485 and MSVC10
.......
Oh that reminds me... I forgot to give your patched protect.h to him so the debug run is not valid. I will repeat that one tomorrow with the proper version. Embarassed

All the other tests were with executables built on my machine, so those count.



Re: Protect packages - split code encryption,client and server [message #40491 is a reply to message #40490] Wed, 07 August 2013 23:46 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
Alboni wrote on Wed, 07 August 2013 23:43

Mine: (on wich it all runs)
Intel Pentium 4 3.2Ghz, 2GB ram, Windows XP home (up to date)
keyboard from 1991 Smile

His:
Intel Core i-3 laptop 8GB ram, Windows 7, 64 bit




Uhmmm... the big difference is the 64 bit OS.
Could you try it on another machine with windows7 64 bit ? It should make no difference, but that's the only important difference I see....
Re: Protect packages - split code encryption,client and server [message #40492 is a reply to message #40222] Wed, 07 August 2013 23:54 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 216
Registered: January 2012
Location: Kajaani, Finland
Experienced Member
I will try/practise the steps you suggest on my own machine first.

If there are differences they should show up, the code is the same.
My computer might just be more tolerant to "almost right".
Re: Protect packages - split code encryption,client and server [message #40493 is a reply to message #40492] Wed, 07 August 2013 23:56 Go to previous messageGo to previous message
mdelfede is currently offline  mdelfede
Messages: 1310
Registered: September 2007
Ultimate Contributor
It would be quite weird, but you can try.
Between the UNENCRYPTED and DECRYPTED you should find ONLY differences on markers parts. If you find more, tell me.
If you don't, I'm afraid you'll have to test it on your friend's machine.

Ah, btw... I guess you'll need another debugger, the UPP one don't allow to save binary chunks, IIRC.
Previous Topic: Broken packages
Next Topic: Encrypted storage with streaming (OpenSSL, AES)
Goto Forum:
  


Current Time: Sat May 30 07:00:47 GMT+2 2026

Total time taken to generate the page: 0.01800 seconds