Skip to content

Commit 7284658

Browse files
committed
added TIA firewall whitelisting
1 parent 98142c5 commit 7284658

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

TiaGitHandler/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
2020
using TiaGitHandler.Properties;
2121
using Application = System.Windows.Application;
22+
using InvalidOperationException = System.InvalidOperationException;
2223
using MessageBox = System.Windows.Forms.MessageBox;
2324
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
2425

@@ -182,6 +183,17 @@ static void Main(string[] args)
182183
}
183184
}
184185

186+
var version = file.Substring(file.Length - 2, 2);
187+
188+
try
189+
{
190+
TiaOpennessWhitelist.EnsureWhitelistEntry(version);
191+
}
192+
catch (InvalidOperationException ex)
193+
{
194+
Console.WriteLine($"Cannot set TIA whitelist registry entry: {ex.Message}");
195+
}
196+
185197
if (attach)
186198
{
187199
if (file.EndsWith("20"))
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using Microsoft.Win32;
2+
using System;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Reflection;
6+
using System.Security.Cryptography;
7+
8+
namespace TiaGitHandler
9+
{
10+
public static class TiaOpennessWhitelist
11+
{
12+
/// <summary>
13+
/// Retrieves the path to the currently executing .exe
14+
/// </summary>
15+
private static string GetApplicationPath()
16+
{
17+
// use the executing assembly’s location
18+
var path = Assembly.GetExecutingAssembly().Location;
19+
20+
if (!string.IsNullOrEmpty(path))
21+
return path;
22+
23+
// use the main module’s file name of the current process
24+
using (var proc = Process.GetCurrentProcess())
25+
{
26+
path = proc.MainModule?.FileName;
27+
if (!string.IsNullOrEmpty(path))
28+
return path;
29+
}
30+
31+
throw new InvalidOperationException("Could not determine the application path.");
32+
}
33+
34+
/// <summary>
35+
/// Ensures that a whitelist entry exists and is up-to-date.
36+
/// Creates or updates the entry only if Path, DateModified, or FileHash differ.
37+
/// </summary>
38+
/// <param name="tiaVersion">TIA Openness version, e.g. "V16.0".</param>
39+
/// <returns>True if the entry was created or updated; false if it was already current.</returns>
40+
public static bool EnsureWhitelistEntry(string tiaVersion)
41+
{
42+
// 1. determine the application path
43+
var applicationPath = GetApplicationPath();
44+
var exeName = Path.GetFileName(applicationPath);
45+
46+
// 2. Compute the desired DateModified value
47+
var fileInfo = new FileInfo(applicationPath);
48+
var desiredDate = fileInfo.LastWriteTimeUtc
49+
.ToString("yyyy/MM/dd HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture);
50+
51+
// 3. Compute the desired FileHash value (SHA-256, Base64)
52+
byte[] hashBytes;
53+
using (var sha = SHA256.Create())
54+
using (var stream = File.OpenRead(applicationPath))
55+
hashBytes = sha.ComputeHash(stream);
56+
var desiredHash = Convert.ToBase64String(hashBytes);
57+
58+
// 4. Construct the registry subkey path
59+
var subKey = $@"SOFTWARE\Siemens\Automation\Openness\{tiaVersion}\Whitelist\{exeName}\Entry";
60+
61+
// 5. Open or create the registry key
62+
using (var entryKey = Registry.LocalMachine.OpenSubKey(subKey, writable: true)
63+
?? Registry.LocalMachine.CreateSubKey(subKey, writable: true))
64+
{
65+
if (entryKey == null)
66+
throw new InvalidOperationException($"Could not create or open registry key: HKLM\\{subKey}");
67+
68+
// 6. Read existing values
69+
var currentPath = entryKey.GetValue("Path") as string;
70+
var currentDate = entryKey.GetValue("DateModified") as string;
71+
var currentHash = entryKey.GetValue("FileHash") as string;
72+
73+
// 7. Check if any value differs
74+
var needsUpdate =
75+
currentPath != applicationPath ||
76+
currentDate != desiredDate ||
77+
currentHash != desiredHash;
78+
79+
if (!needsUpdate)
80+
return false; // already up-to-date
81+
82+
// 8. Write the new values
83+
entryKey.SetValue("Path", applicationPath, RegistryValueKind.String);
84+
entryKey.SetValue("DateModified", desiredDate, RegistryValueKind.String);
85+
entryKey.SetValue("FileHash", desiredHash, RegistryValueKind.String);
86+
87+
return true;
88+
}
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)