ED 330: C# Dot Net (20 pts extra)

What You Need

Purpose

Practice making simple Dot NET apps in C# using Visual. Studio 2019.

Downloading and Installing Visual Studio 2019

In your Windows server, open a Web browser.

Open this page:

https://www.visualstudio.com/downloads/

In the "Community" section, click the "Free download" button, as shown below.

Point to "Download Visual Studio 2019" and click "Community 2019", as shown below.

Save the vs_community__...exe file in your Downloads folder.

Double-click the the vs_community__...exe file.

At the security warning, click Yes to open the file. Click Continue.

A large "Installing" window opens. Click ".Net desktop development", as shown below, and, in the lower right, click the Install button.

Wait while software downloads and installs.

A box pops up saying "Welcome! Connect to all your developer services."

Click "Not now, maybe later".

Click the "Start Visual Studio" button.

Making a Console "Hello World" Console App with C#

In the "Get Started" page, click "Create a new project", as shown below.

In the "Create a new project" page, accept the default selection of "Console App (.NET Core)" in C#, as shown below, and click Next.

In the "Configure your new project" page, click Create.

Your app appears, as shown below.

At the top center, click the green-arrow button labelled ConsoleApp1.

The program compiles and runs, printing "Hello World!" in a "Microsoft Visual Studio Debug Console" window, as shown below.

String Manipulations

Replace the body of the Main() routine with this code, as shown below:
string s = "SHORT";
s = Console.ReadLine();
Console.WriteLine("You said: {0}", s);

At the top center, click the green-arrow button labelled ConsoleApp1.

The program compiles and runs, opens a "Microsoft Visual Studio Debug Console" window. Enter a long string of AAAAAA characters into that window and press Enter.

The program runs without crashing, as shown below.

C would have crashed with a buffer overflow, but C# DOT NET doesn't. Rather than inserting the long string into the space reserved for a short string, it creates a new string with the proper size.

Using Unsafe Code

Replace the entire code with the new code shown below:
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
        	string s = "SHORT";
			s = Console.ReadLine();
			Console.WriteLine("You said: {0}", s);
			bufferOverflow(s);
        }

		static unsafe void bufferOverflow(string s)
		{
		    char* ptr = stackalloc char[10];

		    foreach (var c in s)
    		{
        		*ptr++ = c; // Bufferoverflow if s.Length > 10
    		}
		}
    }
}

From the menu, click Project, "ConsoleApp1 Properties".

On the left side, click the Build tab. Check the "Allow unsafe code" box, as shown below.


Flag ED 330.1: Crash Message (10 pts extra)

At the top center, click the green-arrow button labelled ConsoleApp1.

The program compiles and runs, opens a "Microsoft Visual Studio Debug Console" window. Enter a long string of AAAAAA characters into that window and press Enter.

The program crashes, as shown below.

The flag is covered by a green rectangle in the image below.

Making a Windows Form

If Visual Studio is running, from the menu bar, click File, New, Project.

Otherwise, launch Visual Studio from the Start button.

In the "Get Started" page, click "Create a new project".

In the "Create a new project" page, scroll down and click "Windows Forms App (.NET Framework)", as shown below. Then click Next.

In the "Configure your new project" page, click Create.

Your app appears, with an empty "Form1" window, as shown below.

Adding Controls

From the menu bar, click View, Toolbox.

In the Toolbox, expand "Common Controls".

Follow these steps to add three controls to your form, as shown below.

Entering Code for the Button

Double-click the Enter button to see its underlying code.

Add this code, as shown below:

if (textBox1.Text == "topsecret")
    MessageBox.Show("WIN!");
else
    MessageBox.Show("FAIL!");

Running the Form

At the top center, click the green-arrow button labelled Start.

Your form appears, as shown below.

Enter a password of a and click the Enter button.

A box pops up saying "Fail!", as shown below. Click OK.

Enter a password of topsecret and click the Enter button.

A box pops up saying "WIN!", as shown below. Click OK.

From the menu bar, click Debug, "Stop Debugging".

Finding Your App

At the lower left, in the Output window, scroll up and to the right to find the location of the EXE file, outlined in green in the image below.


Flag ED 330.2: App Properties (10 pts extra)

Navigate to the EXE file you built.

Right-click it and click Properties.

Find the word covered by a green box in the image below. That's the flag.


References

C# Hello World: First Console Application Program

Are buffer overflow exploits possible in C#?

https://docs.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-wpf?view=vs-2019

WPF - Keyboard

Step 1: Create a Windows Forms App project

Design view does not show in visual studio 2019 community

Posted 10-15-19