This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
window_list.cs
142 lines (118 loc) · 4.14 KB
/
window_list.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// A C# implementation of:
// Get-Process | Where-Object {$_.MainWindowTitle -ne ""} | Select-Object MainWindowTitle
// To Compile:
// C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:exe /out:window_list.exe window_list.cs
using System;
using System.Diagnostics;
using System.Collections.Generic;
class WindowList
{
public static void PrintUsage()
{
Console.WriteLine(@"Displays a list of visible windows and their associated process
USAGE:
window_list.exe [/S <remote system>] [/?]");
}
// Source: https://stackoverflow.com/questions/2776673/how-do-i-truncate-a-net-string
private static string Truncate(string str, int maxLength)
{
if (string.IsNullOrEmpty(str)) return str;
return str.Length <= maxLength ? str : str.Substring(0, maxLength - 3) + "...";
}
private static void PrintDivider()
{
Console.WriteLine("\n" + new String('=', 78));
}
private static void PrintHeader()
{
Console.WriteLine("\nProc ID Process Name Window Title");
Console.WriteLine("------- ------------ ------------");
}
private static void PrintProcesses(List<Process> procs)
{
string line = "";
foreach (Process proc in procs)
{
line = proc.Id.ToString().PadRight(11, ' ');
try
{
// Display the executable name
line += Truncate(proc.MainModule.ModuleName, 30).PadRight(34, ' ');
}
catch
{
// Sometimes MainModule fails with "Access Denied", fall back to ProcessName
line += Truncate(proc.ProcessName, 30).PadRight(34, ' ');
}
line += proc.MainWindowTitle;
Console.WriteLine(line);
}
}
public static void Main(string[] args)
{
try
{
string system = ".";
// Parse arguments
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
switch (arg.ToUpper())
{
case "-S":
case "/S":
i++;
try
{
system = args[i].Trim(new Char[] { '\\', ' ' });
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No system specified");
}
break;
case "/?":
PrintUsage();
return;
}
}
if (system != ".")
{
Console.WriteLine("Listing windows on: " + system + "\n");
}
int session;
IDictionary<int, List<Process>> process_map = new SortedDictionary<int, List<Process>>();
// Group processes in a dictionary by session ID
// TODO: Test against remote system
foreach (Process proc in Process.GetProcesses(system))
{
if (proc.MainWindowTitle == "")
{
continue;
}
session = proc.SessionId;
if (!process_map.ContainsKey(session))
{
process_map[session] = new List<Process>();
}
process_map[session].Add(proc);
}
PrintDivider();
foreach (KeyValuePair<int, List<Process>> kvp in process_map)
{
Console.WriteLine("Session ID: " + kvp.Key);
PrintHeader();
PrintProcesses(kvp.Value);
PrintDivider();
}
}
catch (Exception e)
{
Console.Error.WriteLine("[-] ERROR: {0}", e.Message.Trim());
}
finally
{
Console.WriteLine("\nDONE");
}
}
}