Skip to content

Commit

Permalink
Merge pull request #11 from 1RedOne/ConvertingToThreading
Browse files Browse the repository at this point in the history
Converting to threading
  • Loading branch information
1RedOne authored May 5, 2019
2 parents 5404fc8 + ca11341 commit 19e40b5
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 170 deletions.
80 changes: 66 additions & 14 deletions ClientFaux/FauxDeployCMAgent.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
using Microsoft.ConfigurationManagement.Messaging.Framework;
using Microsoft.ConfigurationManagement.Messaging.Messages;
using Microsoft.ConfigurationManagement.Messaging.Sender.Http;
using System.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.Diagnostics;
using System.Xml;
using static CMFaux.CMFauxStatusViewClasses;
using Microsoft.ConfigurationManagement.Messaging.Messages.Server;
using System.IO;
using CERTENROLLLib;

namespace CMFaux
{
Expand All @@ -28,7 +23,6 @@ public static SmsClientId RegisterClient(string CMServerName, string ClientName,
{
X509Certificate2 thisCert = new X509Certificate2(CertPath, pass);


Console.WriteLine(@"Using certificate for client authentication with thumbprint of '{0}'", certificate.Thumbprint);
Console.WriteLine("Signature Algorithm: " + thisCert.SignatureAlgorithm.FriendlyName);

Expand Down Expand Up @@ -164,8 +158,7 @@ public static void SendDiscovery(string CMServerName, string ClientName, string
//hinvMessage.Settings.Security.EncryptMessage = true;
hinvMessage.Discover();

var Classes = CMFauxStatusViewClasses.GetWMIClasses();

var Classes = CMFauxStatusViewClasses.GetWMIClasses();
foreach (string Class in Classes)
{

Expand All @@ -188,7 +181,6 @@ public static void SendDiscovery(string CMServerName, string ClientName, string
hinvMessage.SendMessage(Sender);
};
}

public static void SendCustomDiscovery(string CMServerName, string ClientName, string SiteCode, string FilePath, List<CustomClientRecord> customClientRecords)
{
string ddmLocal = FilePath + "\\DDRS\\" + ClientName;
Expand All @@ -212,7 +204,7 @@ public static void SendCustomDiscovery(string CMServerName, string ClientName, s
FileInfo file = di.GetFiles().FirstOrDefault();

File.Copy(file.FullName, CMddmInbox, true);
System.IO.Directory.Delete(ddmLocal);
System.IO.Directory.Delete(ddmLocal, true);

}
public static void GetPolicy(string CMServerName, string ClientName, string DomainName, string SiteCode, string outPutDirectory, string CertPath, string pass, SmsClientId clientId)
Expand All @@ -237,7 +229,7 @@ public static void GetPolicy(string CMServerName, string ClientName, string Doma
userPolicyMessage.Settings.Security.EncryptMessage = encryption;
userPolicyMessage.Settings.ReplyCompression = (true == replyCompression) ? MessageCompression.Zlib : MessageCompression.None;
userPolicyMessage.Settings.Compression = (true == compression) ? MessageCompression.Zlib : MessageCompression.None;
userPolicyMessage.SendMessage(Sender);
//userPolicyMessage.SendMessage(Sender);

ConfigMgrPolicyAssignmentRequest machinePolicyMessage = new ConfigMgrPolicyAssignmentRequest();
machinePolicyMessage.Settings.HostName = CMServerName;
Expand All @@ -249,11 +241,71 @@ public static void GetPolicy(string CMServerName, string ClientName, string Doma
machinePolicyMessage.SmsId = clientId;
machinePolicyMessage.SiteCode = SiteCode;
machinePolicyMessage.Discover();
machinePolicyMessage.SendMessage(Sender);
//machinePolicyMessage.SendMessage(Sender);
}

}

public static X509Certificate2 CreateSelfSignedCertificate(string subjectName)
{
// create DN for subject and issuer
var dn = new CX500DistinguishedName();
dn.Encode("CN=" + subjectName, X500NameFlags.XCN_CERT_NAME_STR_NONE);
// create a new private key for the certificate
CX509PrivateKey privateKey = new CX509PrivateKey
{
ProviderName = "Microsoft Enhanced RSA and AES Cryptographic Provider",
MachineContext = false,
Length = 2048,
KeySpec = X509KeySpec.XCN_AT_SIGNATURE, // use is not limited
ExportPolicy = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_PLAINTEXT_EXPORT_FLAG
};
privateKey.Create();

// Use the stronger SHA512 hashing algorithm
var hashobj = new CObjectId();
hashobj.InitializeFromAlgorithmName(ObjectIdGroupId.XCN_CRYPT_HASH_ALG_OID_GROUP_ID,
ObjectIdPublicKeyFlags.XCN_CRYPT_OID_INFO_PUBKEY_ANY,
AlgorithmFlags.AlgorithmFlagsNone, "SHA256");

// add extended key usage if you want - look at MSDN for a list of possible OIDs
var oid = new CObjectId();
oid.InitializeFromValue("1.3.6.1.5.5.7.3.1"); // SSL server
var oidlist = new CObjectIds();
oidlist.Add(oid);
var eku = new CX509ExtensionEnhancedKeyUsage();
eku.InitializeEncode(oidlist);

// Create the self signing request
var cert = new CX509CertificateRequestCertificate();
cert.InitializeFromPrivateKey(X509CertificateEnrollmentContext.ContextUser, privateKey, "");
cert.Subject = dn;
cert.Issuer = dn; // the issuer and the subject are the same
cert.NotBefore = DateTime.Now;
// this cert expires immediately. Change to whatever makes sense for you
cert.NotAfter = DateTime.Now.AddYears(1);
cert.X509Extensions.Add((CX509Extension)eku); // add the EKU
cert.HashAlgorithm = hashobj; // Specify the hashing algorithm
cert.Encode(); // encode the certificate

// Do the final enrollment process
var enroll = new CX509Enrollment();
enroll.InitializeFromRequest(cert); // load the certificate
enroll.CertificateFriendlyName = subjectName; // Optional: add a friendly name
string csr = enroll.CreateRequest(); // Output the request in base64
// and install it back as the response
enroll.InstallResponse(InstallResponseRestrictionFlags.AllowUntrustedCertificate,
csr, EncodingType.XCN_CRYPT_STRING_BASE64, ""); // no password
// output a base64 encoded PKCS#12 so we can import it back to the .Net security classes
var base64encoded = enroll.CreatePFX("", // no password, this is for internal consumption
PFXExportOptions.PFXExportChainWithRoot);

// instantiate the target class with the PKCS#12 data (and the empty password)
return new System.Security.Cryptography.X509Certificates.X509Certificate2(
System.Convert.FromBase64String(base64encoded), "",
// mark the private key as exportable (this is usually what you want to do)
System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable
);
}
}
}

21 changes: 12 additions & 9 deletions ClientFaux/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@
<RowDefinition Height="250"/>
<RowDefinition Height="500*"/>
</Grid.RowDefinitions>
<!--<Grid.ColumnDefinitions>
<ColumnDefinition Width="550*" />
<ColumnDefinition Width="400*" />
</Grid.ColumnDefinitions>-->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="700" />
</Grid.ColumnDefinitions>
<Image Source="Images\CMFoxv1.0Logo.png" Stretch="Fill" Grid.Row="0" Grid.ColumnSpan="2"></Image>
<StackPanel HorizontalAlignment="Stretch" Grid.Column="0" Margin="10" Grid.Row="1">
<TextBlock TextWrapping="WrapWithOverflow" FontSize="16">When both are checked off, we're ready to go!</TextBlock>
<Label Name="CMSettingsStatusLabel" FontSize="16">CM Settings: ✔</Label>
<Label Name="DeviceNameStatusLabel" FontSize="16">Device Naming: ✔</Label>
<Button Content="Ready..." MaxWidth="280px" HorizontalAlignment="Left" MinWidth="120px" Background="LawnGreen" Name="CreateClientsButton" Click="CreateClientsButton_Click"/>
<Expander Name="DeviceExpander">
<StackPanel Margin="10">
<DataGrid Name="dgDevices" AutoGenerateColumns="False">
<Label Name="ClientCount" FontSize="24">Device Count</Label>
<Label Name="Counter" FontSize="24" Content="{Binding IdCounter}"/>
<Expander Name="DeviceExpander" >
<ContentControl HorizontalAlignment="Left" Margin="10,0,0,0" MaxWidth="700">
<DataGrid Name="dgDevices" AutoGenerateColumns="False" MaxHeight="500">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True"/>
<DataGridTextColumn Header="Status" Binding="{Binding Status}" IsReadOnly="True" />
Expand Down Expand Up @@ -77,7 +78,7 @@
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</StackPanel>
</ContentControl>
</Expander>
</StackPanel>
</Grid>
Expand Down Expand Up @@ -108,7 +109,9 @@
<Label>Starting Number</Label>
<TextBox ToolTip="The starting digit when making multiple clients, something like [10]." Name="StartingNumber" PreviewTextInput="TextBox_OnStartingButtonTextInput" Width="20px" HorizontalAlignment="Left" Margin="0,0,10,0" TextChanged="textChangedEventHandler"></TextBox>
<Label>Ending Number</Label>
<TextBox ToolTip="The ending digit when making multiple clients, something like [20]." Name="EndingNumber" PreviewTextInput="TextBox_OnEndingButtonTextInput" Width="20px" HorizontalAlignment="Left" Margin="0,0,30,0" TextChanged="textChangedEventHandler"></TextBox>
<TextBox ToolTip="The ending digit when making multiple clients, something like [20]." Name="EndingNumber" PreviewTextInput="TextBox_OnEndingButtonTextInput" Width="45px" HorizontalAlignment="Left" Margin="0,0,30,0" TextChanged="textChangedEventHandler"></TextBox>
<Label>Maximum Threads</Label>
<TextBox ToolTip="How many threads woudl you like to use? Seven will max out even very beefy systems." Name="MaximumThreads" PreviewTextInput="TextBox_OnEndingButtonTextInput" Width="20px" HorizontalAlignment="Left" Margin="0,0,30,0" TextChanged="MaximumThreads_TextChanged"></TextBox>
<Label>Number of Fake CM Clients to be created </Label>
<TextBlock Name="NumberOfClients" Foreground="#FF1053CD">Count will appear here</TextBlock>
</StackPanel>
Expand Down
Loading

0 comments on commit 19e40b5

Please sign in to comment.