Linux c++ 隐藏 cin 输入

警告
本文最后更新于 2021-11-20,文中内容可能已过时。

通过隐藏实现,可以很好的避免执行命令被监控到。

mymain

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <termios.h>
#include <unistd.h>

using namespace std;

int main()
{
    termios oldt;
    tcgetattr(STDIN_FILENO, &oldt);
    termios newt = oldt;
    newt.c_lflag &= ~ECHO;
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);

    string passwd;
    getline(cin, passwd);

    cout << passwd << endl;
    return 0;
}//main

myssh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <termios.h>

using namespace std;

int main(void)
{
    // -------------------------------------------------------------------
    termios oldt;
    tcgetattr(STDIN_FILENO, &oldt);
    termios newt = oldt;
    newt.c_lflag &= ~ECHO;
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);

    std::string passwd;
    std::cout << "welcome: ";
    getline(cin, passwd);
    std::string answer;
	for (const auto& e : {"************"})
		answer += e;
    if (passwd != answer)
    {
        std::cout << "Bye!" << std::endl;
        return -1;
    }
    std::cout << std::endl;
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    // -------------------------------------------------------------------

	std::string cmd;

    // std::cout << cmd << std::endl;
    system(cmd.c_str());
}

mysync

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <termios.h>

using namespace std;
int main(int argc, char* argv[])
{
    // -------------------------------------------------------------------
    termios oldt;
    tcgetattr(STDIN_FILENO, &oldt);
    termios newt = oldt;
    newt.c_lflag &= ~ECHO;
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);

    std::string passwd;
    std::cout << "welcome: ";
    getline(cin, passwd);
    std::string answer;
	for (const auto& e : {"************"})
		answer += e;
    if (passwd != answer)
    {
        std::cout << "Bye!" << std::endl;
        return -1;
    }
    std::cout << std::endl;
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    // -------------------------------------------------------------------

    std::string srcpath;
    std::cout << "srcpath: ";
    std::cin >> srcpath;

    std::string destpath;
    std::cout << "destpath: ";
    std::cin >> destpath;
    std::cout << std::endl;

    std::string cmd;

    system(cmd.c_str());
}

相关内容

william 支付宝支付宝
william 微信微信
0%