Skip to content

Commit

Permalink
Utility to timestamp Authenticode signatures of PowerShell or VBScrip…
Browse files Browse the repository at this point in the history
…t files
  • Loading branch information
salsifis committed Jul 19, 2023
1 parent aa02b57 commit de4809c
Show file tree
Hide file tree
Showing 13 changed files with 817 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin/
obj/
.vs/
6 changes: 6 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
30 changes: 30 additions & 0 deletions IAuthenticodeSignatureTraits.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace PowershellScriptTimestamp
{
internal interface IAuthenticodeSignatureTraits
{
/// <summary>
/// Code point sequence that is found before every signature
/// </summary>
string SignatureBeginSequence { get; }

/// <summary>
/// Code point sequence that is found after every signature
/// </summary>
string SignatureEndSequence { get; }

/// <summary>
/// Code point sequence that is found at the beginning of each signature chunk
/// </summary>
string SignatureLineBeginning { get; }

/// <summary>
/// Code point sequence that is found at the end of each signature chunk, including the line terminator
/// </summary>
string SignatureLineEnding { get; }

/// <summary>
/// Number of base64 characters found on each signature chunk.
/// </summary>
int SignatureCharsPerLine { get; }
}
}
File renamed without changes.
58 changes: 58 additions & 0 deletions PowershellScriptTimestamp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{DA9D348A-026B-4217-B507-8D6A11B73979}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>PowershellScriptTimestamp</RootNamespace>
<AssemblyName>PowershellScriptTimestamp</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="IAuthenticodeSignatureTraits.cs" />
<Compile Include="PowershellSignatureTraits.cs" />
<Compile Include="ProcessExecutionResult.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TextFileTimestamp.cs" />
<Compile Include="VbscriptSignatureTraits.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
25 changes: 25 additions & 0 deletions PowershellScriptTimestamp.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33829.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PowershellScriptTimestamp", "PowershellScriptTimestamp.csproj", "{DA9D348A-026B-4217-B507-8D6A11B73979}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DA9D348A-026B-4217-B507-8D6A11B73979}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DA9D348A-026B-4217-B507-8D6A11B73979}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DA9D348A-026B-4217-B507-8D6A11B73979}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DA9D348A-026B-4217-B507-8D6A11B73979}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CA8E06A2-FA4E-4CAC-9DC2-79FCE76F2931}
EndGlobalSection
EndGlobal
30 changes: 30 additions & 0 deletions PowershellSignatureTraits.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace PowershellScriptTimestamp
{
internal class PowershellSignatureTraits : IAuthenticodeSignatureTraits
{
/// <summary>
/// Code point sequence that is found before every signature
/// </summary>
string IAuthenticodeSignatureTraits.SignatureBeginSequence => "\r\n# SIG # Begin signature block\r\n";

/// <summary>
/// Code point sequence that is found after every signature
/// </summary>
string IAuthenticodeSignatureTraits.SignatureEndSequence => "\r\n# SIG # End signature block\r\n";

/// <summary>
/// Code point sequence that is found at the beginning of each signature chunk
/// </summary>
string IAuthenticodeSignatureTraits.SignatureLineBeginning => "# ";

/// <summary>
/// Code point sequence that is found at the end of each signature chunk, including the line terminator
/// </summary>
string IAuthenticodeSignatureTraits.SignatureLineEnding => "\r\n";

/// <summary>
/// Number of base64 characters found on each signature chunk.
/// </summary>
int IAuthenticodeSignatureTraits.SignatureCharsPerLine => 64;
}
}
107 changes: 107 additions & 0 deletions ProcessExecutionResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Diagnostics;
using System.Text;
using System.Threading;

namespace PowershellScriptTimestamp
{
internal class ProcessExecutionResult
{
public string ExecutablePath { get; }
public string CommandLineArguments { get; }

public bool Successful { get; }
public int ExitCode { get; }

public string Stdout { get; }
public string Stderr { get; }


public ProcessExecutionResult(string path, string arguments)
{

ExecutablePath = path;
CommandLineArguments = arguments;

var outStringBuilder = new StringBuilder();
var errStringBuilder = new StringBuilder();
using (var process = new Process())
try
{

process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = arguments;
process.StartInfo.FileName = path;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;

using (AutoResetEvent stdoutConsumed = new AutoResetEvent(false))
using (AutoResetEvent stderrConsumed = new AutoResetEvent(false))
{
process.OutputDataReceived += (sender, evtArgs) =>
{
if (evtArgs.Data == null)
{
stdoutConsumed.Set();
}
else
{
outStringBuilder.AppendLine(evtArgs.Data);
}
};
process.ErrorDataReceived += (sender, evtArgs) =>
{
if (evtArgs.Data == null)
{
stderrConsumed.Set();
}
else
{
errStringBuilder.AppendLine(evtArgs.Data);
}
};

Console.WriteLine($"[INFO] About to run process {path} with arguments {arguments}.");

if (!process.Start())
{
Console.WriteLine($"[ERROR] Could not start process.");
return;
}

try
{
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
stdoutConsumed.WaitOne();
stderrConsumed.WaitOne();
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Could not wait for end of process. Dumping exception.");
Console.WriteLine(ex);
return;
}
}

ExitCode = process.ExitCode;
Successful = true;

return;
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Could not run process. Dumping exception.");
Console.WriteLine(ex);
}
finally
{
Stdout = outStringBuilder.ToString();
Stderr = errStringBuilder.ToString();
}
}

}
}
Loading

0 comments on commit de4809c

Please sign in to comment.