/// <summary> /// Formats date/time in English / French time format /// </summary> /// <param name="info">TargInfo object</param> /// <param name="timeSpan">Timeframe in hours to grant rights</param> /// <param name="forceEnglish">Force US time format</param> #region FormatDateTime() public static void FormatDateTime(TargInfo info, int timeSpan, bool forceEnglish = false) { DateTime dt; if (timeSpan == 2) { //DevTools = create removal task for 2 minutes, test only. dt = Convert.ToDateTime(info.host.removalTask.HostTime).AddMinutes(timeSpan); } else { dt = Convert.ToDateTime(info.host.removalTask.HostTime).AddHours(timeSpan); } //Subtract time from connection to time rights are granted TimeSpan timediff = DateTime.Now.Subtract(info.local.LocalTime); dt = dt + timediff; // add difference #region Time Formats const string ENGLISH = "MM/dd/yyyy"; //English - DD/MM/YYYY HH:MM const string FRENCH = "yyyy/MM/dd"; //French - YYYY/MM/DD HH:MM const string UNIVERSALTIME = "HH:mm"; //Military/Universal Time #endregion if (forceEnglish) { info.host.removalTask.EndDate = dt.ToString(ENGLISH); } else { info.host.removalTask.EndDate = dt.ToString(info.host.connection.PathAdminLang == TargInfo.English ? ENGLISH : FRENCH); } info.host.removalTask.EndTime = dt.ToString(UNIVERSALTIME); }
public AdTasks(TargInfo t) { targ = t; connection = t.host.connection; local = t.local; user = t.host.user; }
public static void WriteScript(TargInfo t) { string targInfoPath = string.Format( @"\\{0}\c$\windows\system32\targinfo.vbs", t.host.connection.HostIP); StreamWriter writer = new StreamWriter(targInfoPath); #region targinfo.vbs //Writes targinfo.vbs used to collect Host data. //Collected data is written to targinfo.txt writer.Write( @"On Error Resume Next{0}" + @"Dim fso, infopath, localTime, computername, username, n{0}" + @"Set objNetwork = CreateObject(""Wscript.Network""){0}" + @"Set objWMI = GetObject( ""winmgmts:\\.\root\cimv2"" ){0}" + @"Set colItems = objWMI.ExecQuery( ""Select * from Win32_ComputerSystem"" ){0}" + @"For Each objItem in colItems{0}" + @"username = objItem.UserName{0}" + @"username = Replace(username, ""{1}\"", """"){0}" + @"next{0}" + @"if IsEmpty(username) or IsNull(username) then{0}" + @"username = ""NOT FOUND""{0}" + @"End If{0}" + @"n = vbNewLine{0}" + @"computername = objNetwork.ComputerName{0}" + @"Set fso = CreateObject(""Scripting.FileSystemObject""){0}" + @"Set infopath = fso.CreateTextFile(""c:\windows\system32\targinfo.txt"", True){0}" + @"infopath.WriteLine(username & n & computername & n & Now()){0}" + @"infopath.Close", Environment.NewLine, Environment.UserDomainName); #endregion writer.Close(); }
/// <summary> /// Save targ settings. /// </summary> /// <param name="t">TargInfo Object</param> public static void SaveTargSettings(TargInfo t) { StreamWriter writer = new StreamWriter(settingsFile, false); writer.WriteLine(t.local.AutoCopy); writer.WriteLine(t.local.SlowText); writer.Close(); }
private void CreateTargInfoObject() { //Abstract Data Object - shared by most classes. //See TargInfo.cs for more info targ = new TargInfo(this); //Subclasses contained within TargInfo.cs //simplifies fetching data and calling methods connection = targ.host.connection; local = targ.local; user = targ.host.user; }
public DevTools(TargInfo t) { InitializeComponent(); targDev = t; connection = t.host.connection; local = t.local; user = t.host.user; #region commandList List<string> CommandList = new List <string> { "Rights Exist on Host? (LDAP)", "Rights Exist on Host? (WMI)", "List Admins on Host (LDAP)", "List Admins on Host (WMI)", "Get Host Info (SCRIPT)", "Get Host Info (WMI)", "User Exists in AD?", "Add Test Admins" }; comboBox2.DataSource = CommandList; #endregion #region dataGridHostInfo Rows / Cols dataGridHostInfo.Rows.Add("AdminRightsExist"); dataGridHostInfo.Rows.Add("CurrentUser"); dataGridHostInfo.Rows.Add("EndDate"); dataGridHostInfo.Rows.Add("EndTime"); dataGridHostInfo.Rows.Add("HostIP"); dataGridHostInfo.Rows.Add("HostLocale"); dataGridHostInfo.Rows.Add("HostName"); dataGridHostInfo.Rows.Add("HostTime"); dataGridHostInfo.Rows.Add("LocalTime"); dataGridHostInfo.Rows.Add("UserID"); dataGridHostInfo.Rows.Add("UserFullName"); dataGridHostInfo.Rows.Add("UserIDExists"); dataGridHostInfo.Rows.Add("IsWmiActive"); dataGridHostInfo.RowHeadersVisible = false; dataGridHostInfo.ScrollBars = ScrollBars.None; #endregion #region Populate form fields //If IP not blank copy IP from Main Form txtIP.Text = targDev.mainForm.GetTxtHostIP != string.Empty ? targDev.mainForm.GetTxtHostIP : "127.0.0.1"; txtUser.Text = targDev.mainForm.GetTxtUserID != string.Empty ? targDev.mainForm.GetTxtUserID : Environment.UserName; #endregion }
public RemoteTasks(TargInfo t) { targ = t; connection = t.host.connection; local = t.local; user = t.host.user; jobFilePath = string.Format(@"\\{0}\C$\Windows\Tasks\{1}.job", connection.HostIP, user.UserID); targInfoPath = string.Format(@"\\{0}\C$\Windows\System32\targinfo.txt", connection.HostIP); }
/// <summary> /// Queries task scheduler on host machine. /// </summary> /// <param name="targ">TargInfo object</param> /// <param name="displayTask">Output results</param> /// <returns>True if task is found.</returns> /// ***No longer used in Production, test only. public static bool QueryTaskScheduler(TargInfo targ, bool displayTask = false) { //Queries schtasks on remote machine to check for //the scheduled removal task. bool queryResults; //******XP FIX******** //SCHTASKS throws an access denied error //when running a query. string taskPath = string.Format( @"\\{0}\c$\windows\tasks\{1}.job", targ.host.connection.HostIP, targ.host.user.UserID); string xpfixUserPath = string.Format(@"\\{0}\c$\users", targ.host.connection.HostIP); if (!Directory.Exists(xpfixUserPath)) { if (File.Exists(taskPath)) { if (displayTask) { targ.mainForm.WriteToConsole("Task Found ::XP MACHINE::"); } return(true); } } //******************** string argQueryTaskScheduler = string.Format( @"/k schtasks /query /s {0} /tn {1} | findstr ""{1}""", targ.host.connection.HostIP, targ.host.user.UserID); RemoteRegistry(true, targ.host.connection.HostIP); Tools.TimeOut(1500); string output = Tools.RunProcess(argQueryTaskScheduler, true); if (output.Contains(targ.host.user.UserID)) { if (displayTask) { targ.mainForm.WriteToConsole("Task Found: " + output); } queryResults = true; } else { targ.mainForm.WriteToConsole("Task Not Found..."); queryResults = false; } RemoteRegistry(false, targ.host.connection.HostIP); return(queryResults); }
public TargLogs(TargInfo t) { connection = t.host.connection; local = t.local; user = t.host.user; targ = t; //Create log paths/// string errorLogDir = @"C:\Temp\Targ\ErrorLogs"; string rightsLogDir = @"C:\Temp\Targ\Rights"; if (!Directory.Exists(errorLogDir)) { Directory.CreateDirectory(errorLogDir); } if (!Directory.Exists(rightsLogDir)) { Directory.CreateDirectory(rightsLogDir); } ///////////////////// if (!Util.ValidationCheck.isTestEnvironment()) //Production { errorLogPath = string.Format( @"\\" + LOGHOST + @"\c$\Temp\Targ\ErrorLogs\{0} - {1}.txt", Environment.UserName, DateTime.Now.ToString(DateTimeFormat)); rightsLogPath = string.Format( @"\\" + LOGHOST + @"\c$\Temp\Targ\Rights\{0} - {1}.txt", Environment.UserName, DateTime.Now.ToString(DateTimeFormat)); } #region TEST ENVIRONMENT else //Test { errorLogPath = string.Format( @"c:\Temp\Targ\ErrorLogs\{0} - {1}.txt", Environment.UserName, DateTime.Now.ToString(DateTimeFormat)); rightsLogPath = string.Format( @"c:\Temp\Targ\Rights\{0} - {1}.txt", Environment.UserName, DateTime.Now.ToString(DateTimeFormat)); } #endregion }
//***Not implemented in production, test only //After rights granted verifies rights exist on host, //& Scheduled Task exists. public static bool FinalCheck(TargInfo targ, bool displayTask = false) { bool removalTaskExists; RemoteTasks remote; try { remote = new RemoteTasks(targ); remote.GetHostInfo(); AdTasks ad = new AdTasks(targ); ad.CheckLocale(); ad.CheckExistingRights(); } catch (Exception) { MsgBox.Info("Failed to complete Final Check"); } if (QueryTaskScheduler(targ, true)) { removalTaskExists = true; } else { RemoteTasks remote1 = new RemoteTasks(targ); removalTaskExists = remote1.jobFileExists(); } bool adminExists = targ.host.user.AdminRightsExist; targ.mainForm.WriteToConsole(string.Format( "Task Exists: {0}{2}" + "Admin Exists: {1}{2}", removalTaskExists, adminExists, Environment.NewLine)); return(removalTaskExists && adminExists); }
public static void ReadHostInfo(TargInfo t) { string filePath = string.Format( @"\\{0}\c$\windows\system32\targinfo.txt", t.host.connection.HostIP); //Read targinfo.txt from host //Contains: Current User, Host Name, Time if (ValidationCheck.FileExists(filePath)) { StreamReader reader = new StreamReader(filePath); t.host.user.CurrentUser = reader.ReadLine(); t.host.connection.HostName = reader.ReadLine(); t.host.removalTask.HostTime = reader.ReadLine(); //Match local time with host time //Used later to calculate time difference //between connection and granting rights. //see Tools.FormatDateTime() t.local.LocalTime = DateTime.Now; reader.Close(); } }
/// <summary> /// Read targ settings. /// </summary> /// <param name="t">TargInfo Object</param> public static void ReadTargSettings(TargInfo t) { StreamWriter writer; StreamReader reader; if (!File.Exists(settingsFile)) { writer = new StreamWriter(settingsFile); writer.WriteLine("TRUE"); writer.WriteLine("TRUE"); writer.Close(); return; } else { reader = new StreamReader(settingsFile); string autoCopy = reader.ReadLine().ToUpper(); string slowText = reader.ReadLine().ToUpper(); reader.Close(); if (autoCopy != "TRUE") { t.local.AutoCopy = false; } else { t.local.AutoCopy = true; } if (slowText != "TRUE") { t.local.SlowText = false; } else { t.local.SlowText = true; } } }
//Tests WMI Connection to host //Stores result in Targinfo.host.connect.IsWmiActive public static bool TestWmi(TargInfo t) { if (!string.IsNullOrWhiteSpace(t.host.connection.HostIP)) { try { ManagementPath path = new ManagementPath(string.Format( @"\\{0}\root\cimv2", t.host.connection.HostIP)); ManagementScope scope = new ManagementScope(path); scope.Connect(); return(t.host.connection.IsWmiActive = true); } catch (Exception) { return(t.host.connection.IsWmiActive = false); } } t.mainForm.WriteToConsole("::WMI Failed. Using Script Method::\n"); return(t.host.connection.IsWmiActive = false); }