local.go 6.2 KB

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