手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

MySQL新特性之mysql_config_editor 加密算法与解密实现

[日期:2016-02-14] 来源:Linux社区  作者:zxszcaijin [字体:大 中 小]

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的密码,使得不怀好意的黑客通过远程访问。该程序的加密,至少能够防止绝大多数人知道明文密码或者防止那些不了解加密策略的人知道密码,从而降低了安全隐患。

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-02/128364.htm

linux
相关资讯       MySQL新特性  mysql_config_editor 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款