![]() |
[Updated]Maybe somone else can find the problem here.
Well I started porting over a new loginserver with the latest encryption in C#, can decrypt packets fine but for some reason I keep getting bad data errors when attempting to decrypt username/password so I'm posting my mini debug version of the project in hopes that someone can help or at least use the code to share a solution with me.
UPDATE: Found the problem, client encrypts without padding, the microsoft version of RSA doesn't support unpadded decryption, just throws Not Supported exception, mono on other hand does support raw decryption. Removed code since its useless for windows development, but it should work on mono (don't have to test), so if anyone wants a copy you can PM me. |
hmm... throw a copy my way :)
|
Re:
Ok, I re-added the download.
As it stands, I'm thinking about just using a ported BigInteger class with a modPow function to handle decryption but I'm not 100% certain it can handle it, or for that matter how to do it since the documentation on the class is almost non-existant. I've given up on the username/password decrypting part for now, I just validate any login calls now regardless of credentials and have moved over to working on differant parts of the emulator. But if anyone does come up with a solution please let me know! And ignore the sloppy way I handle connections, its just easier to use tcplistener for this little app, my actual version uses a high performance asynchronous I/O callback socket model with thread pooling that can hopefully handle around 4000 connections and Net 2.0 has some interesting new features I wanna look at, the new asynchronous accepts and socket.disconnect in particular. |
I not well know this (RSA and other), but maybe this link will usefull for you
http://www.koders.com/csharp/fid657B...68CDC310A.aspx Idea, look the source of DecryptValue method and implement in loginserver. Also the sorcecode avalible for the mono package |
Re:
Thanks for the link, unfortunately all that version does is default DecryptValue to use PKCS 1.5 padding, but it looks like I might be able to pull something useful out of the source so it may not be a bust after all.
|
Re
I ended up finally able to decrypt it just using the RSA decryption algorithim t = (c^d) mod m using my own power reduced modulo method.
|
yeah... nice job on it :)
u could try sonic.net for the connect thread pool were intending to use it on our own implementation as well it's pretty good and easy to use :) btw... do get to me on irc/icq/msn, i could drop u alot more codes to help ya with the project |
I was trying to merge Mono http://svn.myrealbox.com/source/trun.../RSAManaged.cs with your first ver. of AuthD.... but it doesn't work :( Something I can't understand. Perhaps on MONO it is also not work
|
I spend my xmas vacation to find solution decrypt RSA, and find that problem in generation modulus or something like this. Cause all other modifications on incoming packet give same result as in java. And if i understand right, sever must sends modulus and public exponent to client, but it sends only modulus. :(
So i didn't find solution...Anyone find? |
The public key has to be scrambled before being sent to the client.
It can be scrambled by doing the following: 1. Swap the first 4 bytes with the last 4 bytes 2. Xor first 64 bytes with last 64 bytes 3. Xor bytes 13 - 16 with bytes 52 - 56 4. Xor last 64 bytes with first 64 bytes I have not had any luck using C#'s RSA functions to decrypt packets tho, but I haven't been working on it too hard as I got frustrated. Only the public modulus is sent to the client as the client can use whatever exponent it wants to encrypt the packet, the exponent isn't used in the decryption process at all. |
Yeah, I do encrypt the public key as original :)
So I read many docs about RSA encryption and all says that Quote:
|
Ya thats correct forumula, the reason that the exponent isnt sent to the client is because 99% of the time, the exponent is either 3 or 65537 (usually this one, represented in byte[] form as 1,0,1), and since the exponent isnt used in decryption, the server could care less what the exponent is, so the client just creates its own, the only really important parts are N (public) and D (private).
The real problem with RSA and C# is that M$ thinks that everything RSA related should be padded, with either PKCS#1 v2 or PKCS#1 v1.5. Lineage 2 doesn't need to pad because the packet is always under 128 in length and there fore, never larger then the key. In Java, you can tell the RSA engine this by using NoPadding options. In C# if you try this (by using a direct crypto stream), C# will complain and either throw an exception or try to use PKCS#1 v1.5 by default, which of course screws up the decryption. So the only option is to use the formulas yourself to decrypt, which means you'll need access to Big Integers since a 1024 bit RSA key is larger then a double. You can find many free C# versions of a BigInt on the web. Next you'll need a modpow method to perform the math on the BigInt. Last you'll need a method to perform ECB block handling on the encrypted data. The last is actually fairly easy, since the block size is the length of the key, so technically all you need to do is convert the encrypted data from little-endian to big-endian order. The followin was my attempt at basic RSA encryption and decryption using a Big Integer class found at http://www.codeproject.com/csharp/biginteger.asp Code:
using System;Here's an example of how to use it: Code:
|
Yes!! :) Finally I've made it!!
|
Have you decrypted the RSA in C#, share how you did it, cause I still have yet to reliably do it.
|
Yes... it was today about 0.30 AM :) I use BigInteger class for encripting and dercrypting. For some reason decrypted packet goes shrinked, all leading null-bytes are missing, but I think it not to important... So, now I've changed offsets, and can get login and password. I want to test some other parts where RSA decryption used, and if all fine, I'll share it. :)
|
Here complete pack of RSA implemetation taken from Mono. I've just changed thi namespace for my needs. It can be use as normal RSACryptoServiceProvider :)
This is 7zip archive renamed to *.rar |
Interesting, how are you going about decrypting the packet, are you using the RSAManaged class or just using the BigInteger class.
I tried both and neither method decrypted the packet so I might have used them wrong. I'd like to see the section of code you use to decrypt the packet and get the username and password to see where I'm going wrong. |
I've found one error in your first AuthD and in my code, that Iappend to it. Here is yours In server.c you ctreate
Code:
new ClientThread(stream, rsaPrivateParams, rsaPublicParams, scrambledModulus);Code:
public ClientThread(NetworkStream conn, RSAParameters rsaPublic, RSAParameters rsaPrivate, byte[] scrambled)and here is RequestAuthLogin Code:
public RequestAuthLogin(byte[] rawPacket, RSAParameters rsaPrivateParams) |
Well I see where I went wrong, and all is good now, great work!!!!
Just remember though, C# String.Trim doesn't remove null terminators, so you'll probably wanna change: username = new System.String(dec, 0x02, 14).Trim(); username = username.ToLower(); password = new string(dec, 0x11, 16).Trim(); To: String trimString = "\0 "; username = new System.String(dec, 0x02, 4).Trim(trimString.ToCharArray()); username = username.ToLower(); password = new string(dec, 0x11, 16).Trim(trimString.ToCharArray()); I think we're the first people to crack C4 encryption in C#, woot! Though I did it differant, I didn't use RSAManaged at all, I just used BigIntegers to do it, I'm not too concerned with all the extra crap from RSAManaged like keyblinding and what not, to think all that effort looking for a new way to decrypt it and I had it all along except for a tiny little mistake in my code. That'll teach me. But again, good job on all the work! |
Can any of you post your work with the encryption. I myself am willing to work more on GameServer stuff but i will need that part to start working at all.
|
... was it not posted, eh? Use ur brain :)
|
Heya, did u guys worked on off (lineage2.com) login server encryption (non blowfish based)?
If so, any results? Thx. |
no, it's blowfish I do not know what is in OFF server.....
|
http://freepository.com
^ some new free repository that emailed me.. thought it might come in handy for u guys if your ever want to code together or something :) (P.S how status so far? :x) |
I'm still have a problems with RSA decription, all tests havn't any realy result, help to me please
my decrypt code: Code:
public RequestAuthLogin(byte[] rawPacket, RSAParameters rsaParameters) |
| All times are GMT. The time now is 15:40. |