dsm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //---------------------------------------------------------------------------
  2. // __________________ _________ _____ _____ .__ ._.
  3. // \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
  4. // | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
  5. // | | \| ` \/ / Y \ / | | \ ___/ \|
  6. // |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
  7. // \/ \/ \/ \/ )/ \/ \/ \/
  8. //
  9. // This file is part of libdsm. Copyright © 2014 VideoLabs SAS
  10. //
  11. // Author: Julien 'Lta' BALLET <contact@lta.io>
  12. //
  13. // This program is free software. It comes without any warranty, to the extent
  14. // permitted by applicable law. You can redistribute it and/or modify it under
  15. // the terms of the Do What The Fuck You Want To Public License, Version 2, as
  16. // published by Sam Hocevar. See the COPYING file for more details.
  17. //----------------------------------------------------------------------------
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <assert.h>
  22. #include <unistd.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <netinet/ip.h>
  29. #include <netinet/udp.h>
  30. #include <arpa/inet.h>
  31. #define NBT_UDP_PORT 138
  32. #define NBT_TCP_PORT 139
  33. #include "bdsm.h"
  34. #include "bdsm/smb_trans2.h"
  35. #include <openssl/md4.h>
  36. #include <openssl/md5.h>
  37. int main(int ac, char **av)
  38. {
  39. struct sockaddr_in addr;
  40. bdsm_context_t *ctx;
  41. ctx = bdsm_context_new();
  42. assert(ctx);
  43. addr.sin_addr.s_addr = netbios_ns_resolve(ctx->ns, av[1], NETBIOS_FILESERVER);
  44. printf("%s's IP address is : %s\n", av[1], inet_ntoa(addr.sin_addr));
  45. //netbios_ns_discover(ctx->ns);
  46. //exit(0);
  47. // netbios_session_t *session;
  48. // session = netbios_session_new(addr.sin_addr.s_addr);
  49. // if (netbios_session_connect(session, "Cerbere"))
  50. // printf("A NetBIOS session with %s has been established\n", av[1]);
  51. // else
  52. // {
  53. // printf("Unable to establish a NetBIOS session with %s\n", av[1]);
  54. // exit(21);
  55. // }
  56. // netbios_session_destroy(session);
  57. smb_session_t *session;
  58. session = smb_session_new();
  59. if (smb_session_connect(session, av[1], addr.sin_addr.s_addr))
  60. printf("Successfully connected to %s\n", av[1]);
  61. else
  62. {
  63. printf("Unable to connect to %s\n", av[1]);
  64. exit(42);
  65. }
  66. if (smb_negotiate(session))
  67. {
  68. fprintf(stderr, "Dialect/Security Mode negotation success.\n");
  69. fprintf(stderr, "Session key is 0x%x\n", session->srv.session_key);
  70. fprintf(stderr, "Challenge key is 0x%lx\n", session->srv.challenge);
  71. }
  72. else
  73. {
  74. printf("Unable to negotiate SMB Dialect\n");
  75. exit(42);
  76. }
  77. if (smb_authenticate(session, av[1], av[2], av[3]))
  78. {
  79. if (session->guest)
  80. printf("Login FAILED but we were logged in as GUEST \n");
  81. else
  82. printf("Successfully logged in as %s\\%s\n", av[1], av[2]);
  83. }
  84. else
  85. {
  86. printf("Authentication FAILURE.\n");
  87. exit(42);
  88. }
  89. smb_tid ipc = smb_tree_connect(session, "IPC$");
  90. if (ipc == 0)
  91. {
  92. fprintf(stderr, "Unable to connect to IPC$ share\n");
  93. exit(42);
  94. }
  95. fprintf(stderr, "Connected to IPC$ share\n");
  96. smb_tid test = smb_tree_connect(session, "test");
  97. if (test)
  98. fprintf(stderr, "Connected to Test share\n");
  99. else
  100. {
  101. fprintf(stderr, "Unable to connect to Test share\n");
  102. exit(42);
  103. }
  104. // smb_fd fd = smb_fopen(session, test, "\\BDSM\\test.txt", SMB_MOD_RO);
  105. // if (fd)
  106. // fprintf(stderr, "Successfully opened file: fd = 0x%.8x\n", fd);
  107. // else
  108. // {
  109. // fprintf(stderr, "Unable to open file\n");
  110. // exit(42);
  111. // }
  112. char data[1024];
  113. smb_share_list_t *share_list;
  114. smb_file_t *files;
  115. // smb_fread(session, fd, data, 1024);
  116. // fprintf(stderr, "Read from file:\n%s\n", data);
  117. // smb_fclose(session, fd);
  118. if (!smb_share_list(session, &share_list))
  119. {
  120. fprintf(stderr, "Unable to list share for %s\n", av[1]);
  121. exit(42);
  122. }
  123. else
  124. {
  125. fprintf(stderr, "Share list : \n");
  126. }
  127. fprintf(stderr, "Let's find files at share's root :\n");
  128. files = smb_find(session, test, "\\*");
  129. if (files != NULL)
  130. while(files)
  131. {
  132. fprintf(stderr, "Found a file %s \n", files->name);
  133. files = files->next;
  134. }
  135. else
  136. fprintf(stderr, "Unable to list files\n");
  137. fprintf(stderr, "Query file info for path: %s\n", av[4]);
  138. files = smb_stat(session, test, av[4]);
  139. if (files)
  140. printf("File %s is %d bytes long", av[4], files->size);
  141. smb_session_destroy(session);
  142. bdsm_context_destroy(ctx);
  143. return (0);
  144. }