89 lines
1.5 KiB
Go
Raw Normal View History

2022-03-21 17:39:10 -07:00
package randomip
import (
"crypto/rand"
2022-03-21 17:39:10 -07:00
"net"
2022-03-22 15:16:22 +01:00
"github.com/pkg/errors"
iputil "github.com/projectdiscovery/utils/ip"
randutil "github.com/projectdiscovery/utils/rand"
2022-03-21 17:39:10 -07:00
)
2022-03-22 15:16:22 +01:00
const (
maxIterations = 255
)
2022-03-21 17:39:10 -07:00
func GetRandomIPWithCidr(cidrs ...string) (net.IP, error) {
if len(cidrs) == 0 {
return nil, errors.Errorf("must specify at least one cidr")
}
randIdx, err := randutil.IntN(len(cidrs))
if err != nil {
return nil, err
}
cidr := cidrs[randIdx]
2022-03-22 15:16:22 +01:00
if !iputil.IsCIDR(cidr) {
return nil, errors.Errorf("%s is not a valid cidr", cidr)
2022-03-21 17:39:10 -07:00
}
2022-03-22 15:16:22 +01:00
baseIp, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
2022-03-21 17:39:10 -07:00
}
2022-03-22 15:16:22 +01:00
switch {
case ipnet.Mask[len(ipnet.Mask)-1] == 255:
return baseIp, nil
2022-03-22 15:16:22 +01:00
case iputil.IsIPv4(baseIp.String()):
return getRandomIP(ipnet, 4), nil
case iputil.IsIPv6(baseIp.String()):
return getRandomIP(ipnet, 16), nil
default:
return nil, errors.New("invalid base ip")
}
2022-03-21 17:39:10 -07:00
}
2022-03-22 15:16:22 +01:00
func getRandomIP(ipnet *net.IPNet, size int) net.IP {
ip := ipnet.IP
var iteration int
2022-03-21 17:39:10 -07:00
2022-03-22 15:16:22 +01:00
for iteration < maxIterations {
iteration++
ones, _ := ipnet.Mask.Size()
quotient := ones / 8
remainder := ones % 8
var r []byte
switch size {
case 4, 16:
r = make([]byte, size)
default:
return ip
}
2022-03-21 17:39:10 -07:00
Adding support for code templates (#2930) * Adding support for code templates * adding support for python, powershell and echo (test) * removing debug code * introducing command + trivial trust store mechanism * updating tests * adding basic tests * removing deprecated oracle * mod tidy * adding signature proto with debug prints * removing debug code * fixing test * fixing param order * improving test conditional build * disable file+offlinehttp+code with cloud * adding env vars * removing debug code * reorganizing test folders * adding code template test prototype with dummy priv/pub keys * bump go to 1.20 * fixing go version * fixing lint errors * adding fatal on pub-key test failure * switching to ecdsa asn1 * removing unused signature * fixing signature * adding more tests * extending core with engine args + powershell win test * adding unsigned code test * skip template signing in particular test case * improving test coverage * refactoring key names + adding already signed algo * removing debug code * fixing syntax * fixing lint issues * removing test template * fixing dns tests path * output fmt * adding interact * fixing lint issues * adding -sign cli helper * fixing nil pointer + parse inline keys * making rsa default * adding code prot. ref * moving file to correct loc * moving test * Issue 3339 headless fuzz (#3790) * Basic headless fuzzing * Remove debug statements * Add integration tests * Update template * Fix recognize payload value in matcher * Update tempalte * use req.SetURL() --------- Co-authored-by: Tarun Koyalwar <tarun@projectdiscovery.io> * Auto Generate Syntax Docs + JSONSchema [Fri Jun 9 00:23:32 UTC 2023] :robot: * Add headless header and status matchers (#3794) * add headless header and status matchers * rename headers as header * add integration test for header+status * fix typo * add retry to py-interactsh integration test --------- Co-authored-by: Sandeep Singh <sandeep@projectdiscovery.io> Co-authored-by: Shubham Rasal <shubham@projectdiscovery.io> Co-authored-by: Tarun Koyalwar <tarun@projectdiscovery.io> Co-authored-by: GitHub Action <action@github.com> Co-authored-by: Dogan Can Bakir <65292895+dogancanbakir@users.noreply.github.com> Co-authored-by: Tarun Koyalwar <45962551+tarunKoyalwar@users.noreply.github.com>
2023-06-09 17:24:24 +02:00
_, err := rand.Read(r)
if err != nil {
break
}
2022-03-21 17:39:10 -07:00
2022-03-22 15:16:22 +01:00
for i := 0; i <= quotient; i++ {
if i == quotient {
shifted := byte(r[i]) >> remainder
r[i] = ipnet.IP[i] + (^ipnet.IP[i] & shifted)
2022-03-22 15:16:22 +01:00
} else {
r[i] = ipnet.IP[i]
}
2022-03-21 17:39:10 -07:00
}
2022-03-22 15:16:22 +01:00
ip = r
2022-03-21 17:39:10 -07:00
2022-03-22 15:16:22 +01:00
if !ip.Equal(ipnet.IP) {
break
}
2022-03-21 17:39:10 -07:00
}
return ip
}