mysql_config_editor采用的AES ECB加密。关于AES 的ECB加密通常都是块加密,如果要加密超过块大小的数据,就需要涉及填充和链加密模式,文中提到的ECB就是指链加密模式。这篇文章主要介绍在该工具中该加密技术的使用与实现,并未详细介绍该机密技术的算法与实现细节。
在前一篇文章中,加密的过程如下:
encrypt_and_write_file->encrypt_buffer->my_aes_encrypt
my_aes_encrypt的具体实现如下:
-
int my_aes_encrypt(const char* source, int source_length, char* dest,
-
const char* key, int key_length)
-
{
-
#if defined(HAVE_YASSL)
-
TaoCrypt::AES_ECB_Encryption enc;
-
/* 128 bit block used for padding */
-
uint8 block[MY_AES_BLOCK_SIZE];
-
int num_blocks; /* number of complete blocks */
-
int i;
-
#elif defined(HAVE_OPENSSL)
-
MyCipherCtx ctx;
-
int u_len, f_len;
-
#endif
-
-
/* The real key to be used for encryption */
-
uint8 rkey[AES_KEY_LENGTH / 8];
-
int rc; /* result codes */
-
-
if ((rc= my_aes_create_key(key, key_length, rkey)))
-
return rc;
-
-
#if defined(HAVE_YASSL)
-
enc.SetKey((const TaoCrypt::byte *) rkey, MY_AES_BLOCK_SIZE);
-
-
num_blocks = source_length / MY_AES_BLOCK_SIZE;
-
-
for (i = num_blocks; i > 0; i--) /* Encode complete blocks */
-
{
-
enc.Process((TaoCrypt::byte *) dest, (const TaoCrypt::byte *) source,
-
MY_AES_BLOCK_SIZE);
-
source += MY_AES_BLOCK_SIZE;
-
dest += MY_AES_BLOCK_SIZE;
-
}
-
-
/* Encode the rest. We always have incomplete block */
-
char pad_len = MY_AES_BLOCK_SIZE - (source_length -
-
MY_AES_BLOCK_SIZE * num_blocks);
-
memcpy(block, source, 16 - pad_len);
-
memset(block + MY_AES_BLOCK_SIZE - pad_len, pad_len, pad_len);
-
-
enc.Process((TaoCrypt::byte *) dest, (const TaoCrypt::byte *) block,
-
MY_AES_BLOCK_SIZE);
-
-
return MY_AES_BLOCK_SIZE * (num_blocks + 1);
-
#elif defined(HAVE_OPENSSL)
-
if (! EVP_EncryptInit(&ctx.ctx, EVP_aes_128_ecb(),
-
(const unsigned char *) rkey, NULL))
-
return AES_BAD_DATA; /* Error */
-
if (! EVP_EncryptUpdate(&ctx.ctx, (unsigned char *) dest, &u_len,
-
(unsigned const char *) source, source_length))
-
return AES_BAD_DATA; /* Error */
-
if (! EVP_EncryptFinal(&ctx.ctx, (unsigned char *) dest + u_len, &f_len))
-
return AES_BAD_DATA; /* Error */
-
-
return u_len + f_len;
-
#endif
-
}
上述程序就是mysql的使用AES的机密过程。在加密中,如果mysql定义了自带的AES加密算法,就使用自带的(#define HAVE_YASSL).否则就是用OPENSSL EVP框架的加密算法。
这里介绍OPENSSL EVP加密算法的步骤:
-
int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, ENGINE *impl, const unsigned char *key, const unsigned char *iv)
-
EVP_EncryptInit (初始化)
-
|
-
|
-
V
-
EVP_EncryptUpdate(&ctx,out+len,&outl,in,inl);
-
EVP_EncryptUpdate(这个EVP_EncryptUpdate的实现实际就是将明文按照16 bytes的长度去加密,实现会取得该cipher的块大小(对aes_128来说是16字节)并将block-size的整数倍去加密。如果输入为50字节,则此处仅加密48字节,outl也为48字节。输入in中的最后两字节拷贝到ctx->buf缓存起来。
-
对于inl为block_size整数倍的情形,且ctx->buf并没有以前遗留的数据时则直接加解密操作,省去很多后续工作)
-
|
-
|
-
V
-
EVP_EncryptFinal_ex(&ctx,out+len,&outl);
-
对于如本例所述,第一次除了了48字节余两字节,第二次处理了第一次余下的2字节及46字节,余下了输入100字节中的最后4字节。此处进行处理。如果不支持pading,且还有数据的话就出错,否则,将block_size-待处理字节数个数个字节设置为此个数的值,如block_size=16,数据长度为4,则将后面的12字节设置为16-4=12,补齐为一个分组后加密。对于前面为整分组时,如输入数据为16字节,最后再调用此Final时,不过是对16个0进行加密,此密文不用即可,也根本用不着调一下这Final。
由于我们知道了,在加密后的文件中,KEY是存放在文件头部 offset 4bytes的地方,之后的20bytes 存放的都是key的信息 。所以我们只要读取该key,然后对该key之后的信息一行一行的用该key 调用解密程序就好了。具体的实现如下:
algo_aes_ecb.h
-
#ifndef ALGO_AES_H
-
#define ALGO_AES_H
-
-
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *ciphertext);
-
-
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *plaintext);
-
-
#endif
algo_aes_ecb.c
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <string.h>
-
#include "algo_aes_ecb.h"
-
#include <openssl/evp.h>
-
#include <openssl/aes.h>
-
-
typedef unsigned char uint8;
-
#define AES_KEY_LENGTH 128
-
-
uint8 rkey[AES_KEY_LENGTH / 8];
-
-
void handleErrors(void)
-
{
-
ERR_print_errors_fp(stderr);
-
abort();
-
}
-
-
-
static int my_aes_create_key(const char *key, int key_length, uint8 *rkey)
-
{
-
uint8 *rkey_end= rkey + AES_KEY_LENGTH / 8;
-
uint8 *ptr;
-
const char *sptr;
-
const char *key_end= key + key_length;
-
-
memset(rkey, 0, AES_KEY_LENGTH / 8);
-
-
for (ptr= rkey, sptr= key; sptr < key_end; ptr ++, sptr ++)
-
{
-
if (ptr == rkey_end)
-
ptr= rkey;
-
*ptr ^= (uint8) *sptr;
-
}
-
}
-
-
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,unsigned char *ciphertext)
-
{
-
EVP_CIPHER_CTX *ctx;
-
-
int len;
-
-
int ciphertext_len;
-
my_aes_create_key(key,20,rkey);
-
/* Create and initialise the context */
-
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
-
-
/* Initialise the encryption operation. IMPORTANT - ensure you use a key
-
* and IV size appropriate for your cipher
-
* In this example we are using 128 bit AES (i.e. a 128 bit key).
-
*/
-
-
if(1 != EVP_EncryptInit(ctx, EVP_aes_128_ecb(),(const unsigned char *)rkey,NULL))
-
handleErrors();
-
-
/* Provide the message to be encrypted, and obtain the encrypted output.
-
* EVP_EncryptUpdate can be called multiple times if necessary
-
*
-
*/
-
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, (unsigned const char *)plaintext, plaintext_len))
-
handleErrors();
-
ciphertext_len = len;
-
-
/* Finalise the encryption. Further ciphertext bytes may be written at
-
* * * this stage.
-
* * */
-
if(1 != EVP_EncryptFinal(ctx, ciphertext + len, &len)) handleErrors();
-
ciphertext_len += len;
-
-
/* Clean up */
-
EVP_CIPHER_CTX_free(ctx);
-
-
return ciphertext_len;
-
}
-
-
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *plaintext)
-
{
-
EVP_CIPHER_CTX *ctx;
-
-
int len;
-
-
int plaintext_len;
-
my_aes_create_key(key,20,rkey);
-
-
/* Create and initialise the context */
-
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
-
-
/* Initialise the decryption operation. IMPORTANT - ensure you use a key
-
* size appropriate for your cipher
-
* In this example we are using 128 bit AES (i.e. a 128 bit key). The
-
*/
-
-
if(1 != EVP_DecryptInit(ctx, EVP_aes_128_ecb(),rkey,NULL))
-
handleErrors();
-
-
/* Provide the message to be decrypted, and obtain the plaintext output.
-
* EVP_DecryptUpdate can be called multiple times if necessary
-
*
-
*/
-
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
-
handleErrors();
-
plaintext_len = len;
-
-
/* Finalise the decryption. Further plaintext bytes may be written at
-
* * * this stage.
-
* * */
-
if(1 != EVP_DecryptFinal(ctx, plaintext + len, &len)) handleErrors();
-
plaintext_len += len;
-
-
/* Clean up */
-
EVP_CIPHER_CTX_free(ctx);
-
-
return plaintext_len;
-
}
dynstring.h
-
#ifndef dynstring_h
-
#define dynstring_h
-
-
#include<stdlib.h>
-
typedef struct st_dynamic_string
-
{
-
char *str;
-
size_t length,max_length,alloc_increment;
-
} DYNAMIC_STRING;
-
-
#endif
dynstring.c
-
#include<stdio.h>
-
#include<unistd.h>
-
#include<string.h>
-
#include <stdbool.h>
-
#include "dynstring.h"
-
#define NullS (char *) 0
-
-
-
bool init_dynamic_string(DYNAMIC_STRING *str, const char *init_str,size_t init_alloc, size_t alloc_increment)
-
{
-
-
size_t length;
-
-
if (!alloc_increment)
-
alloc_increment=128;
-
length=1;
-
if (init_str && (length= strlen(init_str)+1) < init_alloc)
-
init_alloc=((length+alloc_increment-1)/alloc_increment)*alloc_increment;
-
if (!init_alloc)
-
init_alloc=alloc_increment;
-
-
if (!(str->str=(char*)malloc(init_alloc))) return false;
-
str->length=length-1;
-
if (init_str)
-
memcpy(str->str,init_str,length);
-
str->max_length=init_alloc;
-
str->alloc_increment=alloc_increment;
-
-
return true;
-
}
-
-
-
bool dynstr_append_mem(DYNAMIC_STRING *str, const char *append,size_t length)
-
{
-
char *new_ptr;
-
if (str->length+length >= str->max_length)
-
{
-
size_t new_length=(str->length+length+str->alloc_increment)/
-
str->alloc_increment;
-
new_length*=str->alloc_increment;
-
-
if (!(new_ptr=(char*) realloc(str->str,new_length)))
-
return true;
-
str->str=new_ptr;
-
str->max_length=new_length;
-
}
-
memcpy(str->str + str->length,append,length);
-
str->length+=length;
-
str->str[str->length]=0;
-
return false;
-
}
-
-
bool dynstr_append(DYNAMIC_STRING *str, const char *append)
-
{
-
return dynstr_append_mem(str,append,(uint) strlen(append));
-
}
-
-
bool dynstr_trunc(DYNAMIC_STRING *str, size_t n)
-
{
-
str->length-=n;
-
str->str[str->length]= '\0';
-
return false;
-
}
-
-
-
char *strcend(register const char *s, register char c)
-
{
-
for (;;)
-
{
-
if (*s == (char) c) return (char*) s;
-
if (!*s++) return (char*) s-1;
-
}
-
}
-
-
-
bool dynstr_realloc(DYNAMIC_STRING *str, size_t additional_size)
-
{
-
-
if (!additional_size) return false;
-
if (str->length + additional_size > str->max_length)
-
{
-
str->max_length=((str->length + additional_size+str->alloc_increment-1)/
-
str->alloc_increment)*str->alloc_increment;
-
if (!(str->str=(char*) realloc(str->str,str->max_length)))
-
return true;
-
}
-
return false;
-
}
-
-
-
void dynstr_free(DYNAMIC_STRING *str)
-
{
-
free(str->str);
-
str->str= NULL;
-
}
decrypt.c
-
#include<stdio.h>
-
#include<string.h>
-
#include<stdlib.h>
-
#include <sys/types.h>
-
#include<unistd.h>
-
#include<fcntl.h>
-
#include "dynstring.h"
-
#include "algo_aes_ecb.h"
-
-
-
#define LOGIN_KEY_LEN 20U
-
#define MY_LINE_MAX 4096
-
#define MAX_CIPHER_STORE_LEN 4U
-
#define FN_REFLEN 256
-
-
#define O_BINARY 0
-
-
typedef unsigned char uchar;
-
#define MY_LOGIN_HEADER_LEN (4 + LOGIN_KEY_LEN)
-
-
-
#define int4store(T,A) do { *((char *)(T))=(char) ((A));\
-
*(((char *)(T))+1)=(char) (((A) >> 8));\
-
*(((char *)(T))+2)=(char) (((A) >> 16));\
-
*(((char *)(T))+3)=(char) (((A) >> 24));\
-
} while(0)
-
-
#define sint4korr(A) (int) (((int) ((uchar) (A)[0])) +\
-
(((int) ((uchar) (A)[1]) << 8)) +\
-
(((int) ((uchar) (A)[2]) << 16)) +\
-
(((int) ((uchar) (A)[3]) << 24)))
-
-
static char my_key[LOGIN_KEY_LEN];
-
static char my_login_file[FN_REFLEN];
-
static int g_fd;
-
const int access_flag= (O_RDWR | O_BINARY);
-
-
static int read_login_key(void)
-
{
-
-
/* Move past the unused buffer. */
-
if (lseek(g_fd, 4, SEEK_SET) != 4)
-
exit(1); /* Error while lseeking. */
-
-
if (read(g_fd, (uchar *)my_key, LOGIN_KEY_LEN)!= LOGIN_KEY_LEN)
-
exit(1);
-
-
}
-
-
static int read_and_decrypt_file(DYNAMIC_STRING *file_buf)
-
{
-
-
char cipher[MY_LINE_MAX], plain[MY_LINE_MAX];
-
uchar len_buf[MAX_CIPHER_STORE_LEN];
-
int cipher_len= 0, dec_len= 0;
-
-
/* Move past key first. */
-
if (lseek(g_fd, MY_LOGIN_HEADER_LEN, SEEK_SET )
-
!= (MY_LOGIN_HEADER_LEN))
-
goto error; /* Error while lseeking. */
-
-
/* First read the length of the cipher. */
-
while (read(g_fd, len_buf, MAX_CIPHER_STORE_LEN) == MAX_CIPHER_STORE_LEN)
-
{
-
cipher_len= sint4korr(len_buf);
-
-
if (cipher_len > MY_LINE_MAX)
-
goto error;
-
-
/* Now read 'cipher_len' bytes from the file. */
-
if ((int) read(g_fd, (uchar *) cipher, cipher_len) == cipher_len)
-
{
-
if ((dec_len= decrypt(cipher, cipher_len, my_key,plain)) < 0)
-
goto error;
-
-
plain[dec_len]= 0;
-
dynstr_append(file_buf, plain);
-
}
-
}
-
-
return 0;
-
-
error:
-
printf("couldn't decrypt the file");
-
return -1;
-
}
-
-
-
int my_default_get_login_file(char *file_name, size_t file_name_size)
-
{
-
size_t rc;
-
-
if (getenv("MYSQL_TEST_LOGIN_FILE"))
-
rc= snprintf(file_name, file_name_size, "%s",
-
getenv("MYSQL_TEST_LOGIN_FILE"));
-
-
else if (getenv("HOME"))
-
rc= snprintf(file_name, file_name_size, "%s/.mylogin.cnf",
-
getenv("HOME"));
-
else
-
{
-
memset(file_name, 0, file_name_size);
-
return 0;
-
}
-
/* Anything <= 0 will be treated as error. */
-
if (rc <= 0)
-
return 0;
-
-
return 1;
-
}
-
-
int main(int argc,char **argv){
-
-
DYNAMIC_STRING file_buf;
-
if(!my_default_get_login_file(my_login_file,sizeof(my_login_file))){
-
printf("logfile file not found \n");
-
goto error;
-
}
-
-
if((g_fd= open(my_login_file, access_flag)) == -1)
-
{
-
printf("couldn't open the file\n");
-
goto error;
-
}
-
init_dynamic_string(&file_buf, "",256, MY_LINE_MAX);
-
-
read_login_key();
-
-
read_and_decrypt_file(&file_buf);
-
printf("%s",file_buf.str);
-
-
error:
-
dynstr_free(&file_buf);
-
}
makefile文件
-
OBJ_DIR = ./obj
-
BIN_DIR = ./bin
-
SRC_DIR = ./
-
OBJS = \
-
$(OBJ_DIR)/algo_aes_ecb.o \
-
$(OBJ_DIR)/dynstring.o \
-
$(OBJ_DIR)/decrypt.o
-
TARGET = decrypt
-
INC_OPT = -I./
-
LNK_OPT = -lssl
-
-
$(BIN_DIR)/$(TARGET) : clean chkobjdir chkbindir $(OBJS)
-
gcc -g -o $@ $(OBJS) $(LNK_OPT)
-
-
$(OBJ_DIR)/algo_aes_ecb.o : algo_aes_ecb.c
-
gcc -g $(INC_OPT) -c -o $@ $<
-
-
$(OBJ_DIR)/decrypt.o : decrypt.c
-
gcc -g $(INC_OPT) -c -o $@ $<
-
-
$(OBJ_DIR)/dynstring.o : dynstring.c
-
gcc -g $(INC_OPT) -c -o $@ $<
-
-
chkobjdir :
-
@if test ! -d $(OBJ_DIR) ; \
-
then \
-
mkdir $(OBJ_DIR) ; \
-
fi
-
-
chkbindir :
-
@if test ! -d $(BIN_DIR) ; \
-
then \
-
mkdir $(BIN_DIR) ; \
-
fi
-
-
clean :
-
rm -rf $(TARGET)
-
rm -rf $(OBJS)
执行该二进制文件,就可以得到加密之后的密码了:
该程序只是解密了默认情况下的加密文件,也就是$HOME/.mylogin.cnf 文件中存放的加密信息。如果文件存放在其他地方,则可以修改代码,指定文件所在位置。
总结:
从整个分析可以看到,mysql_config_editor使用了AES ECB 128bit加密算法,加密简单。而且,加密的秘钥也存放在加密文件开头部分。因此,只要该文件可读,就很容易通过该key来解密。但相比之前的版本,该特性起码能够防止命令行输入密码,这样很容易泄露密码,特别是泄露非localhost的密码,使得不怀好意的黑客通过远程访问。该程序的加密,至少能够防止绝大多数人知道明文密码或者防止那些不了解加密策略的人知道密码,从而降低了安全隐患。
阅读(5889) | 评论(0) | 转发(1) |