#include #include #include #include #include #define LIBSSH_STATIC 1 #include #define BUFFER_SIZE 1024 int verify_knownhost(ssh_session session) { enum ssh_known_hosts_e state; unsigned char *hash = NULL; ssh_key srv_pubkey = NULL; size_t hlen; char *hexa; int rc; rc = ssh_get_server_publickey(session, &srv_pubkey); if (rc < 0) { return -1; } rc = ssh_get_publickey_hash(srv_pubkey, SSH_PUBLICKEY_HASH_SHA1, &hash, &hlen); ssh_key_free(srv_pubkey); if (rc < 0) { return -1; } state = ssh_session_is_known_server(session); switch (state) { case SSH_KNOWN_HOSTS_OK: /* OK */ break; case SSH_KNOWN_HOSTS_CHANGED: fprintf(stderr, "Host key for server changed: it is now:\n"); ssh_print_hash(SSH_PUBLICKEY_HASH_SHA256, hash, hlen); fprintf(stderr, "For security reasons, connection will be stopped\n"); ssh_clean_pubkey_hash(&hash); return -1; case SSH_KNOWN_HOSTS_OTHER: fprintf(stderr, "The host key for this server was not found but an other" "type of key exists.\n"); fprintf(stderr, "An attacker might change the default server key to" "confuse your client into thinking the key does not exist\n"); ssh_clean_pubkey_hash(&hash); return -1; case SSH_KNOWN_HOSTS_NOT_FOUND: fprintf(stderr, "Could not find known host file.\n"); fprintf(stderr, "If you accept the host key here, the file will be" "automatically created.\n"); /* FALL THROUGH to SSH_SERVER_NOT_KNOWN behavior */ case SSH_KNOWN_HOSTS_UNKNOWN: hexa = ssh_get_hexa(hash, hlen); fprintf(stderr, "The server is unknown. Do you trust the host key?\n"); fprintf(stderr, "Public key hash: %s\n", hexa); ssh_string_free_char(hexa); ssh_clean_pubkey_hash(&hash); rc = ssh_session_update_known_hosts(session); if (rc < 0) { fprintf(stderr, "Error %s\n", strerror(errno)); return -1; } break; case SSH_KNOWN_HOSTS_ERROR: fprintf(stderr, "Error %s", ssh_get_error(session)); ssh_clean_pubkey_hash(&hash); return -1; } ssh_clean_pubkey_hash(&hash); return 0; } int show_remote_processes(ssh_session session, char *command) { ssh_channel channel; int rc; char buffer[256]; int nbytes; channel = ssh_channel_new(session); if (channel == NULL) return SSH_ERROR; rc = ssh_channel_open_session(channel); if (rc != SSH_OK) { ssh_channel_free(channel); return rc; } rc = ssh_channel_request_exec(channel, command); if (rc != SSH_OK) { ssh_channel_close(channel); ssh_channel_free(channel); return rc; } nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); while (nbytes > 0) { if (write(1, buffer, nbytes) != (unsigned int)nbytes) { ssh_channel_close(channel); ssh_channel_free(channel); return SSH_ERROR; } nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); } if (nbytes < 0) { ssh_channel_close(channel); ssh_channel_free(channel); return SSH_ERROR; } ssh_channel_send_eof(channel); ssh_channel_close(channel); ssh_channel_free(channel); return SSH_OK; } int authenticate_pubkey(ssh_session session, char *priv_name, char *passphrase) { int rc; ssh_key pkey = NULL; //从文件中个导入密钥 rc = ssh_pki_import_privkey_file(priv_name, passphrase, NULL, NULL, &pkey); if (rc != SSH_OK) { exit(-1); } ssh_userauth_publickey(session, NULL, pkey); ssh_key_free(pkey); return rc; } int rsa_file() { char rsa[8192] = {0}; char temp[] = "./aes/aes -d \"FE67236A61D8D67FD4DEBFD46C9E409B94326781DAE97A7DCD84426711D4D0E078EBA98886342F1E8568828843886DCC38487ACDC9C413D73A4E0C80E6070D054FD5E0DCC0771452FC5E31FCF715EFC62177EABE9014E7F9623B4EA843A93A9020A268F9E8A300BB5D30AD22D52F6EA441222205DAF5560A82F18F28075C035205BF4A47E8E350A957B1F7E90AB63AC2FCCCAC23E1C9C3B50F34B32CDBC7BC8B2155475FBC0B794089C60BFA0EFDD2E096D65DAF31A40E31A5D2C1547A4EA622241EEDD9307CB74BFB3C82E5F48FB1A2CA6D5B223BBB9807B6E54487F4F116D7D9EA2320BFE025ED8B88DBF13F79884C8ACB9161793D59D527579319173EC5A0784398C81435FB0536C048AE656F54422BC41207E0DD3E0B66DB70D725B644915D967D9BA56FC9FE1BF8D628D00D5F6835E48CF9C2B9BCB995EC84D881444F0269272FEE8D4FD667A6C909ADB500FB83E25DB45C9CE75044D2C052E72271665BC37AF036D4A81A889BB8A211B4B9730C0ECC9C70B8B4A07BC4D5A2C274361517DE7C8E87E1AC0A66F3D99F593BE77F1B1343323C92F85B4801957E0F32CAB604252028ACB779E1DCA318A8A9F367F7BF9B4D7CDFEC06D878AF6D4EB4BD7F564BA7134479721CECA232CB0C0FD0F63C6FCC63EA393BA06E13FCF691FC40939B6B332842D8046EBC8049C0971FADACBBE3D28C80FB2F59FFCE1482D050FF3BDB22EF9E67295EB3C1F045EF3CB0F638CE5437E20F8126725E5DF1A867F59407795288B5E69385BDED43E1484886B08BCA06A84938F6BED0CF1326BA149FD3E45E533E3AC48E222025CBD27A33DD6A9BF46F35F601CE8E54DBD6E0ECECE1ECC8D6091F86836C781AC5D21E390FFF2CAAD3FA72497548365E77981C24707B3206E679035D668AE66245EA900FE533E8B935C84D5755ECA88AF396B52C7CBEDB448860D826DD0BEE671F07DE374AE31E71883AF95A45F38066C5BC78C49C783B85395A23627BF202397143F556DE4FB0C39977824834C4716C44435FC5B6286BC14E5D3DFE4C7133A88772509A54C80CCCE45433F7271E6434E37BB20754CA8C9462499BC4DA5E2010B06B05514F5FE7532E873D8918C594D28E2446D074B205589F1A50933EAE1165C48632CC110193D3CA6C6CE001E137A218AA9D09EA918881B5635BCADE6670CD061D7D9302561A5B7D4D752A67FAE30C30FD9D1C7CF83D7700ABD455CA2E190183361F9CF25D963E51CBC601E0FC9D752D45E0A72ACBDEE319927F72D023DB94BEB5DFF2153CC4D6187406A44FF8DEFD7045B3E5DF08A06851CF9B462804F339EB96F7D68BCEE2A3860E5DFD26C2884C121DCF3168185CB261FFE3C6F15343CC4769D0632214CC77EF6313D290D7F42CF317DD90FA6CC98983318863FB2A4C43E231F043CDEEF59C305F06B6AF08683B681CD6C7D7CB6B56EE778ED9FD2790C3D34C3754BD7E8A243D4813542E1DF34D127754F93A205F7889E14D9B21E1760318152E6E67FA608D1610CC1894A127595AF80A939A0FD1A18B5662F79AF3CB0EF0276767DFB062A1BD35CA95912F0B31D5951E0A4245856DDE9F11A782148ACC56F38D866B5505E5DBC2FA1204469AB5D44F996DFEE88F35E47ED8206EA5B537A77C175FAB0A392E6FA8C2BF69D63593D7F78CE72145228BD1CDC1ECC051869DF09CEC01DA31F997250D953745A28C61EB7EB379010518D83F8E1A27A81EA78C84DC7D12F64593D3051A5DA89CCF88580E4CE752F6A528041ADF62268F84F09A5CA7E66F6C4AA3E1FC481813C614BF4F783F5E9AC0846178DA1A7EB24BE1B0620826E34D4AC1C72F820DACBDA40294272093FCA2765DC7C052CCDA64A3C7998E0ADBD118FECC3260529D44D4B10BDA8CE9C9AAD5D15D799B0EB2B4976CBE99B78E4068C1105924D2CCC08FCE95B5963ABDF316D47417D7C73510450386565BDED107F4F278B6CA29D5109C5324CEEFA28A5B84F843932991DE2D1DF828916A097A53B233DF1B82DC084D624BF7F35BF0E2A551EFBCDD0A0B8324A68636B9B72BA68D1A1C6A89B7A736BC82381AE3CD6B4A5CCE0ED35C2B17ADD63BA8C908256AE5297AEC9D9ABD2EBE95A43617383F420AF893752B4B03463AAB3B88E3F835FD19504A615AFC739995D93960987235CA0FBAE7D9626045B2E6382521C4CF01C07AC26E9E1C5CFEAF782AD97E983F3A43F8E90314AC32C60D44AF7CBB1EC74C8BB5139526B2A577BCC2558503842F3AD9A2026D2ED104D462D035AB2CC3802D7136DFC938067F4EFD38ECFDBAE8FE30BC3342D3354628F0F87EDCD5F6A005F69A32A30DA6C6AC2188C3A11790867797D2D03D836B9F6FFD55C590F1AC8DF4A0F63B5201987C9AA05B4EAA91466E4D5454549E184306EB59381CAA20A3B1EFE7EFFC97A98A48E8712A0A98B4E39F050BFF5EFB72917E60DD1992DFC77840F00862FA81D34FBF774AB060B80E0805E63C65CE0B5651256D42A8376FD734BEECE02A58651CC8F567EAD91C0C60690D8C9104205A338B3DAB0F75221BE9AE505CFCB2343F9FD68CC79F96795C8ED06866B32CF12456C30232CDF5D1F8EB253777264EF7D3706E875DF54FC216B2DC90C6653D813E9135288730FC6A9178079D6E89694D9CC4CF828F06AE954224402B1FE2BBBA5A6DECA5344DD11DB77AE49D0BB3DA38FCACC15F8AD1BC5A6212F93F2AD285F5915A4AD642239CDB3240CD622E4DBEE18366BB2CB4604F92D8EFC693C3AB2F90AD84FCFD88CB461C3DF89043E94EFBF08FED2B98C5835CBFC3EDDBA653A33D35BA2E2D08F7ADA87A12FAF76DF84D170BD92A56E62DE8B8B0D1CCAEB4DB4BF7D2FA9F8F23A8443638D721BCCDD31D5396EE2BD5FB3F28BC877BF430908415DB46E3B93AF80016036B9CFA933486B83C19061387478808ABE63FDA4B0B76B3AD50343E540EC4F80400A362627273FC1EE643B6B7736B9338A7F1433780574224674D6F625BEAD125F3AD383EEAA12AFAF0DC812039741034A74423F57F82436542EE1FAA31EB72A98E8D060B992BD5954A42416C5C2A3C826D2F8743D9487DA95DF047B329E7FB125216FA1DE2577DA00998872C8B46FE665A20366EF8C839AA8F549C06917C2C752C72963FA1C619F0ADAB3E1309622E6B5EF01A31EB78380CBC657282604FA98AA9B51596F45B7CD3BEE7CD6E2AAC43224C052911EA564481B3FC4AE054BBF72666447110FA193CE8BB277F6D3D069A0BD7EE7D01075FA7E729F5F6E70E890287B52DC423180081FB7E267AC68EAB94786E73235530BB78A1C4AA5998529820572970063C09D1B15DBD2596D3BFD3DA6AA0CC600D9ADCE8D9EAAE3819B01F482BD852917CEB7C006E6E8FEB057EB0AC9A525A72EA433096C51C279506322F84D76E47612FE2BCD27849F6FE7CEC0DBEA27BEC270A696AB84DA1CCB190A97115447FDBF9362B23E89BCD9960A4436B8324903254E28344DB6376E25D2857CD37BAA99D656293E8F82D3A74DD4026D14B1F8935D581F973BBF5B75170E2655512C6CA0415193B75BE9EEB0F7F9884A7FF11BA473056D556FC7281A9DCD79B3F93515565A3079034C1C5C9DF838AD40897F37D74C090269914F89A60E3DB12CDB79E5CAE0F200310D0C1190047D314857DE44A62AFB1615CFBD25B93E2ADD774937660C9B8231D9D952E728CC0D67A74E76BEEE93189C9BA89EB55D933AD898C77ADF21C7DB40394525F0F3B1DDE711B54228DAA\""; FILE *fd = popen(temp, "r"); if (fd == NULL) perror("popen"); fseek(fd, 0, SEEK_END); long file_size = ftell(fd); rewind(fd); fread(rsa, file_size, 1, fd); rsa[file_size] = '\0'; //printf("%s", rsa); fclose(fd); FILE *rfd = fopen("aixiao.rsa", "w+"); fseek(rfd, 0, SEEK_SET); fwrite(rsa, strlen(rsa), 1, rfd); fclose(rfd); return 0; } void help_information() { puts("\n" "remote command\n" "Author: aixiao@aixiao.me\n" "Usage: [-?hlfpkib]\n" "\n" " Options:\n" " -l : host\n" " -f : port\n" " -p : passwd\n" " -k : private key passwd\n" " -i : private key file\n" " -b : command\n" " -? -h : help information\n"); exit(0); } int main(int argc, char *argv[], char **env) { int rc; int verbosity = SSH_LOG_PROTOCOL; ssh_session my_ssh_session; char host_ip[BUFFER_SIZE]; char password[BUFFER_SIZE]; char *priv_name = "aixiao.rsa"; // 私钥文件名称 char priv_passwd[BUFFER_SIZE]; // 私钥的解密密钥 char command[BUFFER_SIZE]; int host_port = 22; memset(host_ip, 0, BUFFER_SIZE); memset(password, 0, BUFFER_SIZE); memset(priv_passwd, 0, BUFFER_SIZE); memset(command, 0, BUFFER_SIZE); int opt; char optstrs[] = ":l:f:p:k:i:b:h?"; while (-1 != (opt = getopt(argc, argv, optstrs))) { switch (opt) { case 'l': strcpy(host_ip, optarg); break; case 'f': host_port = atoi(optarg); break; case 'p': strcpy(password, optarg); break; case 'k': memset(password, 0, BUFFER_SIZE); strcpy(priv_passwd, optarg); rsa_file(); break; case 'i': priv_name = optarg; break; case 'b': strcpy(command, optarg); break; case ':': printf("\nMissing argument after: -%c\n", optopt); ; case 'h': case '?': help_information(); default: help_information(); } } my_ssh_session = ssh_new(); if (my_ssh_session == NULL) { exit(-1); } ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host_ip); ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &host_port); rc = ssh_connect(my_ssh_session); // Verify the server's identity if (verify_knownhost(my_ssh_session) < 0) { ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(-1); } if (strlen(password) > 1) { // Authenticate ourselves rc = ssh_userauth_password(my_ssh_session, NULL, password); if (rc != SSH_AUTH_SUCCESS) { fprintf(stderr, "Error authenticating with password: %s\n", ssh_get_error(my_ssh_session)); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(-1); } } else { // 私钥认证 authenticate_pubkey(my_ssh_session, priv_name, priv_passwd); } // 传递远程命令 show_remote_processes(my_ssh_session, command); // 从一个会话断开连接(服务器或客户端) ssh_disconnect(my_ssh_session); // 释放已分配的SSH会话句柄 ssh_free(my_ssh_session); system("test -f aixiao.rsa && rm aixiao.rsa"); return 0; }