dsm.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <errno.h>
  24. #include <sys/socket.h>
  25. #include <netinet/in.h>
  26. #include <netinet/ip.h>
  27. #include <netinet/udp.h>
  28. #include <arpa/inet.h>
  29. #define NBT_UDP_PORT 138
  30. #define NBT_TCP_PORT 139
  31. #include "bdsm.h"
  32. #include <openssl/md4.h>
  33. #include <openssl/md5.h>
  34. int main(int ac, char **av)
  35. {
  36. struct sockaddr_in addr;
  37. bdsm_context_t *ctx;
  38. ctx = bdsm_context_new();
  39. assert(ctx);
  40. addr.sin_addr.s_addr = netbios_ns_resolve(ctx->ns, av[1], NETBIOS_FILESERVER);
  41. printf("%s's IP address is : %s\n", av[1], inet_ntoa(addr.sin_addr));
  42. //netbios_ns_discover(ctx->ns);
  43. //exit(0);
  44. // netbios_session_t *session;
  45. // session = netbios_session_new(addr.sin_addr.s_addr);
  46. // if (netbios_session_connect(session, "Cerbere"))
  47. // printf("A NetBIOS session with %s has been established\n", av[1]);
  48. // else
  49. // {
  50. // printf("Unable to establish a NetBIOS session with %s\n", av[1]);
  51. // exit(21);
  52. // }
  53. // netbios_session_destroy(session);
  54. smb_session_t *session;
  55. session = smb_session_new();
  56. if (smb_session_connect(session, av[1], addr.sin_addr.s_addr))
  57. printf("Successfully connected to %s\n", av[1]);
  58. else
  59. {
  60. printf("Unable to connect to %s\n", av[1]);
  61. exit(42);
  62. }
  63. if (smb_negotiate(session))
  64. {
  65. fprintf(stderr, "Dialect/Security Mode negotation success.\n");
  66. fprintf(stderr, "Session key is 0x%x\n", session->srv.session_key);
  67. fprintf(stderr, "Challenge key is 0x%lx\n", session->srv.challenge);
  68. }
  69. else
  70. {
  71. printf("Unable to negotiate SMB Dialect\n");
  72. exit(42);
  73. }
  74. if (smb_authenticate(session, av[1], av[2], av[3]))
  75. {
  76. if (session->guest)
  77. printf("Login FAILED but we were logged in as GUEST \n");
  78. else
  79. printf("Successfully logged in as %s\\%s\n", av[1], av[2]);
  80. }
  81. else
  82. {
  83. printf("Authentication FAILURE.\n");
  84. exit(42);
  85. }
  86. smb_tid ipc = smb_tree_connect(session, "\\\\CERBERE\\IPC$");
  87. if (ipc == 0)
  88. {
  89. fprintf(stderr, "Unable to connect to IPC$ share\n");
  90. exit(42);
  91. }
  92. fprintf(stderr, "Connected to IPC$ share\n");
  93. smb_tid test = smb_tree_connect(session, "\\\\CERBERE\\TEST");
  94. if (test)
  95. fprintf(stderr, "Connected to Test share\n");
  96. else
  97. {
  98. fprintf(stderr, "Unable to connect to Test share\n");
  99. exit(42);
  100. }
  101. smb_fd fd = smb_fopen(session, test, "\\test.txt", SMB_MOD_RO);
  102. if (fd)
  103. fprintf(stderr, "Successfully opened \\test.txt: fd = 0x%.8x\n", fd);
  104. else
  105. {
  106. fprintf(stderr, "Unable to open \\test.txt\n");
  107. exit(42);
  108. }
  109. char data[512];
  110. memset(data, 0, 512);
  111. smb_fread(session, fd, data, 512);
  112. fprintf(stderr, "Read from file:\n%s\n", data);
  113. smb_fclose(session, fd);
  114. smb_session_destroy(session);
  115. bdsm_context_destroy(ctx);
  116. return (0);
  117. }