Implement a program could only be launched by

Purpose

Support a program Callee that could only be launched by Caller; so Callee cannot be launched through shell, or other programs.


Solution Idea

1. caller process passing a string value to callee process, using command line

    callee --id idstring

2. caller pass the MD5 string through stdin

3. callee get idstring from command line option

4. callee regenerate the MD5 string for idstring, saved as str1

5. callee get MD5 string from stdin, and saved in str2

6. callee compare str1 and str2 whether they are same.



Caller:

#!/bin/ksh

id=hello
hash0="$(echo -n "${id}" | md5sum | awk '{print $1}')"

./a.out --id ${id} <<EOF
$hash0
EOF

Callee:

#include <time.h>
#include <getopt.h>
#include <openssl/md5.h>

#include <iostream>

static struct option long_options[] = {
    {"help",    no_argument,       0, 'h'},     // help
    {"id",      required_argument, 0, 'i'},     // id
    {0,         0,                 0, 0  }
};

/**
 * generate a MD5 hash value for string str.
 */
std::string str2md5(const std::string & str) {
    unsigned char digest[16];
    char buffer[16 * 2 + 1] = { '\0' };

    MD5_CTX c;
    MD5_Init(&c);
    const char * plain = str.c_str();
    int length = str.length();
    while (length > 0) {
        if (length > 512) {
            MD5_Update(&c, plain, 512);
        } else {
            MD5_Update(&c, plain, length);
        }
        length -= 512;
        plain += 512;
    }

    MD5_Final(digest, &c);

    for (int n = 0; n < 16; ++n) {
        snprintf(&(buffer[n*2]), 16*2, "%02x", (unsigned int)digest[n]);
    }
    
    //std::cout << "MD5[" << str << "]=[" << buffer << "]" << std::endl;
    return buffer;
}

int main(int argc, char * argv[]) {
    std::string id; // input parameter

    int c = 0;
    while (true) {
        int option_index = 0;
        c = getopt_long(argc, argv, "hc:j:n:u:", long_options, &option_index);
        if (c == -1) {
            break;
        }

        switch (c) {
            case 'h':
                printf("Usage ...\n");
                exit(0);
            case 'i':
                id = optarg;
                break;
            default:
                printf("Invalid option: %c\n", c);
                return false;
        }
    }

    /** read 2 hash value from stdin */
    std::string hash0;
    std::cin >> hash0;  // read first line
 
    /** regenerate a hash value, based on time and input value */
    std::string md5string = str2md5(id);

    /** compare the new generated hash value with passing in hash value */
    if (md5string == hash0) {
        std::cout << "id=[" << id << "] is verified with SUCCESS" << std::endl;
    }
    else {
        std::cout << "id=[" << id << "] is verified with FAILURE" << std::endl;
    }

    return 0;
}

Compile and Execute

$ g++ tt.cpp -lcrypto
$ ./caller.ksh 
id=[hello] is verified with SUCCESS


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值