local.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. GNU GENERAL PUBLIC LICENSE
  3. Version 3, 29 June 2007
  4. kyoketsu, a Client-To-Client Network Enumeration System
  5. Copyright (C) 2024 Russell Hrubesky, ChiralWorks Software LLC
  6. Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
  7. Everyone is permitted to copy and distribute verbatim copies
  8. of this license document, but changing it is not allowed.
  9. This program is free software: you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. See the GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package kyoketsu
  21. import (
  22. "fmt"
  23. "log"
  24. "math"
  25. "net"
  26. "net/netip"
  27. "strconv"
  28. "strings"
  29. )
  30. const IPV4_BITLEN = 32
  31. type NetworkInterfaceNotFound struct{ Passed string }
  32. // Implementing error interface
  33. func (n *NetworkInterfaceNotFound) Error() string {
  34. return fmt.Sprintf("Interface: '%s' not found.", n.Passed)
  35. }
  36. type IpSubnetMapper struct {
  37. Ipv4s []net.IP `json:"addresses"`
  38. NetworkAddr net.IP
  39. Current net.IP
  40. Mask int
  41. }
  42. /*
  43. Get the next IPv4 address of the address specified in the 'addr' argument,
  44. :param addr: the address to get the next address of
  45. */
  46. func getNextAddr(addr string) string {
  47. parsed, err := netip.ParseAddr(addr)
  48. if err != nil {
  49. log.Fatal("failed while parsing address in getNextAddr() ", err, "\n")
  50. }
  51. return parsed.Next().String()
  52. }
  53. /*
  54. get the network address of the ip address in 'addr' with the subnet mask from 'cidr'
  55. :param addr: the ipv4 address to get the network address of
  56. :param cidr: the CIDR notation of the subbet
  57. */
  58. func getNetwork(addr string, cidr int) string {
  59. addr = fmt.Sprintf("%s/%v", addr, cidr)
  60. ip, net, err := net.ParseCIDR(addr)
  61. if err != nil {
  62. log.Fatal("failed whilst attempting to parse cidr in getNetwork() ", err, "\n")
  63. }
  64. return ip.Mask(net.Mask).String()
  65. }
  66. /*
  67. Recursive function to get all of the IPv4 addresses for each IPv4 network that the host is on
  68. :param ipmap: a pointer to an IpSubnetMapper struct which contains domain details such as
  69. the subnet mask, the original network mask, and the current IP address used in the
  70. recursive function
  71. :param max: This is safety feature to prevent stack overflows, so you can manually set the depth to
  72. call the function
  73. */
  74. func addressRecurse(ipmap *IpSubnetMapper) {
  75. next := getNextAddr(ipmap.Current.String())
  76. nextNet := getNetwork(next, ipmap.Mask)
  77. currentNet := ipmap.NetworkAddr.String()
  78. if nextNet != currentNet {
  79. return
  80. }
  81. ipmap.Current = net.ParseIP(next)
  82. ipmap.Ipv4s = append(ipmap.Ipv4s, net.ParseIP(next))
  83. addressRecurse(ipmap)
  84. }
  85. /*
  86. Get all of the IPv4 addresses in the network that 'addr' belongs to. YOU MUST PASS THE ADDRESS WITH CIDR NOTATION
  87. i.e. '192.168.50.1/24'
  88. :param addr: the ipv4 address to use for subnet discovery
  89. */
  90. func GetNetworkAddresses(addr string) (*IpSubnetMapper, error) {
  91. ipmap := &IpSubnetMapper{Ipv4s: []net.IP{}}
  92. ip, net, err := net.ParseCIDR(addr)
  93. if err != nil {
  94. return nil, err
  95. }
  96. mask, err := strconv.Atoi(strings.Split(addr, "/")[1])
  97. if err != nil {
  98. return nil, err
  99. }
  100. ipmap.NetworkAddr = ip.Mask(net.Mask)
  101. ipmap.Mask = mask
  102. ipmap.Current = ip.Mask(net.Mask)
  103. addressRecurse(ipmap)
  104. return ipmap, nil
  105. }
  106. type PromptEntry struct {
  107. HostAddress string
  108. NetworkAddress string
  109. Cidr string
  110. SubnetMask string
  111. InterfaceName string
  112. MacAddress string
  113. }
  114. type TuiSelectionFeed struct {
  115. Choice []PromptEntry
  116. }
  117. func matchAddressToMac(ip string, intfs []net.Interface) (*PromptEntry, error) {
  118. for i := range intfs {
  119. intfsAddr, err := intfs[i].Addrs()
  120. if err != nil {
  121. return nil, err
  122. }
  123. for x := range intfsAddr {
  124. fmt.Println(intfsAddr[x].String())
  125. }
  126. }
  127. return nil, nil
  128. }
  129. func bitsToMask(ones int, bits int) string {
  130. var bitmask []int
  131. for i := 0; i < ones; i++ {
  132. bitmask = append(bitmask, 1)
  133. }
  134. for i := ones; i < bits; i++ {
  135. bitmask = append(bitmask, 0)
  136. }
  137. octets := []string{
  138. strconv.Itoa(base2to10(bitmask[0:8])),
  139. strconv.Itoa(base2to10(bitmask[8:16])),
  140. strconv.Itoa(base2to10(bitmask[16:24])),
  141. strconv.Itoa(base2to10(bitmask[24:32])),
  142. }
  143. return strings.Join(octets, ".")
  144. }
  145. /*
  146. convert a base 2 number (represented as an array) to a base 10 integer
  147. :param bits: the slice of ints split into an array, e.g. '11110000' would be [1 1 1 1 0 0 0 0]
  148. */
  149. func base2to10(bits []int) int {
  150. var sum int
  151. sum = 0
  152. for i := range bits {
  153. bits[i] = bits[i] * powerInt(2, len(bits)-1-i)
  154. }
  155. for i := range bits {
  156. sum = sum + bits[i]
  157. }
  158. return sum
  159. }
  160. /*
  161. Wrapper func for getting the value of x to the power of y, as int opposed to float64
  162. :param x: the base number to operate on
  163. :param y: the exponent
  164. */
  165. func powerInt(x int, y int) int {
  166. return int(math.Pow(float64(x), float64(y)))
  167. }
  168. func RetrieveLocalAddresses() (TuiSelectionFeed, error) {
  169. var tuidata TuiSelectionFeed
  170. intf, err := net.Interfaces()
  171. if err != nil {
  172. return tuidata, err
  173. }
  174. addrs, err := net.InterfaceAddrs()
  175. if err != nil {
  176. return tuidata, err
  177. }
  178. for x := range addrs {
  179. var mac string
  180. var interfacename string
  181. ip, net, err := net.ParseCIDR(addrs[x].String())
  182. if err != nil {
  183. return tuidata, err
  184. }
  185. for i := range intf {
  186. intfAddrs, err := intf[i].Addrs()
  187. if err != nil {
  188. return tuidata, err
  189. }
  190. for y := range intfAddrs {
  191. if strings.Contains(intfAddrs[y].String(), strings.Split(ip.String(), "/")[0]) {
  192. interfacename = intf[i].Name
  193. mac = intf[i].HardwareAddr.String()
  194. }
  195. }
  196. }
  197. tuidata.Choice = append(tuidata.Choice, PromptEntry{
  198. HostAddress: ip.String(),
  199. NetworkAddress: ip.Mask(net.Mask).String(),
  200. Cidr: strings.Split(addrs[x].String(), "/")[1],
  201. SubnetMask: bitsToMask(net.Mask.Size()),
  202. MacAddress: mac,
  203. InterfaceName: interfacename,
  204. })
  205. }
  206. return tuidata, nil
  207. }