SSL_accept returns SSL_ERROR_SSL - c++

I have a strange error working with openssl in c++ with non blocking sockets.
SSL_accept return SSL_ERROR_SSL.
error string is
"error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol"
as obtained from
ERR_error_string(ERR_get_error(), NULL);
ssldump:
New TCP connection #1: localhost(35677) <-> localhost(8084)
1 1 1443513828.4788 (0.0001) C>SV3.1(153) Handshake
ClientHello
Version 3.3
random[32]=
e9 fc d9 2b 25 20 77 9f 5f a4 b1 eb 5d 50 15 f2
38 7a a9 86 16 49 f8 a3 e9 00 7f 93 11 cd 7b b1
cipher suites
Unknown value 0xc02b
Unknown value 0xc02f
Unknown value 0xc00a
Unknown value 0xc009
Unknown value 0xc013
Unknown value 0xc014
Unknown value 0xc012
Unknown value 0xc007
Unknown value 0xc011
TLS_DHE_RSA_WITH_AES_128_CBC_SHA
TLS_DHE_DSS_WITH_AES_128_CBC_SHA
Unknown value 0x45
TLS_DHE_RSA_WITH_AES_256_CBC_SHA
TLS_DHE_DSS_WITH_AES_256_CBC_SHA
Unknown value 0x88
TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
TLS_RSA_WITH_AES_128_CBC_SHA
Unknown value 0x41
TLS_RSA_WITH_AES_256_CBC_SHA
Unknown value 0x84
TLS_RSA_WITH_3DES_EDE_CBC_SHA
TLS_RSA_WITH_RC4_128_SHA
TLS_RSA_WITH_RC4_128_MD5
compression methods
NULL
1 2 1443513828.4790 (0.0002) S>CV3.3(53) Handshake
ServerHello
Version 3.3
random[32]=
aa d6 10 d1 eb b5 de b1 ed f8 6b 2f 44 fa 9f 57
14 1d c0 27 a1 e9 24 3a 20 cb 09 fa 75 33 1c c7
session_id[0]=
cipherSuite TLS_RSA_WITH_AES_128_CBC_SHA
compressionMethod NULL
1 3 1443513828.4790 (0.0000) S>CV3.3(1003) Handshake
Certificate
certificate[993]=
30 82 03 dd 30 82 02 c5 a0 03 02 01 02 02 09 00
bc 6b da c1 c3 f0 3f 14 30 0d 06 09 2a 86 48 86
f7 0d 01 01 0b 05 00 30 81 84 31 0b 30 09 06 03
55 04 06 13 02 49 4e 31 0b 30 09 06 03 55 04 08
0c 02 49 4e 31 10 30 0e 06 03 55 04 07 0c 07 67
75 72 67 61 6f 6e 31 0e 30 0c 06 03 55 04 0a 0c
05 69 72 65 78 78 31 0c 30 0a 06 03 55 04 0b 0c
03 64 65 76 31 12 30 10 06 03 55 04 03 0c 09 6c
6f 63 61 6c 68 6f 73 74 31 24 30 22 06 09 2a 86
48 86 f7 0d 01 09 01 16 15 76 69 6b 61 73 73 6f
6f 64 31 31 40 67 6d 61 69 6c 2e 63 6f 6d 30 1e
17 0d 31 35 30 39 32 39 30 38 30 32 35 36 5a 17
0d 31 36 30 39 32 38 30 38 30 32 35 36 5a 30 81
84 31 0b 30 09 06 03 55 04 06 13 02 49 4e 31 0b
30 09 06 03 55 04 08 0c 02 49 4e 31 10 30 0e 06
03 55 04 07 0c 07 67 75 72 67 61 6f 6e 31 0e 30
0c 06 03 55 04 0a 0c 05 69 72 65 78 78 31 0c 30
0a 06 03 55 04 0b 0c 03 64 65 76 31 12 30 10 06
03 55 04 03 0c 09 6c 6f 63 61 6c 68 6f 73 74 31
24 30 22 06 09 2a 86 48 86 f7 0d 01 09 01 16 15
76 69 6b 61 73 73 6f 6f 64 31 31 40 67 6d 61 69
6c 2e 63 6f 6d 30 82 01 22 30 0d 06 09 2a 86 48
86 f7 0d 01 01 01 05 00 03 82 01 0f 00 30 82 01
0a 02 82 01 01 00 e2 9e cb 0c 9a c2 a4 0a ff 81
77 23 ad 74 c6 a0 12 9f f4 a4 86 94 bf 02 57 cc
ff 13 cf 4f 13 83 6e 70 8b 2d ee 92 93 94 44 d4
db 20 e1 67 65 3a 67 c1 d5 91 67 ab f9 98 aa 59
16 b4 38 99 92 66 c2 de e7 88 82 cd a0 10 95 89
78 f5 64 6a e5 b6 75 a2 e0 d0 76 c3 57 92 03 c7
d3 7d f3 c5 18 e5 bf 8d f5 71 17 4c dd 18 fa 11
8c d1 ed 8b 0f b6 5c fb 16 bf 5a 44 23 71 c8 83
83 5a 4d ca b0 8c 15 96 66 dc cd 9a 06 33 b3 eb
71 43 25 b1 0f df 1a c6 f1 7a 4c 4d 5e 34 12 b7
70 76 b6 f9 7e 40 6c 2e 70 df 26 cd f9 c9 0b 90
57 dd 0b c3 57 b8 55 ad 63 13 16 15 bf 8d 3b 48
9d 11 cf 15 55 a1 f2 ab ff 8f 43 48 09 e7 c9 b0
21 66 e7 e4 76 14 aa d4 a9 88 ef bf e1 db 0f b3
27 95 d0 c9 50 6f 36 c8 5e 47 3f c3 a8 59 9c a6
8a 75 57 0b db 83 c6 08 13 fa 9c 64 3c 5a 4e d5
ba 4f 23 39 67 77 02 03 01 00 01 a3 50 30 4e 30
1d 06 03 55 1d 0e 04 16 04 14 29 86 80 ff 9c 14
00 5e 2e 89 81 4a 1e 3c 32 82 ec 44 35 7b 30 1f
06 03 55 1d 23 04 18 30 16 80 14 29 86 80 ff 9c
14 00 5e 2e 89 81 4a 1e 3c 32 82 ec 44 35 7b 30
0c 06 03 55 1d 13 04 05 30 03 01 01 ff 30 0d 06
09 2a 86 48 86 f7 0d 01 01 0b 05 00 03 82 01 01
00 c9 f4 f7 0f bd a4 f2 81 e4 4e c5 4d 86 9d f3
30 fe df f6 b1 b5 a3 a6 ef 41 51 fc f3 1b c5 be
e1 1f 75 fe f1 ee 94 95 54 c7 07 13 d4 b6 89 2b
a3 86 e3 7b 12 6d 0a 51 66 31 74 d5 83 67 92 79
45 b3 03 1d 77 6b b0 71 a0 07 c0 06 e6 6b 81 74
01 8e 6d a6 c1 82 8b 34 b2 6c 52 b9 3e a6 c0 c5
ce f6 c8 b2 4b 6a 8c 43 4a 20 38 61 8a 09 1f 39
cd e6 4a 0b 6c 89 b8 88 74 d7 1e fa 86 dc 0a cd
d4 73 39 5f 53 4b 13 79 f8 cd 62 ee 38 42 61 56
26 31 44 ac 5b 39 24 2b 3f f9 21 b4 59 25 14 95
69 e6 33 41 df ef b5 ea 20 7d 38 a9 f4 c3 d1 f7
c5 37 4a c9 01 cd f4 3a e7 3c a7 16 33 0f 5d 46
19 bc 50 3d 33 05 63 ee 29 51 d1 79 c4 01 04 0f
be 27 65 57 ab e5 e8 d3 bf 4d 11 55 bc 52 e4 9f
d9 0c 87 be a9 62 9d be 65 21 a8 8a 46 63 d1 91
f9 2b e1 c6 5d 33 fb ae b6 9e a0 36 0b 37 2a fa
3b
1 4 1443513828.4790 (0.0000) S>CV3.3(4) Handshake
ServerHelloDone
1 5 1443513828.4800 (0.0009) C>SV3.3(262) Handshake
ClientKeyExchange
EncryptedPreMasterSecret[256]=
76 58 13 c2 dd ed fb 6e 7b 49 aa 0c 26 40 a1 1f
8b 87 b0 f6 a5 f8 24 fe 0c 7b a9 e6 b0 51 b1 92
2c 52 33 ba af 2a d2 52 b8 80 0f ea fd 24 b7 89
15 2f 0e 90 86 ef 9c 9d 52 02 ca 56 09 b5 81 5f
e1 05 a8 9a 44 32 7f 4f ec 58 33 4a 97 96 23 15
7d a8 1d 8c bc 5d 20 13 39 54 2f 61 27 be aa 6a
ec 49 be 82 cc 2b c7 3d a3 b0 cb 4f 26 cb 14 41
d4 4b 00 57 6f 52 2b 66 aa 23 97 ed e9 e7 9c d9
ed 81 28 0c 08 9f dd 9d e4 00 b5 ba aa a4 f6 3b
23 87 f6 da 64 c0 1e 33 87 bb ef bc c3 fe 28 64
d1 c8 ff ae f9 3f 5a fe 38 e6 ab 3e 34 2c b5 96
f7 a8 99 b6 8e b3 46 d3 c9 f8 f8 94 26 97 27 85
5f 3e ff 85 15 5f c8 1f 10 53 26 e4 88 32 4f 48
62 87 ac a8 9f 14 f1 e3 f4 c4 1a 71 49 e6 33 15
16 66 66 ec 68 ee f7 91 0c 4c b3 3a 49 88 e1 31
a5 fe 4c 31 35 3d dc 01 f6 be f2 e5 65 a0 bb 78
1 6 1443513828.4800 (0.0000) C>SV3.3(1) ChangeCipherSpec
1 7 1443513828.4800 (0.0000) C>SV3.3(64) Handshake
Unknown SSL content type 72
1 1443513828.4801 (0.0001) S>C TCP RST
I am not sure why the server resets the connection.
I am using a self signed certificate to test a websocket connection using scheme wss. Client is Mozilla Firefox and websocket server is what I am trying to implement.
Any pointers on why the server is resetting the connection?
error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol
What possible reason could throw unknown protocol?
Thanks
Edited: Adding code. It is not all the code but gives a fair idea of what i did.
Here is how my code looks like:
/*
this the main method that accepts incoming connection on a listening socket.
*/
SecureServer::Accept()
{
int ret = 0;
while(!mFinished) {
ret = epoll_wait(millis);
if(ret == -1) {
continue;
}
else if(ret == 0) {
idle(0);
}
else if(ret > 0) {
//some events are triggered
for(int i=0; i<ret; i++) {
//getEvent method is in the base class.
//SecureServer is derived from this class.
//Base class is just an epoll wrapper
epoll_event* ev = getEvent(i);
//check for error
if( (ev->events & EPOLLERR) || (ev->events & EPOLLHUP) || !(ev->events & EPOLLIN)) {
ERROR(Log::eSecureServer, "Error occured on fd = %d", ev->data.fd);
removeEpollFd(ev->data.fd);
continue;
}
//check if its a new incoming connection on server socket
else if(ev->data.fd == mServerSocket) {
//accept the connection
struct sockaddr peerAddress;
socklen_t socklen = sizeof(peerAddress);
int newSocket = accept(mSocket, &peerAddress, &socklen);
if(newSocket == -1) {
ERROR(Log::eSecureServer, "Error in accept, listen address = %s, port = %d epoll_wait returned = %d", mHost.c_str(), mPort, ret);
continue;
}
rxUShort port = ntohs(*((rxUShort*) &(peerAddress.sa_data[0])));
char ipPort[32] = {0};
sprintf(ipPort, "%hhu.%hhu.%hhu.%hhu:%hu", peerAddress.sa_data[2],peerAddress.sa_data[3],peerAddress.sa_data[4],
peerAddress.sa_data[5],port);
INFO(Log::eSecureServer, "SecureTCPServer received incoming client connection from = %s", ipPort);
//add epoll fd
// This method also makes the socket NON BLOCKING
addEpollFd(newSocket);
}
//check the handshake from a previously accepted connection
else {
//process the handshake
processHandshake(ev->data.fd);
}
}
}
}
}
/*
This method determines the client and server protocol
*/
int Protocol::processHandshake(int fd)
{
int length = 0;
length = recv(fd, handshake, 1024, MSG_PEEK);
if(length == 0) {
return UNKNOWN_PROTOCOL;
}
else if((bcmp(handshake, "\x16", 1) == 0) ||
(bcmp(handshake, "\x80", 1) == 0)) {
//secure connection requested on this fd
SSL* pSSL = 0;
int ret = NewSSLSocket(pSSL, fd);
if(SSL_ERROR_NON == ret) {
///success
}
else {
if(SSL_ERROR_WANT_READ == ret || SSL_ERROR_WANT_WRITE == ret) {
NFO(Log::eProtocol, "Handshake error, in progress.");
return eProtocolHandshakeInProgress;
} else if(SSL_ERROR_SSL == ret) {
INFO(Log::eProtocol, "Handshake error, library error");
return eProtocolInvalidHeader;
} else if(SSL_ERROR_SYSCALL == ret) {
INFO(Log::eProtocol, "Handshake error, possibly syscall error");
return eProtocolInvalidHeader;
}
SSL_free(pSSL);
}
}
}
int Protocol::NewSSLSocket(SSL* pSSL, rxUInt fd)
{
pSSL = SSL_new(mSSLContext);
SSL_set_fd(pSSL, fd);
int ret = SSL_accept(pSSL);
switch(SSL_get_error(pSSL, ret)) {
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
{
INFO(Log::eNetwork, "SSL Handshake is in progress on fd = %d %s", fd, ERR_error_string(ERR_get_error(), NULL));
return SSL_ERROR_WANT_READ;
}
break;
case SSL_ERROR_SYSCALL:
{
INFO(Log::eNetwork, "SSL syscall error during handshake on fd = %d %s", fd, ERR_error_string(ERR_get_error(), NULL));
return SSL_ERROR_SYSCALL;
}
break;
case SSL_ERROR_SSL:
{
INFO(Log::eNetwork, "SSL library error during handshake on fd = %d %s", fd, ERR_error_string(ERR_get_error(), NULL));
return SSL_ERROR_SSL;
}
case SSL_ERROR_NONE:
{
INFO(Log::eNetwork, "SSL handshake on fd = %d complete", fd);
return SSL_ERROR_NONE;
}
break;
default:
{
INFO(Log::eNetwork, "SSL unknown error during handshake on fd = %d %s", fd, ERR_error_string(ERR_get_error(), NULL));
return -1;
}
break;
}
return -1;
}
//Protocol Constructor Init the SSL Context
Protocol::Protocol()
{
const SSL_METHOD* pSSLMethod = SSLv23_server_method();
char err[1024] = {0};
mSSLContext = SSL_CTX_new(pSSLMethod);
if(mSSLContext == NULL) {
ERROR(Log::eNetwork, "Failed to initialize SLS_CTX. TLS Unavaliable.");
ERROR(Log::eNetwork, ERR_error_string(ERR_get_error(), err));
} else
INFO(Log::eNetwork, "Initialized SSL Context.");
SSL_CTX_set_cipher_list(mSSLContext, "ALL");
//Load the SSL Certificates
rxUInt ret = 0;
ret = SSL_CTX_use_certificate_file(mSSLContext, mCACertificateFile.c_str(), SSL_FILETYPE_PEM);
if(ret <=0 ) {
ERROR(Log::eNetwork, "Failed to read ssl sertificate. We should exit.");
ERROR(Log::eNetwork, ERR_error_string(ERR_get_error(), err));
} else
INFO(Log::eNetwork, "SSL Certificate read complete. OK!");
//Load the private key file
ret = SSL_CTX_use_PrivateKey_file(mSSLContext, mPrivateKeyFile.c_str(), SSL_FILETYPE_PEM);
if(ret <=0 ) {
ERROR(Log::eNetwork, "Failed to read private key file. We should exit.");
ERROR(Log::eNetwork, ERR_error_string(ERR_get_error(), err));
} else
INFO(Log::eNetwork, "Private Key read complete. OK!");
if(!SSL_CTX_check_private_key(mSSLContext)) {
ERROR(Log::eNetwork, "Private key does not match the SSL Certificate. We should exit.");
ERROR(Log::eNetwork, ERR_error_string(ERR_get_error(), err));
} else
INFO(Log::eNetwork, "Private Key and SSL Certificate Matched. OK!");
}

1 7 1443513828.4800 (0.0000) C>SV3.3(64) Handshake
Unknown SSL content type 72
My guess is that you are sending some plain data on the connection, i.e. write to the plain socket instead to the SSL socket. This is just a guess because you did not provide any code but looking for such error one finds issues like this.

Related

How can I save raw bytes into a PNG file?

I have an array of bytes, they are a raw PNG file that was originally a base64 encoded string.
The encoded string:
stEncodedQR = "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";
The raw bytes:
(src\funcs.cpp:1064) DecodedQRPicture ptr:0x19E51C0 size:0x673
(:0) ---- ----------------------------------------------- ----------------
(:0) 000| 89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52 .PNG........IHDR
(:0) 010| 00 00 01 40 00 00 01 40 01 00 00 00 00 E5 EB 98 ...#...#.....ek.
(:0) 020| 07 00 00 06 3A 49 44 41 54 78 DA ED 9A 31 8E AB ....:IDATxZm.1.+
(:0) 030| 4A 10 45 0B 11 90 D9 1B 40 EA 6D 90 F5 96 F0 06 J.E...Y.#jm.u.p.
(:0) 040| 30 6C 00 6F 89 8C 6D 20 B1 01 C8 08 10 F5 CF 65 0l.o..m 1.H..uOe
(:0) 050| 22 7B 9E F4 7F 40 BF E0 CB D6 48 A3 B1 0F A3 56 "{.t.#?`KVH#1.#V
(:0) 060| 77 D5 AD 5B D5 36 FF 8F 2F FB 82 5F F0 0B 7E C1 wU-[U6../{._p.~A
(:0) 070| 2F F8 05 FF CF E0 62 F6 F4 79 29 CA C7 30 3D 3C /x..O`bvty)JG0=<
(:0) 080| 1C E6 47 0C FC 59 C7 DD 2C 24 05 57 DF 1F 63 E8 .fG.|YG],$.W_.ch
(:0) 090| DC BB 21 F7 2D 7F 6D 61 1D ED 31 4E CF B1 7C 7A \;!w-.ma.m1NO1|z
(:0) 0A0| 62 70 28 1B 9B 5B 0B EB B6 5B B1 3F 60 B5 F0 F2 bp(..[.k6[1?`5pr
(:0) 0B0| E1 65 5D 24 07 EB 68 F7 31 6F A3 F7 85 DD 8A FD ae]$.khw1o#w.].}
(:0) 0C0| BE 4D 75 51 DE FF 0A D8 14 B9 FB 5E 9B DD DD 7B >MuQ^..X.9{^.]]{
(:0) 0D0| E3 C5 A3 41 FF 20 35 C8 C9 0C 1C 0B 11 61 6C CF cE#A. 5HI....alO
(:0) 0E0| 73 9B 9E 83 82 82 C5 FE 3E C2 6B 41 E2 F1 31 96 s.....E~>BkAbq1.
(:0) 0F0| 7F FC F9 15 B8 17 83 BC 8E 6A 7E 8D 65 E6 F9 51 .|y.8..<.j~.efyQ
(:0) 100| F1 90 3D 87 7C 89 F3 9F D2 F5 62 90 D0 B3 6A EE q.=.|.s.Rub.P3jn
(:0) 110| DC 6A 0B E0 8D 4D 66 F9 3A 2A 33 FA 2A 31 18 E7 \j.`.Mfy:*3z*1.g
(:0) 120| A3 28 9F 23 41 31 1F 55 DE 8D D3 7D B0 A6 DA 33 #(.#A1.U^.S}0&Z3
(:0) 130| AD 3A 2D C8 6F A2 6F A9 90 9C FD 3E 5A 23 9C A0 -:-Ho"o)..}>Z#.
(:0) 140| D8 EB 6A EE 63 5A F0 E0 E4 CF 14 6C 2B 42 72 BF XkjncZp`dO.l+Br?
(:0) 150| 45 AB 15 8C 73 B7 B1 6D 21 29 B8 54 65 36 70 26 E+..s71m!)8Te6p&
(:0) 160| 46 DE DF F4 D0 DE 54 08 1E E9 38 2F 96 16 3C 8A F^_tP^T..i8/..<.
(:0) 170| BC 1B 50 9A D0 16 53 C3 7E 0C AC 2E B4 91 B3 B2 <.P.P.SC~.,.4.32
(:0) 180| 6C 4C 0C 1A 22 47 08 A0 7C BE 14 D3 7D 0B 3E D8 lL.."G. |>.S}.>X
(:0) 190| 73 2B 6F 31 6F CD 13 83 FA E9 46 36 A3 BC 0F 88 s+o1oM..ziF6#<..
(:0) 1A0| EE CF 26 CD AB 97 D9 16 92 82 0B B1 B0 51 E2 CC nO&M+.Y....10QbL
(:0) 1B0| 2A 43 7A D1 00 4E A6 2E F2 B6 40 0C 3C 2D 68 64 *CzQ.N&.r6#.<-hd
(:0) 1C0| 7F 78 6D B3 2B FF A8 F0 96 E9 CD 3D 43 6E 87 90 .xm3+.(p.iM=Cn..
(:0) 1D0| 14 E4 F7 8D E5 0C 94 97 F9 88 14 76 D3 02 8D 37 .dw.e...y..vS..7
(:0) 1E0| BD FB 14 80 8B C1 15 2B E1 EC 90 AF 04 42 25 15 ={...A.+al./.B%.
(:0) 1F0| CC C6 A9 B6 29 DB A6 F7 2C BC 1E F4 01 1F E1 AF LF)6)[&w,<.t..a/
(:0) 200| 21 3F A2 DD 0C ED E1 AC 76 72 02 B9 5D B7 B4 20 !?"].ma,vr.9]74
(:0) 210| C1 F8 F2 D2 0A EF 23 8B 9A 5B 8A 3C 92 2F C1 9B AxrR.o#..[.<./A.
(:0) 220| 78 33 29 B8 C4 09 E7 42 55 6F A8 E4 83 52 DF AA x3)8D.gBUo(d.R_*
(:0) 230| 7C 75 FC 45 38 52 83 C6 EA F2 C3 10 9B E9 56 4D |u|E8R.FjrC..iVM
(:0) 240| C4 42 6F DE A2 B8 3C F4 AE B8 97 83 18 D5 CC 31 DBo^"8<t.8...UL1
(:0) 250| AA 67 0C 56 E5 03 17 89 CB F0 BC 8F 96 6D 9E 14 *g.Ve...Kp<..m..
(:0) 260| 3C 8A 70 6E 86 76 E8 EE B2 70 4F 14 28 52 D8 79 <.pn.vhn2pO.(RXy
(:0) 270| 3F 24 05 DD 65 9A B2 91 92 1E 7A BC 33 69 31 90 ?$.]e.2...z<3i1.
(:0) 280| 88 53 1D CB 77 ED B9 1E 5C 87 9F 35 92 EE 2A EF .S.Kwm9.\..5.n*o
(:0) 290| 2B 4A CF 56 E9 4C C2 12 43 52 10 89 B5 78 2A AE +JOViLB.CR..5x*.
(:0) 2A0| F4 06 0D E0 64 F0 D1 25 19 69 A9 41 FB 49 F4 D0 t..`dpQ%.i)A{ItP
(:0) 2B0| 4A 65 A9 36 BB 92 80 DA CE DE 0C 9E 18 44 E4 64 Je)6;..ZN^...Ddd
(:0) 2C0| 1B 1F F8 88 28 6B F3 1A 26 1C DC AD C0 C0 A6 05 ..x.(ks.&.\-##&.
(:0) 2D0| D7 8D CE 8C FA 06 5B 5A CC 17 19 67 CE 84 D0 50 W.N.z.[ZL..gN.PP
(:0) 2E0| 6F 9A 14 3C 0A AB 2B 95 1A E5 E2 40 9B 22 0D 6E o..<.++..eb#.".n
(:0) 2F0| 0A 79 E7 A5 48 0B BA D3 83 86 D3 41 60 61 E6 36 .yg%H.:S..SA`af6
(:0) 300| 92 FD A1 AF C2 CB 65 DB 93 82 EB 66 34 61 34 67 .}!/BKe[..kf4a4g
(:0) 310| D9 A0 2D 59 C7 FD 3E 90 7F AA F0 0F 4F 0D 22 78 Y -YG}>..*p.O."x
(:0) 320| D2 75 56 67 67 16 D2 8B F7 71 47 F3 96 2A 2D 78 RuVgg.R.wqGs.*-x
(:0) 330| BA 18 AC AB 4E A6 A1 21 DE A8 36 38 29 1A 85 F0 :.,+N&!!^(68)..p
(:0) 340| 61 3E 2E 07 69 4A 5E 58 F5 02 5B 41 16 12 08 F8 a>..iJ^Xu.[A...x
(:0) 350| 38 82 02 1B 95 77 EF D2 7C 39 28 D7 46 E7 AD B2 8....woR|9(WFg-2
(:0) 360| 46 49 9F 5F 3E F3 5C 4D 72 14 A1 7B F7 8F 97 83 FI._>s\Mr.!{w...
(:0) 370| 2E 9F 4E 5C 94 8F B3 1D 6C 6C AF D5 2D ED 19 59 ..N\..3.ll/U-m.Y
(:0) 380| 58 A5 05 0F 4C 84 4D 4F 89 3D D9 80 A1 9B D1 1E X%..L.MO.=Y.!.Q.
(:0) 390| 7A 14 2B 76 8B 69 41 B4 87 D3 E8 25 00 6A 43 51 z.+v.iA4.Sh%.jCQ
(:0) 3A0| 41 3C 1D 11 F1 C2 D2 56 21 31 A8 0E 58 93 AD E2 A<..qBRV!1(.X.-b
(:0) 3B0| 74 EE 84 E7 98 2F 9A FD 4C D9 67 BA 5E 0C EA F3 tn.g./.}LYg:^.js
(:0) 3C0| 4D A7 E1 43 D9 60 DF C6 FC 28 E4 9A 79 BA 7F B7 M'aCY`_F|(d.y:.7
(:0) 3D0| 5C 97 83 72 CD 8E DE E4 CE AE 98 3C BB 56 8D 8D \..rM.^dN..<;V..
(:0) 3E0| D5 50 33 2D B8 A2 AC 83 86 97 6D E4 89 5C E1 2F UP3-8",...md.\a/
(:0) 3F0| E9 DD B1 93 1F 4D C5 E5 E0 A1 FA E6 64 1E 85 85 i]1..MEe`!zfd...
(:0) 400| 8D B9 69 7C 4B 3F 4A 72 F0 81 27 05 D7 91 22 33 .9i|K?Jrp.'.W."3
(:0) 410| 77 23 D2 4E 81 25 F3 24 7B D9 28 A3 B1 7C B8 E6 w#RN.%s${Y(#1|8f
(:0) 420| CB 41 2F 51 1A 04 1E F6 56 E5 3D A9 30 CE 2C F6 KA/Q...vVe=)0N,v
(:0) 430| 3E 96 75 62 D0 A5 79 41 73 4D D7 58 EB 9C 78 95 >.ubP%yAsMWXk.x.
(:0) 440| 52 DC D3 4E 26 06 39 19 F2 DE 6A 4C 04 6B AC 88 R\SN&.9.r^jL.k,.
(:0) 450| 44 4E C9 CE CA 13 92 82 C8 FC 3A 6A BE 25 ED D1 DNINJ...H|:j>%mQ
(:0) 460| 5C 8D EE 44 9E 82 26 69 FD 98 68 5E 0D 3A E1 A0 \.nD..&i}.h^.:a
(:0) 470| D9 03 45 46 16 E6 A8 66 57 AF 80 E2 FA FB BC E7 Y.EF.f(fW/.bz{<g
(:0) 480| 7A F0 50 29 9B A5 F4 63 79 97 97 44 8D C2 61 41 zpP).%tcy..D.BaA
(:0) 490| B3 AE 2A 2D E8 2A 29 B2 C9 28 3D 6E AE 93 A7 43 3.*-h*)2I(=n..'C
(:0) 4A0| 12 C8 48 0C 5D 5A 70 A1 DB DE 66 3E E9 39 8A CD .HH.]Zp![^f>i9.M
(:0) 4B0| EE 72 52 B2 6F AB C4 3E A4 05 23 91 48 6B 22 17 nrR2o+D>$.#.Hk".
(:0) 4C0| 49 A1 D3 1C D7 D4 25 60 A3 DA 22 2D 88 6B 5E AA I!S.WT%`#Z"-.k^*
(:0) 4D0| B3 2F 89 14 9C BC 1B 66 C2 B3 31 55 D7 F7 89 E6 3/...<.fB31UWw.f
(:0) 4E0| F5 E0 A2 FC 43 D7 95 0D 84 A1 8A DB 59 6A D4 30 u`"|CW...!.[YjT0
(:0) 4F0| 7D 4C 3E 2E 07 89 47 DF 9B 6A 6A A2 2E 6A 5E AE }L>...G_.jj".j^.
(:0) 500| FB A8 FB 38 53 6D 2C A6 05 71 CD D4 93 D6 66 17 {({8Sm,&.qMT.Vf.
(:0) 510| 82 73 9F CF 0E 89 0E D5 DB C4 E0 21 B9 3D 6F 20 .s.O...U[D`!9=o
(:0) 520| C7 DD 2A 45 25 E6 11 23 F9 18 7F 35 E2 57 83 4B G]*E%f.#y..5bW.K
(:0) 530| C4 B4 6A 66 5F C7 FC 1C 6B B1 64 82 62 6A 64 AC D4jf_G|.k1d.bjd,
(:0) 540| 3C 29 88 7F EC 0B DD 0E 51 57 EF 83 8C 55 2F C9 <)..l.].QWo..U/I
(:0) 550| A7 EC E0 6A 13 83 BA FD 23 E7 D0 78 D6 58 AA 29 'l`j..:}#gPxVX*)
(:0) 560| 94 E2 6A F6 F3 31 60 4B 00 E6 6D 35 F7 72 AF 14 .bjvs1`K.fm5wr/.
(:0) 570| F3 F3 86 6A D3 0D E4 13 57 B5 A5 05 35 BE 8D AA ss.jS.d.W5%.5>.*
(:0) 580| 2D 78 A8 26 DA 39 D8 23 46 E6 25 CE 1F 23 92 CB -x(&Z9X#Ff%N.#.K
(:0) 590| 41 BC 43 87 E2 6E 08 8F 86 6A BA 9D 38 A7 D7 9A A<C.bn...j:.8'W.
(:0) 5A0| 7C BC DB 99 CB 41 8D 51 9D ED D1 0D 24 6D E8 8B |<[.KA.Q.mQ.$mh.
(:0) 5B0| 84 C0 48 46 B6 0A D9 F3 A4 E0 8F F2 FD C4 45 A3 .#HF6.Ys$`.r}DE#
(:0) 5C0| AF 33 D0 99 FD 74 FF BF 26 9A 57 83 BA 6D F6 89 /3P.}t.?&.W.:mv.
(:0) 5D0| A6 1F 0F AB 9B CF 8D 8D 91 A7 63 AB 6A F3 A4 20 &..+.O...'c+js$
(:0) 5E0| EF 3D 37 3B BF 52 A1 AF 51 DC CE 39 6E 37 68 1C o=7;?R!/Q\N9n7h.
(:0) 5F0| F2 E9 9A 2F 07 95 76 34 46 F2 8F 7D 85 B3 90 79 ri./..v4Fr.}.3.y
(:0) 600| 7C 8E A8 82 7A 88 E4 E0 39 6D D2 2D 8D 5A 13 96 |.(.z.d`9mR-.Z..
(:0) 610| B6 DF 4C F3 9E 6E FB 0B A0 AE BB 5B DB 6B 09 7F 6_Ls.n{. .;[[k..
(:0) 620| EE E7 AC EB 16 D3 83 6A FD 71 2E FB 7D 0B BA 83 ng,k.S.j}q.{}.:.
(:0) 630| DD D4 89 A2 3A 1C CE 2B 31 78 7A 0A 8A 8C DD CC ]T.":.N+1xz...]L
(:0) 640| CF CE 7B 7E 0D 54 DA BC FB 75 35 74 39 F8 FD 76 ON{~.TZ<{u5t9x}v
(:0) 650| D8 17 FC 82 5F F0 0B 7E C1 2F F8 AF AF 7F 00 2C X.|._p.~A/x//..,
(:0) 660| B0 6E EF BE 03 93 F3 00 00 00 00 49 45 4E 44 AE 0no>..s....IEND.
(:0) 670| 42 60 82 B`.
But when I try to write a file.png with the raw bytes, it doesn't write anything and the created png is blank. Any ideas how can I work around this?
size_t szDecodedLength = 0;
unsigned char * stDecodedQR = base64_decode(encodedPictureBase64,inLenEncodedPicture, &szDecodedLength);
ofstream outPicture("./flash/file.png", std::ios::out | std::ios::binary | std::ios::trunc);
string outString(reinterpret_cast<char*>(stDecodedQR), szDecodedLength);
if( outPicture && !outPicture.is_open() ){
return;
}
outPicture.write( reinterpret_cast<char*> (stDecodedQR), szDecodedLength);
// outPicture << outString; I've also tried creating a string and doing this but result is the same.
outPicture.flush();
outPicture.close();
This code solves the issue, it looks like it might be permission on the device I'm working on. Tested below code in Visual Studio with C++ and it generates the wanted file(picture) correctly, is a QR code that I can scan with my smartphone.
size_t szDecodedLength = 0;
unsigned char* stDecodedQR = base64_decode(encodedPictureBase64, inLenEncodedPicture, &szDecodedLength);
const size_t szArr = szDecodedLength;
std::vector<unsigned char> vDecodedQR;
for (auto i = 0; i < szDecodedLength; i++) {
vDecodedQR.push_back(stDecodedQR[i]);
}
std::ofstream outPicture("mypng.png", std::ios::out | std::ios::binary | std::ios::trunc);
if (outPicture && !outPicture.is_open()) {
std::cerr << " Error creating PNG file" << std::endl;
return;
}
outPicture.write(reinterpret_cast<char*> (stDecodedQR), szDecodedLength);
outPicture.close();

How to de-compress old data compressed with addCRYPT?

since the 2000th years we were using a compression-lib called addCRYPT.dll from a company called littleBIGware for our Windows-apps. Lib and company doesn't exists anymore, but users of our apps created lots of files up to now which are compressed with this lib.
Now for a new version I don't want to use this old lib, but must still be able to decompress the existing data.
I tried Zip, GZip and some others, but with no success.
Does anybody now, how to decompress these data? Here is a hex-dump of the first 64 bytes of some compressed blocks. There is a kind of a pattern visible, but what?
015104: 40 9c 00 00 42 5a 68 39 31 41 59 26 53 59 63 98 00 a8 00 30 ba 70 00 7f ff fa 10 8f fb e0 51 7c 77 db ed 7d bb 92 35 77 63 91 75 89 5d 60 65 91 26 b0 bb 32 ed aa 14 b5 a5 35 8d 34 ae dd c6 fa ...
017640: 40 9c 00 00 42 5a 68 39 31 41 59 26 53 59 2d fc 84 c1 00 49 2e 70 00 7f f8 7a 10 ef fb e0 5a ee 3e 9f 1e da ed a5 68 16 d5 ad ad 6b 3d cf 5e a4 f7 6d d6 d3 6b 47 bb ab 76 39 34 d8 ad bd ec ea ...
015760: 40 9c 00 00 42 5a 68 39 31 41 59 26 53 59 a5 a4 8a 00 00 4a 8b f0 00 7f ff fa 14 cf fb e0 51 dd b6 d5 ad 5e d6 fa 9b b9 bb 1a 77 77 6e dd 54 80 52 aa e5 98 cd 6d ad 92 18 bb 7a cb af 53 b1 da ...
015336: 40 9c 00 00 42 5a 68 39 31 41 59 26 53 59 4f 3c 3e 6f 00 2d cd f0 00 7f ff fe 10 8f fb e0 53 9f 6f 66 cb ec f6 6f 64 a2 da da 61 8d 2a 85 6b 4d 04 80 03 2b 1b 20 16 d9 de cf 6a b3 d5 a0 32 52 ...
016240: 40 9c 00 00 42 5a 68 39 31 41 59 26 53 59 af b2 df 80 00 27 bd f0 00 7f fd fa 10 8f fb e0 57 bd 5e 9f 19 f7 67 02 35 b0 dd ba 5d 67 b3 72 ce cd db 7a eb ba ce bd de f6 af 0c d6 ba d7 57 dc 1c ...

Decoding an RSA PublicKey in C++/Qt

I had to port a Java app to C++/Qt, but I have a problem about the RSA encryption.
Basically my app ask the server for a key and the the server return it like this :
30 81 9f 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 05 00 03
81 8d 00 30 81 89 02 81 81 00 b4 74 05 71 53 79 39 16 29
fc 17 d3 dd 27 c0 84 72 1e 4a 66 b8 43 e8 57 58 f6 13 65
3c a6 85 ff e1 c3 b3 76 11 3e 10 33 00 7d a4 ab 94 d3 86
a2 99 71 d6 c9 99 a6 54 ec 3c 32 de 25 60 61 c3 84 6d 00
32 6a 4e 7c f7 25 38 3c 12 f7 f7 c0 08 e0 8a ca 6f dc 1d
3c 37 bc e9 77 70 11 e7 9e 67 f0 96 d6 df a1 37 d2 8e 83
1c 75 cf 86 ff 39 bb 53 a6 63 f7 ce 99 eb b3 f2 20 39 37
52 41 31 6e 9f 02 03 01 00 01
After that, all i have to do is to encrypt the credentials and sending them to the server. But I can't find any way to use that key.
The Java version look like this
byte[] key;
// Get the key from the server
// ...
//
KeyFactory kf;
EncodedKeySpec eks = new X509EncodedKeySpec(key);
PublicKey localPublicKey = kf.generatePublic(eks);
I've looked both OpenSSL and QSsl libs but I didn't found anything like this.
If anyone had any ideas ...

Example of LUC algorithm with Crypto++

I am looking for an example of LUC algorithm, but I can't find anything. I know that it is in Crypto++, but I don't know C++ too well to use it.
I look for an example of algorithm of LUC...
It kind of depends on what you want to do. You might want to browse luc.h to see some of the things Crypto++ offers for LUC. There's a LUCES, a LUCSS and a LUC_IES. The *ES is encryption scheme, the *SS is a signature scheme, and the *IES is an integrated encryption scheme (which includes a key agreement algorithm and mask function).
Generally speaking, LUC is a public key encryption system. Using it is like using any other public key encryption system offered by Crypto++. That's because all the public key encryption systems inherit from the same classes (more correctly, base interfaces). You can see the design in the comments for file pubkey.h.
$ grep -R LUCES *
...
typedef LUCES<OAEP<SHA> >::Decryptor LUCES_OAEP_SHA_Decryptor;
typedef LUCES<OAEP<SHA> >::Encryptor LUCES_OAEP_SHA_Encryptor;
And that's pretty much all you need, though you may not know it.
Here's the easier problem to solve. How do you perform RSA encryption in Crypto++?
$ grep -R RSAES *
...
typedef RSAES<PKCS1v15>::Decryptor RSAES_PKCS1v15_Decryptor;
typedef RSAES<PKCS1v15>::Encryptor RSAES_PKCS1v15_Encryptor;
typedef RSAES<OAEP<SHA> >::Decryptor RSAES_OAEP_SHA_Decryptor;
typedef RSAES<OAEP<SHA> >::Encryptor RSAES_OAEP_SHA_Encryptor;
If you find an RSAES_PKCS1v15_Decryptor or RSAES_OAEP_SHA_Decryptor example, you just copy/replace with LUCES_OAEP_SHA_Decryptor and it will work just fine. And if you find an RSAES_PKCS1v15_Encryptor or RSAES_OAEP_SHA_Encryptor example, you just copy/replace with LUCES_OAEP_SHA_Encryptor and it will work just fine.
You can find the examples of using RSAES_OAEP_SHA_Encryptor and RSAES_OAEP_SHA_Decryptor on the Crypto++ wiki page for RSA Encryption Schemes. Or you can use the ECIES examples at Elliptic Curve Integrated Encryption Scheme (remember, all the public key systems inherit from the same base interfaces, so they all have the same methods and you use them the same way).
This should get you started. It creates a private key, saves it, then creates a public key, and saves it.
try
{
AutoSeededRandomPool prng;
FileSink fs1("lucs-private.der", true);
FileSink fs2("lucs-public.der", true);
InvertibleLUCFunction params;
params.GenerateRandomWithKeySize(prng, 2048);
LUC::PrivateKey privateKey(params);
privateKey.DEREncode(fs1);
LUCES_OAEP_SHA_Decryptor decryptor(privateKey);
// ...
LUC::PublicKey publicKey(params);
publicKey.DEREncode(fs2);
LUCES_OAEP_SHA_Encryptor encryptor(publicKey);
// ...
}
catch(CryptoPP::Exception& ex)
{
cerr << ex.what() << endl;
}
If you don't want to use InvertibleLUCFunction, the do something like this to generate the key. Note: RSA has an InvertibleRSAFunction.
LUC::PrivateKey privateKey;
privateKey.Initialize(prng, 2048);
...
LUC::PublicKey publicKey(privateKey);
...
An here's yet another way to do it:
FileSink fs1("lucs-private.der", true);
FileSink fs2("lucs-public.der", true);
LUCES_OAEP_SHA_Decryptor decryptor;
decryptor.AccessKey().Initialize(prng, 2048);
decryptor.AccessKey().DEREncode(fs1);
...
LUCES_OAEP_SHA_Encryptor encryptor(decryptor);
encryptor.AccessKey().DEREncode(fs2);
...
And here's a dump of the private key created by the test program:
$ dumpasn1 lucs-private.der
0 662: SEQUENCE {
4 1: INTEGER 0
7 257: INTEGER
: 00 B8 7A CA 6A 61 D9 CF 2F D8 89 5C A4 7D 74 7B
: AC F5 10 4C 3D 95 BF DD 2E F5 4E E5 F4 20 CF CD
: 44 7F C7 27 41 48 6B 83 E0 7C D9 66 16 8D 54 36
: 97 B9 CE 2D 80 A6 F6 E5 25 87 83 6E B9 41 45 DC
: 2A EB EC 4E EC D9 C0 17 B4 E0 04 F0 58 61 60 F8
: 87 18 27 16 58 BA 56 4E DD 9B C8 CD 18 46 28 38
: A2 6A A6 14 36 D0 A6 FF 9C B8 A8 B5 0F 3A 11 B5
: 00 08 44 B3 31 58 AF 01 F8 57 17 E8 FC 68 B2 5F
: [ Another 129 bytes skipped ]
268 1: INTEGER 17
271 129: INTEGER
: 00 C8 DF 47 D0 B2 6F C2 1A E4 B7 E8 3D 12 BB FF
: 04 F7 34 40 A0 0E ED DC F7 24 7B D9 46 EE 10 C4
: D5 E2 9C 93 05 CF 13 53 40 F4 50 EC 1F 6D D7 33
: FF FF 46 42 88 8D FC F4 EE 7F 0C 8B 71 71 51 D2
: 3C 32 E3 9A 11 B7 D8 CF EA 10 B2 07 49 3F 93 CD
: A0 3F 71 A9 23 27 35 1F 6A C9 1D FE CE 24 75 33
: 8F 53 71 B9 0B DE BC 05 93 98 A3 EA 94 8E 04 B1
: 29 A1 4F 4C 82 34 7A 08 3A 0E 07 98 8B 00 30 D7
: 5B
403 129: INTEGER
: 00 EB 1B D0 EF 5C 0F FC FC B7 56 A7 70 8C AA B7
: A6 90 C8 1F AA AD A0 0B 66 E5 33 75 F2 BE 68 35
: 29 2E 57 AC E0 E0 C8 04 A7 C4 13 1D 10 30 8B 50
: 20 17 0C 83 A7 14 4A 7D 25 31 77 50 66 08 36 13
: BE 9D C0 4E F4 44 74 7A BB D2 92 D0 F7 AE 7C EB
: 8E 84 5C 27 61 2C C9 7A D1 D0 C5 A0 13 98 96 E3
: 76 CD B0 E7 E8 7E 4E 0A 2D 00 86 07 57 DB 8A 51
: 1E 59 76 EA 88 44 4D DA F3 D6 AB 75 CB A6 45 F3
: F3
535 128: INTEGER
: 2E 6A AA BA B4 E8 DD 11 2D 31 A4 D5 F7 08 AB E3
: 1A 9A 15 58 AE C8 59 BE C4 75 85 90 6D 5D A4 18
: 39 27 8F FF 1C 9A FD 0F 0C 29 05 98 9C 16 FE 84
: A4 5C 85 15 F7 98 E6 D5 5B 23 CA 2F A2 27 8A 00
: 6E B1 BB 02 6E 93 53 85 30 30 61 F5 1C 49 5D 19
: EF DF CD 6F 11 7C 6D DC AE F6 A2 06 53 BB 7E 03
: C3 E5 4E E9 59 E0 D8 5F C3 28 0E E0 17 5C 63 6E
: 8E A6 18 FC AD A5 9B 08 D1 8B 7B 28 9D E2 CF E2
: }
0 warnings, 0 errors.

implementing MSCHAPv2 algorithm in python

I am trying to write a brute-force dictionary attack on MS-CHAPv2 using python. The two pieces of data that I'm giving to the script are hex representations of the 16-byte Client Challenge Hash and the 24-byte Client Challenge Response. Using a dictionary file, I am generating the Responses using the MS-CHAPv2 algorithm:
Convert the client's password (dictionary entry) to little endian Unicode (16-bit).
Hash the converted password with MD4 --> NT_hash (16 bytes long).
Pad NT_hash with 0's to 21 bytes (add five 0's).
Split NT_hash into three 7-byte chunks ([:14], [14:28], and [28:]).
Add odd-parity values to each byte of each chunk, creating three 8-byte keys.
Encrypt the 16-byte Client Challenge Hash three times with DES, each time with a
different 8-byte key from step 5.
Concatenate the results from step 6 into a response to compare with the original
Client Challenge Response.
The problem I'm having is actually with the first two steps. I have tried several combinations of UTF encoding with various MD4 python implementations with some strange results. What is perplexing me is that in some combinations, I achieve the correct NT hash of some of my passwords, but not others. I'm using the following website to check my hashes:
http://www.arsitech.com/cryptography/windows/password/
Can anyone tell me exactly what hashing function I should be using and/or clarify the inconsistencies I'm discovering? My code (most of it) and example output (using a small dictionary of 11 words) are shown below if that helps. The last entry in the output computes the NT hash for the password "SecREt01", and it matches what this forum post has:
http://www.waraxe.us/ftopic-3900-days0-orderasc-30.html
...but none of the others are right. HELP!
SOURCE CODE
# NOTE: these values came from a user with the password "frog"
user = "user1"
CCH16 = "9c:27:2b:1f:3c:be:53:00"
CCH16 = HexToByte(CCH16)
CCR24 = "e9:cc:b4:9a:08:46:30:8b:4e:44:17:e7:cb:a9:af:4d:85:09:8b:89:4f:8a:18:d6"
CCR24 = CCR24.replace(':', ' ')
CCR24 = CCR24.upper()
print "CCR24 --> ", CCR24 # Client Challenge Response
# Initialize variables for loop
password = ""
password_found = False
# Find password
for guess in dictionary:
print "Guess ---> ", guess
# Create nt_hash for this guess using MD4 hashing algorithm.
# TRYING TO ENCODE guess
uGuess = guess.encode('utf-16le')
print "uGuess --> ", uGuess
byteUGuess = bytes(uGuess)
print "byteUGuess --> ", byteUGuess
hexByteUGuess = ByteToHex(byteUGuess)
print "hexByteUGuess --> ", hexByteUGuess
# VARIOUS MD4 IMPLEMENTATIONS
#nt_hash = MD4.new(hexByteUGuess).hexdigest()
#nt_hash = hashlib.new('md4', hexByteUGuess).hexdigest()
nt_hash = nthash.encrypt(guess)
#nt_hash = hashlib.new('md4', uGuess).hexdigest()
#nt_hash = hmac.new(digest, user.upper().encode('utf-16le')).hexdigest()
print " nt_hash = ", nt_hash
# Split nt_hash into three DES keys.
# Add the parity bits to the DES keys to make them 8-bytes each.
des_key_1 = HexToByte(addParity(nt_hash[0:14]))
des_key_2 = HexToByte(addParity(nt_hash[14:28]))
des_key_3 = HexToByte(addParity(nt_hash[28:] + "0000000000"))
print " des_key_1 = ", ByteToHex(des_key_1)
print " des_key_2 = ", ByteToHex(des_key_2)
print " des_key_3 = ", ByteToHex(des_key_3)
# Create DES encryption objects with keys.
des_1 = DES.new(des_key_1, DES.MODE_ECB)
des_2 = DES.new(des_key_2, DES.MODE_ECB)
des_3 = DES.new(des_key_3, DES.MODE_ECB)
# Calculate 24-byte Client Challenge Response for this guess
# with the DES objects and the 16-byte Client Challenge Hash.
ccr24_part1 = des_1.encrypt(CCH16)
ccr24_part2 = des_2.encrypt(CCH16)
ccr24_part3 = des_3.encrypt(CCH16)
ccr24_guess = ByteToHex(ccr24_part1 + ccr24_part2 + ccr24_part3)
print " ccr24_part1 = ", ByteToHex(ccr24_part1)
print " ccr24_part2 = ", ByteToHex(ccr24_part2)
print " ccr24_part3 = ", ByteToHex(ccr24_part3)
print " ccr24 --> ", ccr24_guess
print "CCR24 -----> ", CCR24, "\n"
# Compare the guess (ccr24_guess) with the original (CCR24).
if ccr24_guess == CCR24:
password_found = True
password = guess
break
# If the password was found, display it.
# Otherwise, display fail message.
if password_found == True:
print "Success - Password found!"
print " - PASSWORD --> ", password
else:
print "Failure - Dictionary exhausted..."
print " - Password not found."
EXAMPLE OUTPUT
CCR24 --> E9 CC B4 9A 08 46 30 8B 4E 44 17 E7 CB A9 AF 4D 85 09 8B 89 4F 8A 18 D6
Guess ---> skate
uGuess --> s k a t e
byteUGuess --> s k a t e
hexByteUGuess --> 73 00 6B 00 61 00 74 00 65 00 0A 00
nt_hash = e4a8e60472df5891903f148b212e2100
des_key_1 = E5 54 38 C1 46 97 7C B0
des_key_2 = 91 C8 0E E3 49 58 85 5D
des_key_3 = 20 80 01 01 01 01 01 01
ccr24_part1 = AB C0 E5 72 51 8B 9C 23
ccr24_part2 = 23 1E 04 09 51 71 8D 35
ccr24_part3 = 8F 17 C3 5A 08 B8 DC 7B
ccr24 --> AB C0 E5 72 51 8B 9C 23 23 1E 04 09 51 71 8D 35 8F 17 C3 5A 08 B8 DC 7B
CCR24 -----> E9 CC B4 9A 08 46 30 8B 4E 44 17 E7 CB A9 AF 4D 85 09 8B 89 4F 8A 18 D6
Guess ---> love
uGuess --> l o v e
byteUGuess --> l o v e
hexByteUGuess --> 6C 00 6F 00 76 00 65 00 0A 00
nt_hash = afe622205005339e583f020ced5596b7
des_key_1 = AE F2 89 45 04 80 15 67
des_key_2 = 9E 2C 0E E0 20 67 B5 AB
des_key_3 = 97 5B C1 01 01 01 01 01
ccr24_part1 = 97 51 2E AB 79 15 FD A6
ccr24_part2 = 5E 05 25 3D 9D 7C 92 D0
ccr24_part3 = 54 0D 1F D8 18 E9 2E E3
ccr24 --> 97 51 2E AB 79 15 FD A6 5E 05 25 3D 9D 7C 92 D0 54 0D 1F D8 18 E9 2E E3
CCR24 -----> E9 CC B4 9A 08 46 30 8B 4E 44 17 E7 CB A9 AF 4D 85 09 8B 89 4F 8A 18 D6
Guess ---> dry
uGuess --> d r y
byteUGuess --> d r y
hexByteUGuess --> 64 00 72 00 79 00 0A 00
nt_hash = 45f15741ac05cfa965f3ee726f38c116
des_key_1 = 45 F8 54 E9 1A 61 16 9E
des_key_2 = A8 B3 7C 7C E6 92 BC 70
des_key_3 = C1 8A 80 01 01 01 01 01
ccr24_part1 = E0 E5 5C D3 5B 6B 0C 0B
ccr24_part2 = 16 18 F9 DE 53 59 AA BA
ccr24_part3 = 17 62 17 A1 93 88 2F 38
ccr24 --> E0 E5 5C D3 5B 6B 0C 0B 16 18 F9 DE 53 59 AA BA 17 62 17 A1 93 88 2F 38
CCR24 -----> E9 CC B4 9A 08 46 30 8B 4E 44 17 E7 CB A9 AF 4D 85 09 8B 89 4F 8A 18 D6
Guess ---> music
uGuess --> m u s i c
byteUGuess --> m u s i c
hexByteUGuess --> 6D 00 75 00 73 00 69 00 63 00 0A 00
nt_hash = bf8704c94ab6b72ba63f9e8d3354c658
des_key_1 = BF C2 C1 98 94 54 DA 6E
des_key_2 = 2A D3 8F F2 E9 68 CD A8
des_key_3 = C7 2C 01 01 01 01 01 01
ccr24_part1 = 42 E1 2F FF D0 26 2F D9
ccr24_part2 = 3A E1 52 CD 98 D8 65 F2
ccr24_part3 = FC 0A E7 CC 99 E0 B2 26
ccr24 --> 42 E1 2F FF D0 26 2F D9 3A E1 52 CD 98 D8 65 F2 FC 0A E7 CC 99 E0 B2 26
CCR24 -----> E9 CC B4 9A 08 46 30 8B 4E 44 17 E7 CB A9 AF 4D 85 09 8B 89 4F 8A 18 D6
Guess ---> german
uGuess --> g e r m a n
byteUGuess --> g e r m a n
hexByteUGuess --> 67 00 65 00 72 00 6D 00 61 00 6E 00 0A 00
nt_hash = 0964cddced56685197ed7271804a903a
des_key_1 = 08 B3 32 BA CE 6B 58 D0
des_key_2 = 51 CB FB AE 26 8C 01 94
des_key_3 = 91 1C 80 01 01 01 01 01
ccr24_part1 = 1A 4C 59 1E 9F 62 F2 EB
ccr24_part2 = BC B5 73 C1 7D A1 F5 3E
ccr24_part3 = 88 09 FB CD 38 EB 27 9F
ccr24 --> 1A 4C 59 1E 9F 62 F2 EB BC B5 73 C1 7D A1 F5 3E 88 09 FB CD 38 EB 27 9F
CCR24 -----> E9 CC B4 9A 08 46 30 8B 4E 44 17 E7 CB A9 AF 4D 85 09 8B 89 4F 8A 18 D6
Guess ---> flag
uGuess --> f l a g
byteUGuess --> f l a g
hexByteUGuess --> 66 00 6C 00 61 00 67 00 0A 00
nt_hash = 98da2ab73404f97e65d877b3253e646d
des_key_1 = 98 6D 8A 57 73 A1 13 F2
des_key_2 = 7F 32 76 0E 7A 98 94 7C
des_key_3 = 64 37 40 01 01 01 01 01
ccr24_part1 = D7 F2 66 36 63 30 CB FE
ccr24_part2 = B3 74 C0 C0 D9 96 71 E8
ccr24_part3 = 61 2B 69 DE B5 7B 71 56
ccr24 --> D7 F2 66 36 63 30 CB FE B3 74 C0 C0 D9 96 71 E8 61 2B 69 DE B5 7B 71 56
CCR24 -----> E9 CC B4 9A 08 46 30 8B 4E 44 17 E7 CB A9 AF 4D 85 09 8B 89 4F 8A 18 D6
Guess ---> frog
uGuess --> f r o g
byteUGuess --> f r o g
hexByteUGuess --> 66 00 72 00 6F 00 67 00 0A 00
nt_hash = 78d1b2ee58428afdb5fff389c40b0b90
des_key_1 = 79 68 6D 5D E5 C2 0B 15
des_key_2 = FD DA 7F FE 38 4F 10 16
des_key_3 = 0B C8 01 01 01 01 01 01
ccr24_part1 = 76 1D 5C 06 E9 9D 3D 69
ccr24_part2 = C0 44 26 C6 C3 7E F9 ED
ccr24_part3 = 95 9D EB 10 8D 67 8D 6E
ccr24 --> 76 1D 5C 06 E9 9D 3D 69 C0 44 26 C6 C3 7E F9 ED 95 9D EB 10 8D 67 8D 6E
CCR24 -----> E9 CC B4 9A 08 46 30 8B 4E 44 17 E7 CB A9 AF 4D 85 09 8B 89 4F 8A 18 D6
Guess ---> horse
uGuess --> h o r s e
byteUGuess --> h o r s e
hexByteUGuess --> 68 00 6F 00 72 00 73 00 65 00 0A 00
nt_hash = ee23c8c25b9443759520c49b9b6a2756
des_key_1 = EF 10 F2 19 25 DC 51 86
des_key_2 = 75 CB 49 19 49 DC 6D D5
des_key_3 = 26 AB 80 01 01 01 01 01
ccr24_part1 = 50 83 2A A8 E0 96 D5 76
ccr24_part2 = 1E 79 0E 0F 46 1C F5 D9
ccr24_part3 = 15 B1 8A EF B5 5E 5B 86
ccr24 --> 50 83 2A A8 E0 96 D5 76 1E 79 0E 0F 46 1C F5 D9 15 B1 8A EF B5 5E 5B 86
CCR24 -----> E9 CC B4 9A 08 46 30 8B 4E 44 17 E7 CB A9 AF 4D 85 09 8B 89 4F 8A 18 D6
Guess ---> word
uGuess --> w o r d
byteUGuess --> w o r d
hexByteUGuess --> 77 00 6F 00 72 00 64 00 0A 00
nt_hash = 3cd0adcea3231e4d6f4e313390e25347
des_key_1 = 3D 68 2A B9 EA 19 8C 3D
des_key_2 = 4C B6 D3 C7 13 9D 43 C4
des_key_3 = 52 A2 C1 01 01 01 01 01
ccr24_part1 = DB 21 74 37 87 A2 C8 B6
ccr24_part2 = 49 E0 04 3A 77 D0 2D A5
ccr24_part3 = 22 C9 DB F7 6D E8 5A C7
ccr24 --> DB 21 74 37 87 A2 C8 B6 49 E0 04 3A 77 D0 2D A5 22 C9 DB F7 6D E8 5A C7
CCR24 -----> E9 CC B4 9A 08 46 30 8B 4E 44 17 E7 CB A9 AF 4D 85 09 8B 89 4F 8A 18 D6
Guess ---> dense
uGuess --> d e n s e
byteUGuess --> d e n s e
hexByteUGuess --> 64 00 65 00 6E 00 73 00 65 00 0A 00
nt_hash = 2e46b43c23ad709512f8b1898608ef02
des_key_1 = 2F 23 AD 86 C2 1C B5 E0
des_key_2 = 94 89 BF 16 19 4C 19 10
des_key_3 = EF 80 80 01 01 01 01 01
ccr24_part1 = CB C1 1E AB DC 27 58 67
ccr24_part2 = 09 25 34 A4 83 D0 C3 68
ccr24_part3 = 56 22 E7 84 45 B2 F3 6D
ccr24 --> CB C1 1E AB DC 27 58 67 09 25 34 A4 83 D0 C3 68 56 22 E7 84 45 B2 F3 6D
CCR24 -----> E9 CC B4 9A 08 46 30 8B 4E 44 17 E7 CB A9 AF 4D 85 09 8B 89 4F 8A 18 D6
Guess ---> SecREt01
uGuess --> S e c R E t 0 1
byteUGuess --> S e c R E t 0 1
hexByteUGuess --> 53 00 65 00 63 00 52 00 45 00 74 00 30 00 31 00
nt_hash = cd06ca7c7e10c99b1d33b7485a2ed808
des_key_1 = CD 83 B3 4F C7 F1 43 92
des_key_2 = 9B 8F 4C 76 75 43 68 5D
des_key_3 = D9 04 01 01 01 01 01 01
ccr24_part1 = E7 0A E5 FE 60 61 E9 ED
ccr24_part2 = E5 BB AF 19 78 E0 C0 A6
ccr24_part3 = 12 04 60 76 E6 1F D1 AB
ccr24 --> E7 0A E5 FE 60 61 E9 ED E5 BB AF 19 78 E0 C0 A6 12 04 60 76 E6 1F D1 AB
CCR24 -----> E9 CC B4 9A 08 46 30 8B 4E 44 17 E7 CB A9 AF 4D 85 09 8B 89 4F 8A 18 D6
Failure - Dictionary exhausted...
- Password not found.