c++/přepis-cli-parametrů/přepis-cli-parametrů.cpp
author František Kučera <franta-hg@frantovo.cz>
Tue, 21 Nov 2017 00:20:27 +0100
changeset 54 2a22f9959ea3
permissions -rw-r--r--
přepis-cli-parametrů: první verze
franta-hg@54
     1
/**
franta-hg@54
     2
 * Přepis CLI parametrů
franta-hg@54
     3
 * Copyright © 2017 František Kučera (frantovo.cz)
franta-hg@54
     4
 *
franta-hg@54
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@54
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@54
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@54
     8
 * (at your option) any later version.
franta-hg@54
     9
 *
franta-hg@54
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@54
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@54
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@54
    13
 * GNU General Public License for more details.
franta-hg@54
    14
 *
franta-hg@54
    15
 * You should have received a copy of the GNU General Public License
franta-hg@54
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@54
    17
 */
franta-hg@54
    18
franta-hg@54
    19
#include <stdlib.h>
franta-hg@54
    20
#include <iostream>
franta-hg@54
    21
#include <wchar.h>
franta-hg@54
    22
#include <locale.h>
franta-hg@54
    23
franta-hg@54
    24
franta-hg@54
    25
using namespace std;
franta-hg@54
    26
franta-hg@54
    27
void cekej() {
franta-hg@54
    28
	wprintf(L"Stiskněte Enter pro pokračování…");
franta-hg@54
    29
	getwchar();
franta-hg@54
    30
}
franta-hg@54
    31
franta-hg@54
    32
void napoveda(const string nazevProgramu) {
franta-hg@54
    33
  wprintf(L"Vypište si parametry jedním z následujících příkazů:\n");
franta-hg@54
    34
  wprintf(L"\tcat /proc/$(pidof %s)/cmdline | xargs -0 -n1 echo\n", nazevProgramu.c_str());
franta-hg@54
    35
  wprintf(L"\tps aux | grep '[ ]%s'\n", nazevProgramu.c_str());
franta-hg@54
    36
}
franta-hg@54
    37
franta-hg@54
    38
/**
franta-hg@54
    39
 * 1) Program vypíše zadané parametry (argumenty příkazového řádku) a zastaví se (čeká na potvrzení uživatelem).
franta-hg@54
    40
 * 2) Uživatel si vypíše proces a jeho parametry v druhém terminálu.
franta-hg@54
    41
 * 3) Program přepíše svoje parametry – např. 'heslo' → 'xxxxx'.
franta-hg@54
    42
 * 4) Uživatel si opět vypíše informace o procesu a vidí, že původní informace (v praxi např. hesla) jsou pryč.
franta-hg@54
    43
 * 5) Program se ukončí.
franta-hg@54
    44
 */
franta-hg@54
    45
int main(int argc, char* argv[]) {
franta-hg@54
    46
franta-hg@54
    47
	setlocale(LC_ALL,"");
franta-hg@54
    48
franta-hg@54
    49
	string nazevProgramu = argv[0];
franta-hg@54
    50
franta-hg@54
    51
	if (argc == 1) { // 1 = jen název programu, ale žádné parametry
franta-hg@54
    52
		wprintf(L"Není, co přepisovat – příště prosím zadejte nějaké parametry – např.\n");
franta-hg@54
    53
		wprintf(L"\t%s aaa bbb ccc\n", nazevProgramu.c_str());
franta-hg@54
    54
		return 1;
franta-hg@54
    55
	}
franta-hg@54
    56
franta-hg@54
    57
	wprintf(L"Přehled zadaných parametrů:\n");
franta-hg@54
    58
	for (int i = 1; i < argc; i++) {
franta-hg@54
    59
		wprintf(L"\t%d = %s\n", i, argv[i]);
franta-hg@54
    60
	}
franta-hg@54
    61
franta-hg@54
    62
	napoveda(nazevProgramu);
franta-hg@54
    63
	cekej();
franta-hg@54
    64
franta-hg@54
    65
	// Nahradíme všechny znaky ve všech parametrech, aby nebylo možné přečíst původní hodnoty (např. hesla).
franta-hg@54
    66
	// Pracujeme zde na úrovni bajtů, takže pokud parametr obsahoval vícebajtové znaky, bude mít po nahrazení více znaků (ale stejně bajtů).
franta-hg@54
    67
	// Stále bude možné zjistit délku původních parametrů (v bajtech).
franta-hg@54
    68
	for (int i = 1; i < argc; i++) {
franta-hg@54
    69
		char * arg = argv[i];
franta-hg@54
    70
		while (*arg) *arg++= 'x';
franta-hg@54
    71
	}
franta-hg@54
    72
	// Pokud bychom začínali od nuly (i = 0), přepsali bychom i samotný název programu!
franta-hg@54
    73
	// Možná už jste ve výpisu procesů někdy viděli názvy, které neodpovídají žádné binárce.
franta-hg@54
    74
	// Program se tak může maskovat – nicméně i tak bude symbolický odkaz v /proc/…/exe ukazovat na jeho binárku.
franta-hg@54
    75
franta-hg@54
    76
	wprintf(L"Parametry byly přepsány!\n");
franta-hg@54
    77
	napoveda(nazevProgramu);
franta-hg@54
    78
	cekej();
franta-hg@54
    79
}