Implemented a simple impersonation demo.

This commit is contained in:
antiduh
2014-06-30 20:09:42 +00:00
parent a41e35dc84
commit fcacd2195b
2 changed files with 65 additions and 31 deletions

View File

@@ -33,6 +33,8 @@
this.startButton = new System.Windows.Forms.Button();
this.stopButton = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.clientUsernameTextBox = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.serverUsernameTextbox = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
@@ -42,8 +44,7 @@
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.receivedTextbox = new System.Windows.Forms.TextBox();
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.label3 = new System.Windows.Forms.Label();
this.clientUsernameTextBox = new System.Windows.Forms.TextBox();
this.impersonateButton = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.portNumeric)).BeginInit();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
@@ -110,11 +111,30 @@
this.groupBox1.Controls.Add(this.serverUsernameTextbox);
this.groupBox1.Location = new System.Drawing.Point(400, 12);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(305, 76);
this.groupBox1.Size = new System.Drawing.Size(450, 76);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Credentials";
//
// clientUsernameTextBox
//
this.clientUsernameTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.clientUsernameTextBox.Location = new System.Drawing.Point(106, 47);
this.clientUsernameTextBox.Name = "clientUsernameTextBox";
this.clientUsernameTextBox.ReadOnly = true;
this.clientUsernameTextBox.Size = new System.Drawing.Size(338, 20);
this.clientUsernameTextBox.TabIndex = 3;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(7, 50);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(85, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Client username:";
//
// label2
//
this.label2.AutoSize = true;
@@ -131,18 +151,19 @@
this.serverUsernameTextbox.Location = new System.Drawing.Point(106, 20);
this.serverUsernameTextbox.Name = "serverUsernameTextbox";
this.serverUsernameTextbox.ReadOnly = true;
this.serverUsernameTextbox.Size = new System.Drawing.Size(193, 20);
this.serverUsernameTextbox.Size = new System.Drawing.Size(338, 20);
this.serverUsernameTextbox.TabIndex = 0;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.impersonateButton);
this.groupBox2.Controls.Add(this.signButton);
this.groupBox2.Controls.Add(this.encryptButton);
this.groupBox2.Controls.Add(this.sendTextbox);
this.groupBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.groupBox2.Location = new System.Drawing.Point(3, 3);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(340, 396);
this.groupBox2.Size = new System.Drawing.Size(413, 389);
this.groupBox2.TabIndex = 5;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Send a message to the client";
@@ -150,7 +171,7 @@
// signButton
//
this.signButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.signButton.Location = new System.Drawing.Point(174, 357);
this.signButton.Location = new System.Drawing.Point(139, 350);
this.signButton.Name = "signButton";
this.signButton.Size = new System.Drawing.Size(117, 23);
this.signButton.TabIndex = 3;
@@ -160,7 +181,7 @@
// encryptButton
//
this.encryptButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.encryptButton.Location = new System.Drawing.Point(33, 357);
this.encryptButton.Location = new System.Drawing.Point(6, 350);
this.encryptButton.Name = "encryptButton";
this.encryptButton.Size = new System.Drawing.Size(120, 23);
this.encryptButton.TabIndex = 2;
@@ -175,16 +196,16 @@
this.sendTextbox.Location = new System.Drawing.Point(6, 19);
this.sendTextbox.Multiline = true;
this.sendTextbox.Name = "sendTextbox";
this.sendTextbox.Size = new System.Drawing.Size(327, 330);
this.sendTextbox.Size = new System.Drawing.Size(400, 323);
this.sendTextbox.TabIndex = 0;
//
// groupBox3
//
this.groupBox3.Controls.Add(this.receivedTextbox);
this.groupBox3.Dock = System.Windows.Forms.DockStyle.Fill;
this.groupBox3.Location = new System.Drawing.Point(349, 3);
this.groupBox3.Location = new System.Drawing.Point(422, 3);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(341, 396);
this.groupBox3.Size = new System.Drawing.Size(413, 389);
this.groupBox3.TabIndex = 6;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Received messages from the client";
@@ -195,7 +216,7 @@
this.receivedTextbox.Location = new System.Drawing.Point(3, 16);
this.receivedTextbox.Multiline = true;
this.receivedTextbox.Name = "receivedTextbox";
this.receivedTextbox.Size = new System.Drawing.Size(335, 377);
this.receivedTextbox.Size = new System.Drawing.Size(407, 370);
this.receivedTextbox.TabIndex = 0;
//
// tableLayoutPanel1
@@ -212,33 +233,23 @@
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 1;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(693, 402);
this.tableLayoutPanel1.Size = new System.Drawing.Size(838, 395);
this.tableLayoutPanel1.TabIndex = 7;
//
// label3
// impersonateButton
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(7, 50);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(85, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Client username:";
//
// clientUsernameTextBox
//
this.clientUsernameTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.clientUsernameTextBox.Location = new System.Drawing.Point(106, 47);
this.clientUsernameTextBox.Name = "clientUsernameTextBox";
this.clientUsernameTextBox.ReadOnly = true;
this.clientUsernameTextBox.Size = new System.Drawing.Size(193, 20);
this.clientUsernameTextBox.TabIndex = 3;
this.impersonateButton.Location = new System.Drawing.Point(262, 350);
this.impersonateButton.Name = "impersonateButton";
this.impersonateButton.Size = new System.Drawing.Size(116, 23);
this.impersonateButton.TabIndex = 4;
this.impersonateButton.Text = "Test impersonation";
this.impersonateButton.UseVisualStyleBackColor = true;
//
// ServerForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(717, 499);
this.ClientSize = new System.Drawing.Size(862, 492);
this.Controls.Add(this.tableLayoutPanel1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.stopButton);
@@ -278,6 +289,7 @@
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.TextBox clientUsernameTextBox;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button impersonateButton;
}
}

View File

@@ -11,6 +11,7 @@ using TestProtocol;
namespace TestServer
{
using System.IO;
using NSspi;
using NSspi.Contexts;
using NSspi.Credentials;
@@ -57,6 +58,7 @@ namespace TestServer
this.encryptButton.Click += encryptButton_Click;
this.signButton.Click += signButton_Click;
this.impersonateButton.Click += impersonateButton_Click;
this.running = false;
this.initializing = false;
@@ -118,7 +120,27 @@ namespace TestServer
this.server.Send( message );
}
private void impersonateButton_Click( object sender, EventArgs e )
{
ImpersonationHandle handle;
using( handle = this.serverContext.ImpersonateClient() )
{
MessageBox.Show( "Starting impersonation: " + Environment.UserName );
FileStream stream = File.Create( Environment.GetFolderPath( Environment.SpecialFolder.DesktopDirectory) + @"\test.txt" );
StreamWriter writer = new StreamWriter( stream, Encoding.UTF8 );
writer.WriteLine( "Hello world." );
writer.Close();
stream.Close();
}
MessageBox.Show( "Impersonation complete: " + Environment.UserName );
}
private void UpdateButtons()
{
this.startButton.Enabled = this.running == false;