Skip to content

Commit 67816b9

Browse files
authored
Merge pull request #69 from RemiEscourrou/dev-computerversion
Add scanner computer version
2 parents 0a4315a + 0836968 commit 67816b9

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

NativeMethods.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,45 @@ public static DateTime GetStartupTime(string server)
643643
}
644644
}
645645

646-
[DllImport("winspool.drv", CharSet = CharSet.Unicode, EntryPoint = "OpenPrinterW", SetLastError = true)]
646+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
647+
public struct WKSTA_INFO_100
648+
{
649+
public int platform_id;
650+
public string computer_name;
651+
public string lan_group;
652+
public int ver_major;
653+
public int ver_minor;
654+
}
655+
656+
[DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
657+
internal static extern uint NetWkstaGetInfo(
658+
string servername,
659+
int level,
660+
out IntPtr bufptr);
661+
662+
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
663+
public static string GetComputerVersion(string server)
664+
{
665+
IntPtr buffer = IntPtr.Zero;
666+
uint ret = NetWkstaGetInfo(server,100, out buffer);
667+
if (ret != 0)
668+
{
669+
Trace.WriteLine("GetComputerVersion " + server + " returned " + ret);
670+
return "not found";
671+
}
672+
try
673+
{
674+
WKSTA_INFO_100 data = (WKSTA_INFO_100)Marshal.PtrToStructure(buffer, typeof(WKSTA_INFO_100));
675+
string version = data.ver_major.ToString() + "." + data.ver_minor.ToString();
676+
return version;
677+
}
678+
finally
679+
{
680+
NetApiBufferFree(buffer);
681+
}
682+
}
683+
684+
[DllImport("winspool.drv", CharSet = CharSet.Unicode, EntryPoint = "OpenPrinterW", SetLastError = true)]
647685
internal static extern bool OpenPrinter(string pPrinterName, out IntPtr phPrinter, IntPtr pDefault);
648686

649687
[DllImport("winspool.drv", CharSet = CharSet.Unicode, EntryPoint = "ClosePrinter", SetLastError = true)]

PingCastle.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@
283283
<Compile Include="Rules\RuleSet.cs" />
284284
<Compile Include="Scanners\AntivirusScanner.cs" />
285285
<Compile Include="Scanners\bluekeepscanner.cs" />
286+
<Compile Include="Scanners\ComputerVersion.cs" />
286287
<Compile Include="Scanners\ZeroLogonScanner.cs" />
287288
<Compile Include="Scanners\ExportUsersScanner.cs" />
288289
<Compile Include="Scanners\RemoteScanner.cs" />

Scanners/ComputerVersion.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Copyright (c) Ping Castle. All rights reserved.
3+
// https://www.pingcastle.com
4+
//
5+
// Licensed under the Non-Profit OSL. See LICENSE file in the project root for full license information.
6+
//
7+
using PingCastle.ADWS;
8+
using PingCastle.misc;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Diagnostics;
12+
using System.IO;
13+
using System.Net;
14+
using System.Net.Sockets;
15+
using System.Security.Principal;
16+
using System.Text;
17+
using System.Threading;
18+
19+
namespace PingCastle.Scanners
20+
{
21+
public class ComputerVersion : ScannerBase
22+
{
23+
public override string Name { get { return "computerversion"; } }
24+
25+
public override string Description { get { return "Get the version of a computer. Can be used to determine if obsolete operating systems are still present."; } }
26+
27+
override protected string GetCsvHeader()
28+
{
29+
return "Computer\tVersion";
30+
}
31+
32+
override protected string GetCsvData(string computer)
33+
{
34+
string version = NativeMethods.GetComputerVersion(computer);
35+
if (version != "not found")
36+
{
37+
return computer + "\t" + version;
38+
}
39+
return null;
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)