Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. /*
  2. Copyright (c) 2016, Nitin Gode
  3. All rights reserved.
  4.  
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of the <organization> nor the
  13. names of its contributors may be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15.  
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  20. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26.  
  27. */
  28. package main
  29.  
  30. import (
  31. "fmt"
  32. "net"
  33. "syscall"
  34. "unsafe"
  35. )
  36.  
  37. /*
  38. #include <stdint.h>
  39. #include <stdlib.h>
  40.  
  41. typedef struct __attribute__((packed))
  42. {
  43. char dest[6];
  44. char sender[6];
  45. uint16_t protocolType;
  46. } EthernetHeader;
  47.  
  48. typedef struct __attribute__((packed))
  49. {
  50. uint16_t hwType;
  51. uint16_t protoType;
  52. char hwLen;
  53. char protocolLen;
  54. uint16_t oper;
  55. char SHA[6];
  56. char SPA[4];
  57. char THA[6];
  58. char TPA[4];
  59. } ArpPacket;
  60.  
  61. typedef struct __attribute__((packed))
  62. {
  63. EthernetHeader eth;
  64. ArpPacket arp;
  65. } EthernetArpPacket;
  66.  
  67. char* FillRequestPacketFields(char* senderMac, char* senderIp)
  68. {
  69. EthernetArpPacket * packet = malloc(sizeof(EthernetArpPacket));
  70. memset(packet, 0, sizeof(EthernetArpPacket));
  71. // Ethernet header
  72. // Dest = Broadcast (ff:ff:ff:ff:ff)
  73. packet->eth.dest[0] = 0xff;
  74. packet->eth.dest[1] = 0xff;
  75. packet->eth.dest[2] = 0xff;
  76. packet->eth.dest[3] = 0xff;
  77. packet->eth.dest[4] = 0xff;
  78. packet->eth.dest[5] = 0xff;
  79.  
  80. packet->eth.sender[0] = strtol(senderMac, NULL, 16); senderMac += 3;
  81. packet->eth.sender[1] = strtol(senderMac, NULL, 16); senderMac += 3;
  82. packet->eth.sender[2] = strtol(senderMac, NULL, 16); senderMac += 3;
  83. packet->eth.sender[3] = strtol(senderMac, NULL, 16); senderMac += 3;
  84. packet->eth.sender[4] = strtol(senderMac, NULL, 16); senderMac += 3;
  85. packet->eth.sender[5] = strtol(senderMac, NULL, 16);
  86.  
  87. packet->eth.protocolType = htons(0x0806); // ARP
  88.  
  89. // ARP Packet fields
  90. packet->arp.hwType = htons(1); // Ethernet
  91. packet->arp.protoType = htons(0x800); //IP;
  92. packet->arp.hwLen = 6;
  93. packet->arp.protocolLen = 4;
  94. packet->arp.oper = htons(2); // response
  95.  
  96. // Sender MAC (same as that in eth header)
  97. memcpy(packet->arp.SHA, packet->eth.sender, 6);
  98.  
  99. // Sender IP
  100. packet->arp.SPA[0] = strtol(senderIp, NULL, 10); senderIp = strchr(senderIp, '.') + 1;
  101. packet->arp.SPA[1] = strtol(senderIp, NULL, 10); senderIp = strchr(senderIp, '.') + 1;
  102. packet->arp.SPA[2] = strtol(senderIp, NULL, 10); senderIp = strchr(senderIp, '.') + 1;
  103. packet->arp.SPA[3] = strtol(senderIp, NULL, 10);
  104.  
  105. // Dest MAC: Same as SHA, as we use an ARP response
  106. memcpy(packet->arp.THA, packet->arp.SHA, 6);
  107.  
  108. // Dest IP: Same as SPA
  109. memcpy(packet->arp.TPA, packet->arp.SPA, 4);
  110.  
  111. return (char*) packet;
  112. }
  113. */
  114. import "C"
  115.  
  116. func main() {
  117. etherArp := new(C.EthernetArpPacket)
  118. size := uint(unsafe.Sizeof(*etherArp))
  119. fmt.Println("Size : ", size)
  120.  
  121. fd, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, syscall.ETH_P_ALL)
  122. if err != nil {
  123. fmt.Println("Error: " + err.Error())
  124. return
  125. }
  126. fmt.Println("Obtained fd ", fd)
  127. defer syscall.Close(fd)
  128.  
  129. // Get Mac address of vboxnet1
  130. interf, err := net.InterfaceByName("vboxnet1")
  131. if err != nil {
  132. fmt.Println("Could not find vboxnet interface")
  133. return
  134. }
  135.  
  136. fmt.Println("Interface hw address: ", interf.HardwareAddr)
  137. fmt.Println("Creating request for IP 10.10.10.2 from IP 10.10.10.1")
  138.  
  139. iface_cstr := C.CString(interf.HardwareAddr.String())
  140. ip_cstr := C.CString("10.10.10.3")
  141.  
  142. packet := C.GoBytes(unsafe.Pointer(C.FillRequestPacketFields(iface_cstr, ip_cstr)) , C.int(size))
  143.  
  144. // Send the packet
  145. var addr syscall.SockaddrLinklayer
  146. addr.Protocol = syscall.ETH_P_ARP
  147. addr.Ifindex = interf.Index
  148. addr.Hatype = syscall.ARPHRD_ETHER
  149.  
  150. err = syscall.Sendto(fd, packet, 0, &addr)
  151.  
  152. if err != nil {
  153. fmt.Println("Error: ", err)
  154. } else {
  155. fmt.Println("Sent packet")
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement