local.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. if getNetwork(next, ipmap.Mask) != ipmap.NetworkAddr.String() {
  88. return
  89. }
  90. ipmap.Current = net.ParseIP(next)
  91. ipmap.Ipv4s = append(ipmap.Ipv4s, net.ParseIP(next))
  92. addressRecurse(ipmap)
  93. }
  94. /*
  95. Get all of the IPv4 addresses in the network that 'addr' belongs to. YOU MUST PASS THE ADDRESS WITH CIDR NOTATION
  96. i.e. '192.168.50.1/24'
  97. :param addr: the ipv4 address to use for subnet discovery
  98. */
  99. func GetNetworkAddresses(addr string) (*IpSubnetMapper, error) {
  100. ipmap := &IpSubnetMapper{Ipv4s: []net.IP{}}
  101. ip, net, err := net.ParseCIDR(addr)
  102. if err != nil {
  103. return nil, err
  104. }
  105. mask, err := strconv.Atoi(strings.Split(addr, "/")[1])
  106. if err != nil {
  107. return nil, err
  108. }
  109. ipmap.NetworkAddr = ip.Mask(net.Mask)
  110. ipmap.Mask = mask
  111. ipmap.Current = ip.Mask(net.Mask)
  112. addressRecurse(ipmap)
  113. return ipmap, nil
  114. }
  115. /*
  116. Turns a set of network mask bits into a valid IPv4 representation
  117. :param ones: number of 1's in the netmask, i.e. 16 == 11111111 11111111 00000000 00000000
  118. :param bits: the number of bits that the mask consists of (need to keep this param for ipv6 support later)
  119. */
  120. func bitsToMask(ones int, bits int) string {
  121. var bitmask []int
  122. for i := 0; i < ones; i++ {
  123. bitmask = append(bitmask, 1)
  124. }
  125. for i := ones; i < bits; i++ {
  126. bitmask = append(bitmask, 0)
  127. }
  128. octets := []string{
  129. strconv.Itoa(base2to10(bitmask[0:8])),
  130. strconv.Itoa(base2to10(bitmask[8:16])),
  131. strconv.Itoa(base2to10(bitmask[16:24])),
  132. strconv.Itoa(base2to10(bitmask[24:32])),
  133. }
  134. return strings.Join(octets, ".")
  135. }
  136. /*
  137. convert a base 2 number (represented as an array) to a base 10 integer
  138. :param bits: the slice of ints split into an array, e.g. '11110000' would be [1 1 1 1 0 0 0 0]
  139. */
  140. func base2to10(bits []int) int {
  141. var sum int
  142. sum = 0
  143. for i := range bits {
  144. bits[i] = bits[i] * powerInt(2, len(bits)-1-i)
  145. }
  146. for i := range bits {
  147. sum = sum + bits[i]
  148. }
  149. return sum
  150. }
  151. /*
  152. Wrapper func for getting the value of x to the power of y, as int opposed to float64
  153. :param x: the base number to operate on
  154. :param y: the exponent
  155. */
  156. func powerInt(x int, y int) int {
  157. return int(math.Pow(float64(x), float64(y)))
  158. }
  159. // Needs cleanup, but this function populatest a data structure that will be used during TUI program startup
  160. func RetrieveLocalAddresses() (TuiSelectionFeed, error) {
  161. var tuidata TuiSelectionFeed
  162. intf, err := net.Interfaces()
  163. if err != nil {
  164. return tuidata, err
  165. }
  166. addrs, err := net.InterfaceAddrs()
  167. if err != nil {
  168. return tuidata, err
  169. }
  170. for x := range addrs {
  171. var mac string
  172. var interfacename string
  173. ip, net, err := net.ParseCIDR(addrs[x].String())
  174. if err != nil {
  175. return tuidata, err
  176. }
  177. for i := range intf {
  178. intfAddrs, err := intf[i].Addrs()
  179. if err != nil {
  180. return tuidata, err
  181. }
  182. for y := range intfAddrs {
  183. if strings.Contains(intfAddrs[y].String(), strings.Split(ip.String(), "/")[0]) {
  184. interfacename = intf[i].Name
  185. mac = intf[i].HardwareAddr.String()
  186. }
  187. }
  188. }
  189. tuidata.Choice = append(tuidata.Choice, PromptEntry{
  190. HostAddress: ip.String(),
  191. NetworkAddress: ip.Mask(net.Mask).String(),
  192. Cidr: strings.Split(addrs[x].String(), "/")[1],
  193. SubnetMask: bitsToMask(net.Mask.Size()),
  194. MacAddress: mac,
  195. InterfaceName: interfacename,
  196. })
  197. }
  198. return tuidata, nil
  199. }