local.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. type PromptEntry struct {
  43. HostAddress string
  44. NetworkAddress string
  45. Cidr string
  46. SubnetMask string
  47. InterfaceName string
  48. MacAddress string
  49. }
  50. type TuiSelectionFeed struct {
  51. Choice []PromptEntry
  52. }
  53. /*
  54. Get the next IPv4 address of the address specified in the 'addr' argument,
  55. :param addr: the address to get the next address of
  56. */
  57. func getNextAddr(addr string) string {
  58. parsed, err := netip.ParseAddr(addr)
  59. if err != nil {
  60. log.Fatal("failed while parsing address in getNextAddr() ", err, "\n")
  61. }
  62. return parsed.Next().String()
  63. }
  64. /*
  65. get the network address of the ip address in 'addr' with the subnet mask from 'cidr'
  66. :param addr: the ipv4 address to get the network address of
  67. :param cidr: the CIDR notation of the subbet
  68. */
  69. func getNetwork(addr string, cidr int) string {
  70. addr = fmt.Sprintf("%s/%v", addr, cidr)
  71. ip, net, err := net.ParseCIDR(addr)
  72. if err != nil {
  73. log.Fatal("failed whilst attempting to parse cidr in getNetwork() ", err, "\n")
  74. }
  75. return ip.Mask(net.Mask).String()
  76. }
  77. /*
  78. Recursive function to get all of the IPv4 addresses for each IPv4 network that the host is on
  79. :param ipmap: a pointer to an IpSubnetMapper struct which contains domain details such as
  80. the subnet mask, the original network mask, and the current IP address used in the
  81. recursive function
  82. :param max: This is safety feature to prevent stack overflows, so you can manually set the depth to
  83. call the function
  84. */
  85. func addressRecurse(ipmap *IpSubnetMapper) {
  86. next := getNextAddr(ipmap.Current.String())
  87. nextNet := getNetwork(next, ipmap.Mask)
  88. currentNet := ipmap.NetworkAddr.String()
  89. if nextNet != currentNet {
  90. return
  91. }
  92. ipmap.Current = net.ParseIP(next)
  93. ipmap.Ipv4s = append(ipmap.Ipv4s, net.ParseIP(next))
  94. addressRecurse(ipmap)
  95. }
  96. /*
  97. Get all of the IPv4 addresses in the network that 'addr' belongs to. YOU MUST PASS THE ADDRESS WITH CIDR NOTATION
  98. i.e. '192.168.50.1/24'
  99. :param addr: the ipv4 address to use for subnet discovery
  100. */
  101. func GetNetworkAddresses(addr string) (*IpSubnetMapper, error) {
  102. ipmap := &IpSubnetMapper{Ipv4s: []net.IP{}}
  103. ip, net, err := net.ParseCIDR(addr)
  104. if err != nil {
  105. return nil, err
  106. }
  107. mask, err := strconv.Atoi(strings.Split(addr, "/")[1])
  108. if err != nil {
  109. return nil, err
  110. }
  111. ipmap.NetworkAddr = ip.Mask(net.Mask)
  112. ipmap.Mask = mask
  113. ipmap.Current = ip.Mask(net.Mask)
  114. addressRecurse(ipmap)
  115. return ipmap, nil
  116. }
  117. /*
  118. Turns a set of network mask bits into a valid IPv4 representation
  119. :param ones: number of 1's in the netmask, i.e. 16 == 11111111 11111111 00000000 00000000
  120. :param bits: the number of bits that the mask consists of (need to keep this param for ipv6 support later)
  121. */
  122. func bitsToMask(ones int, bits int) string {
  123. var bitmask []int
  124. for i := 0; i < ones; i++ {
  125. bitmask = append(bitmask, 1)
  126. }
  127. for i := ones; i < bits; i++ {
  128. bitmask = append(bitmask, 0)
  129. }
  130. octets := []string{
  131. strconv.Itoa(base2to10(bitmask[0:8])),
  132. strconv.Itoa(base2to10(bitmask[8:16])),
  133. strconv.Itoa(base2to10(bitmask[16:24])),
  134. strconv.Itoa(base2to10(bitmask[24:32])),
  135. }
  136. return strings.Join(octets, ".")
  137. }
  138. /*
  139. convert a base 2 number (represented as an array) to a base 10 integer
  140. :param bits: the slice of ints split into an array, e.g. '11110000' would be [1 1 1 1 0 0 0 0]
  141. */
  142. func base2to10(bits []int) int {
  143. var sum int
  144. sum = 0
  145. for i := range bits {
  146. bits[i] = bits[i] * powerInt(2, len(bits)-1-i)
  147. }
  148. for i := range bits {
  149. sum = sum + bits[i]
  150. }
  151. return sum
  152. }
  153. /*
  154. Wrapper func for getting the value of x to the power of y, as int opposed to float64
  155. :param x: the base number to operate on
  156. :param y: the exponent
  157. */
  158. func powerInt(x int, y int) int {
  159. return int(math.Pow(float64(x), float64(y)))
  160. }
  161. // Needs cleanup, but this function populatest a data structure that will be used during TUI program startup
  162. func RetrieveLocalAddresses() (TuiSelectionFeed, error) {
  163. var tuidata TuiSelectionFeed
  164. intf, err := net.Interfaces()
  165. if err != nil {
  166. return tuidata, err
  167. }
  168. addrs, err := net.InterfaceAddrs()
  169. if err != nil {
  170. return tuidata, err
  171. }
  172. for x := range addrs {
  173. var mac string
  174. var interfacename string
  175. ip, net, err := net.ParseCIDR(addrs[x].String())
  176. if err != nil {
  177. return tuidata, err
  178. }
  179. for i := range intf {
  180. intfAddrs, err := intf[i].Addrs()
  181. if err != nil {
  182. return tuidata, err
  183. }
  184. for y := range intfAddrs {
  185. if strings.Contains(intfAddrs[y].String(), strings.Split(ip.String(), "/")[0]) {
  186. interfacename = intf[i].Name
  187. mac = intf[i].HardwareAddr.String()
  188. }
  189. }
  190. }
  191. tuidata.Choice = append(tuidata.Choice, PromptEntry{
  192. HostAddress: ip.String(),
  193. NetworkAddress: ip.Mask(net.Mask).String(),
  194. Cidr: strings.Split(addrs[x].String(), "/")[1],
  195. SubnetMask: bitsToMask(net.Mask.Size()),
  196. MacAddress: mac,
  197. InterfaceName: interfacename,
  198. })
  199. }
  200. return tuidata, nil
  201. }