Advertisement
szymski

Untitled

Apr 26th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10.  
  11. namespace RdpBruter
  12. {
  13. class IpRange : IEnumerable<IPAddress>
  14. {
  15. private int _lower;
  16. private int _upper;
  17.  
  18. private IpRange(int lower, int upper)
  19. {
  20. _lower = lower;
  21. _upper = upper;
  22. }
  23.  
  24. public IEnumerator<IPAddress> GetEnumerator()
  25. {
  26. for (int ip = _lower; ip <= _upper; ip++)
  27. {
  28. yield return (IPAddress) typeof(IPAddress)
  29. .GetConstructor(
  30. BindingFlags.NonPublic | BindingFlags.Instance,
  31. null,
  32. new[] {typeof(int)},
  33. null)
  34. .Invoke(new object[] {IPAddress.HostToNetworkOrder(ip)});
  35. }
  36. }
  37.  
  38. IEnumerator IEnumerable.GetEnumerator()
  39. {
  40. return GetEnumerator();
  41. }
  42.  
  43. public static IpRange Parse(string range)
  44. {
  45. Match m;
  46.  
  47. // IP and mask form
  48. if ((m = Regex.Match(range, @"([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})[\\/]([0-9]{1,2})")).Success)
  49. {
  50. var ip = ReverseEndian(IpFromOctets(
  51. byte.Parse(m.Groups[1].Value),
  52. byte.Parse(m.Groups[2].Value),
  53. byte.Parse(m.Groups[3].Value),
  54. byte.Parse(m.Groups[4].Value)));
  55.  
  56. var mask = byte.Parse(m.Groups[5].Value);
  57.  
  58. int upperAddr = (int) (ip | (uint.MaxValue >> mask));
  59.  
  60. return new IpRange(ip, upperAddr);
  61. }
  62.  
  63. // IP to IP form
  64. else if ((m = Regex.Match(range, @"([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\s*-\s*([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})")).Success)
  65. {
  66. var lowerAddr = IpFromOctets(
  67. byte.Parse(m.Groups[1].Value),
  68. byte.Parse(m.Groups[2].Value),
  69. byte.Parse(m.Groups[3].Value),
  70. byte.Parse(m.Groups[4].Value));
  71.  
  72. var upperAddr = IpFromOctets(
  73. byte.Parse(m.Groups[5].Value),
  74. byte.Parse(m.Groups[6].Value),
  75. byte.Parse(m.Groups[7].Value),
  76. byte.Parse(m.Groups[8].Value));
  77.  
  78. return new IpRange(ReverseEndian(lowerAddr), ReverseEndian(upperAddr));
  79. }
  80.  
  81. throw new ArgumentException($"Invalid IP range: {range}");
  82. }
  83.  
  84. private static int IpFromOctets(byte o1, byte o2, byte o3, byte o4)
  85. {
  86. return o1 | (o2 << 8) | (o3 << 16) | (o4 << 24);
  87. }
  88.  
  89. private static int ReverseEndian(int value)
  90. {
  91. var b1 = (value >> 0) & 0xff;
  92. var b2 = (value >> 8) & 0xff;
  93. var b3 = (value >> 16) & 0xff;
  94. var b4 = (value >> 24) & 0xff;
  95.  
  96. return b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0;
  97. }
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement