Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.95 KB | None | 0 0
  1. // ssh-connect.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. /*
  5.  * $Id: ssh2.c,v 1.19 2009/04/28 10:35:30 bagder Exp $
  6.  *
  7.  * Sample showing how to do SSH2 connect.
  8.  *
  9.  * The sample code has default values for host name, user name, password
  10.  * and path to copy, but you can specify them on the command line like:
  11.  *
  12.  * "ssh2 host user password [-p|-i|-k]"
  13.  */
  14. #include "stdafx.h"
  15.  
  16. #include "libssh2_config.h"
  17. #include <libssh2.h>
  18. #include <libssh2_sftp.h>
  19.  
  20.  
  21.  
  22. #ifdef HAVE_SYS_SOCKET_H
  23. # include <sys/socket.h>
  24. #endif
  25. #ifdef HAVE_NETINET_IN_H
  26. # include <netinet/in.h>
  27. #endif
  28. # ifdef HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. # ifdef HAVE_ARPA_INET_H
  32. #include <arpa/inet.h>
  33. #endif
  34.  
  35. #include <sys/types.h>
  36. #include <fcntl.h>
  37. #include <errno.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <ctype.h>
  41.  
  42. int main(int argc, char *argv[])
  43. {
  44.     unsigned long hostaddr;
  45.     int sock, i, auth_pw = 0;
  46.     struct sockaddr_in sin;
  47.     const char *fingerprint;
  48.     char *userauthlist;
  49.     LIBSSH2_SESSION *session;
  50.     LIBSSH2_CHANNEL *channel;
  51. #ifdef WIN32
  52.     WSADATA wsadata;
  53.  
  54.     WSAStartup(MAKEWORD(2,0), &wsadata);
  55. #endif
  56.     const char *pubkeyfile="etc/user.pub";
  57.     const char *privkeyfile="etc/user";
  58.     const char *username="buz";
  59.     const char *password="Sup3rP4sS";
  60.     int ec = 1;
  61.  
  62.     (void)argc;
  63.     (void)argv;
  64.  
  65.     if (getenv ("USER"))
  66.       username = getenv ("USER");
  67.  
  68.     if (getenv ("PRIVKEY"))
  69.       privkeyfile = getenv ("PRIVKEY");
  70.  
  71.     if (getenv ("PUBKEY"))
  72.       pubkeyfile = getenv ("PUBKEY");
  73.  
  74.     hostaddr = htonl(0x7F000001);
  75.  
  76.     sock = socket(AF_INET, SOCK_STREAM, 0);
  77. #ifndef WIN32
  78.     fcntl(sock, F_SETFL, 0);
  79. #endif
  80.     sin.sin_family = AF_INET;
  81.     sin.sin_port = htons(22);
  82.     sin.sin_addr.s_addr     = inet_addr("124.150.54.140");
  83.     //sin.sin_addr.s_addr = hostaddr;
  84.     if (connect(sock, (struct sockaddr*)(&sin),
  85.                 sizeof(struct sockaddr_in)) != 0) {
  86.         fprintf(stderr, "[+] failed to connect!\n");
  87.         return -1;
  88.     }
  89.  
  90.     /* Create a session instance and start it up
  91.      * This will trade welcome banners, exchange keys, and setup crypto, compression, and MAC layers
  92.      */
  93.     session = libssh2_session_init();
  94.     if (libssh2_session_startup(session, sock)) {
  95.         fprintf(stderr, "[+] Failure establishing SSH session\n");
  96.         return -1;
  97.     }
  98.  
  99.     printf("[+] Established SSH Connection\n");
  100.     /* At this point we havn't authenticated,
  101.      * The first thing to do is check the hostkey's fingerprint against our known hosts
  102.      * Your app may have it hard coded, may go to a file, may present it to the user, that's your call
  103.      */
  104.     fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  105.     printf("[+] Fingerprint: ");
  106.     for(i = 0; i < 20; i++) {
  107.         printf("%02X ", (unsigned char)fingerprint[i]);
  108.     }
  109.     printf("\n");
  110.  
  111.     /* check what authentication methods are available */
  112.     userauthlist = libssh2_userauth_list(session, username, strlen(username));
  113.     printf("[+] Authentication methods: %s\n", userauthlist);
  114.     if (strstr(userauthlist, "password") != NULL) {
  115.         auth_pw |= 1;
  116.     }
  117.     if (strstr(userauthlist, "keyboard-interactive") != NULL) {
  118.         auth_pw |= 2;
  119.     }
  120.     if (strstr(userauthlist, "publickey") != NULL) {
  121.         auth_pw |= 4;
  122.     }
  123.  
  124.     if (auth_pw & 4) {
  125.         /* Authenticate by public key */
  126.         if (libssh2_userauth_publickey_fromfile(session, username, pubkeyfile, privkeyfile, password)) {
  127.             printf("[+] \tAuthentication by public key failed!\n");
  128.             getchar();
  129.             goto shutdown;
  130.         } else {
  131.             printf("[+] \tAuthentication by public key succeeded.\n");
  132.         }
  133.     } else {
  134.         printf("[+] No supported authentication methods found!\n");
  135.         goto shutdown;
  136.     }
  137.  
  138.     /* Request a shell */
  139.     if (!(channel = libssh2_channel_open_session(session))) {
  140.         fprintf(stderr, "[+] Unable to open a session\n");
  141.         goto shutdown;
  142.     }
  143.  
  144.     /* Some environment variables may be set,
  145.      * It's up to the server which ones it'll allow though
  146.      */
  147.     libssh2_channel_setenv(channel, "FOO", "bar");
  148.  
  149.     /* Request a terminal with 'vanilla' terminal emulation
  150.      * See /etc/termcap for more options
  151.      */
  152.     if (libssh2_channel_request_pty(channel, "vanilla")) {
  153.         fprintf(stderr, "Failed requesting pty\n");
  154.         goto skip_shell;
  155.     }
  156.  
  157.     /* Open a SHELL on that pty */
  158.     if (libssh2_channel_shell(channel)) {
  159.         fprintf(stderr, "Unable to request shell on allocated pty\n");
  160.         goto shutdown;
  161.     }
  162.  
  163.     ec = 0;
  164.  
  165.   skip_shell:
  166.     if (channel) {
  167.         libssh2_channel_free(channel);
  168.         channel = NULL;
  169.     }
  170.  
  171.   shutdown:
  172.  
  173.     libssh2_session_disconnect(session, "Normal Shutdown");
  174.     libssh2_session_free(session);
  175.  
  176. #ifdef WIN32
  177.     Sleep(1000);
  178.     closesocket(sock);
  179. #else
  180.     sleep(1);
  181.     close(sock);
  182. #endif
  183.  
  184.     return ec;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement