Advertisement
honey_the_codewitch

Environment.CommandLine cracker

Jan 26th, 2024 (edited)
1,070
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. internal static List<KeyValuePair<bool,string>> CrackCommandLine(string commandLine, out string exename, char esc = '\\')
  2. {
  3.     exename = null;
  4.     var result = new List<KeyValuePair<bool,string>>();
  5.     var i = 0;
  6.     var inQuote = false;
  7.     var sb = new StringBuilder();
  8.     while (i < commandLine.Length)
  9.     {
  10.         if (!inQuote)
  11.         {
  12.             if (sb.Length == 0)
  13.             {
  14.                 if (commandLine[i] == '"')
  15.                 {
  16.                     inQuote = true;
  17.                     ++i;
  18.                     continue;
  19.                 }
  20.             }
  21.             var ws = false;
  22.             while (char.IsWhiteSpace(commandLine[i]))
  23.             {
  24.                 ws = true;
  25.                 ++i;
  26.             }
  27.             if (ws)
  28.             {
  29.                 if (sb.Length > 0)
  30.                 {
  31.                     if (exename == null)
  32.                     {
  33.                         exename = sb.ToString();
  34.                     }
  35.                     else
  36.                     {
  37.                         result.Add(new KeyValuePair<bool, string>(false, sb.ToString()));
  38.                     }
  39.                     sb.Clear();
  40.                 }
  41.             }
  42.             else
  43.             {
  44.                 sb.Append(commandLine[i]);
  45.                 ++i;
  46.             }
  47.  
  48.         }
  49.         else
  50.         {
  51.             if (i < commandLine.Length - 1 && commandLine[i] == esc && commandLine[i + 1] == '\"')
  52.             {
  53.                 sb.Append('\"');
  54.                 i += 2;
  55.             }
  56.             else if (commandLine[i] == '\"')
  57.             {
  58.                 if (exename == null)
  59.                 {
  60.                     exename = sb.ToString();
  61.                 }
  62.                 else
  63.                 {
  64.                     result.Add(new KeyValuePair<bool, string>(true, sb.ToString()));
  65.                 }
  66.                 sb.Clear();
  67.                 inQuote = false;
  68.                 ++i;
  69.                 continue;
  70.             }
  71.             sb.Append(commandLine[i]);
  72.             ++i;
  73.         }
  74.     }
  75.     if (sb.Length > 0)
  76.     {
  77.         if (exename == null)
  78.         {
  79.             exename = sb.ToString();
  80.         }
  81.         else
  82.         {
  83.             result.Add(new KeyValuePair<bool, string>(inQuote, sb.ToString()));
  84.         }
  85.     }
  86.     return result;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement