Skip to content

Commit

Permalink
Added Readme.md. Added support for CMD commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerardo Grignoli committed Nov 7, 2019
1 parent 8c4bc66 commit 9c23bfb
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 13 deletions.
24 changes: 24 additions & 0 deletions CommandInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace gsudo
{
class CommandInterceptor
{
static readonly HashSet<string> CMD_COMMANDS = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "ASSOC", "ATTRIB", "BREAK", "BCDEDIT", "CACLS", "CALL", "CD", "CHCP", "CHDIR", "CHKDSK", "CHKNTFS", "CLS", "CMD", "COLOR", "COMP", "COMPACT", "CONVERT", "COPY", "DATE", "DEL", "DIR", "DISKPART", "DOSKEY", "DRIVERQUERY", "ECHO", "ENDLOCAL", "ERASE", "EXIT", "FC", "FIND", "FINDSTR", "FOR", "FORMAT", "FSUTIL", "FTYPE", "GOTO", "GPRESULT", "GRAFTABL", "HELP", "ICACLS", "IF", "LABEL", "MD", "MKDIR", "MKLINK", "MODE", "MORE", "MOVE", "OPENFILES", "PATH", "PAUSE", "POPD", "PRINT", "PROMPT", "PUSHD", "RD", "RECOVER", "REM", "REN", "RENAME", "REPLACE", "RMDIR", "ROBOCOPY", "SET", "SETLOCAL", "SC", "SCHTASKS", "SHIFT", "SHUTDOWN", "SORT", "START", "SUBST", "SYSTEMINFO", "TASKLIST", "TASKKILL", "TIME", "TITLE", "TREE", "TYPE", "VER", "VERIFY", "VOL", "XCOPY", "WMIC" };
internal string[] AugmentCommand(string[] args)
{
if (args.Length == 0) return new string[] { Environment.GetEnvironmentVariable("COMSPEC"), "/k" };

if (CMD_COMMANDS.Contains(args[0]))
return new string[]
{ Environment.GetEnvironmentVariable("COMSPEC"), "/c" }
.Concat(args).ToArray();

return args;
}
}
}
1 change: 0 additions & 1 deletion Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ private ConsoleColor GetColor(LogLevel level)
{
if (level <= LogLevel.Debug) return ConsoleColor.DarkGray;
if (level == LogLevel.Info) return ConsoleColor.Gray;
//if (level >= LogLevel.Error)
return ConsoleColor.Red;
}
}
Expand Down
18 changes: 9 additions & 9 deletions ProcessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public async Task Start(string exeName, string arguments, string secret, int tim
pipe.Connect(timeoutMilliseconds);
Settings.Logger.Log("Connected.", LogLevel.Debug);

int cancelAttempts = 0;
// no funca bien.
Console.CancelKeyPress += (sender, e) =>
{
if (cancelAttempts++ <= 3) e.Cancel = true;
var CtrlC_Command = Settings.Encoding.GetBytes("\x3");
pipe.WriteAsync(CtrlC_Command, 0, CtrlC_Command.Length);
};
//// doesnt works.
//int cancelAttempts = 0;
//Console.CancelKeyPress += (sender, e) =>
//{
// if (cancelAttempts++ <= 3) e.Cancel = true;
// var CtrlC_Command = Settings.Encoding.GetBytes("\x3");
// pipe.WriteAsync(CtrlC_Command, 0, CtrlC_Command.Length);
//};

var payload = Newtonsoft.Json.JsonConvert.SerializeObject(new RequestStartInfo()
{
Expand Down Expand Up @@ -54,7 +54,7 @@ public async Task Start(string exeName, string arguments, string secret, int tim
}
if (ExitCode.HasValue)
{
Settings.Logger.Log($"Elevated process exited with code {ExitCode}", LogLevel.Info);
Settings.Logger.Log($"Elevated process exited with code {ExitCode}", LogLevel.Debug);

Environment.Exit(ExitCode.Value);
}
Expand Down
5 changes: 3 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class Program
{
async static Task Main(string[] args)
{
if (args.Length == 0) args = new string[] { Environment.GetEnvironmentVariable("COMSPEC") , "/k" };
args = new CommandInterceptor().AugmentCommand(args);

if (args.Length > 1 && args[0] == "service")
{
// service mode
Expand Down Expand Up @@ -62,7 +63,7 @@ async static Task Main(string[] args)
process.StartInfo = new ProcessStartInfo(exeName, $"service {secret}");
process.StartInfo.UseShellExecute = true;
process.StartInfo.Verb = "runas";
#if !DEBUG
#if !DEBUG || true
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
#endif
process.Start();
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# gsudo - a sudo for Windows

gsudo is a `sudo` for Windows that tries to bring a similar user-experience as *nix sudo.

When you call gsudo for the first time, it launches itself elevated in `service mode`. This will open the Windows UAC pop-up. The requested command is then ran by the elevated service and streamed to the user-level console. The service stays running for 1 minute in case you need to elevate again, and then shutdowns. Calls to gsudo before such time-out, will not show the UAC pop-up.

```gsudo```
Opens an elevated CMD in the current console.

```gsudo [command] [arguments]```
Executes the specified command, elevated, and returns.

![gsudo demo](demo.gif)

## Features

- Elevated commands are shown in the user-level console, as `*nix sudo` does, instead of opening the command in a new window.
- Does not shows the UAC pop-up every time.
- Suport for CMD commands as `*nix sudo` does, like `gsudo copy SomeOrigin SomeDestination` instead of `gsudo cmd /c copy SomeOrigin SomeDestination`

# Known issues

- This project was made in a few hours. It is more of a Proof of concept at this point. Logging, argument parsing, configurability, are in the backlog.
- Windows legacy Console is very limited, which explains some of the issues, and `gsudo` still does not support ConPTY.
- When you spawn an elevated cmd, the `<TAB>` key auto complete doesn't work as expected.
- Elevating git-bash does not work. But Powershell and Cmd does. Under investigation.
2 changes: 1 addition & 1 deletion Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Settings
public static bool SharedService { get; set; } = true;
public static TimeSpan ServerTimeout { get; set; } = TimeSpan.FromMinutes(1);
public static int BufferSize { get; set; } = 1024;
public static LogLevel LogLevel { get; set; } = LogLevel.All;
public static LogLevel LogLevel { get; set; } = LogLevel.Info;

public static readonly Encoding Encoding = System.Text.UnicodeEncoding.UTF8;
internal const string TOKEN_EXITCODE = "<GSUDO-EXITCODE>";
Expand Down
Binary file added demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9c23bfb

Please sign in to comment.