title: Cracking C# application with OllyDebug
body: <p>I would like to know if there is a way to crack C# Windows application with OllyDebug. I have simple my own CrackMe application written with Visual C# 2010 Express. When I open it with OllyDebug and modify ASM code as I need, there is no "Copy to executable" option in OllyDebug since my registration form window is dynamically allocated with "new" operator (which is, I believe, VirtualAlloc() function call in debugger). Though I am able to modify ASM code (which is simply NOP'ing JE jumps), I am not able to save my .exe file with cracked code, looks like OllyDbg "sees" the code in data segment which is not existing when the application launches and only is dynamically allocated.
  Can anyone help me with the problem? I think modifying *.exe should be possible with at least 2 approaches: </p>
  
  <p>1) Dig deeper into code with OllyDbg and find place where actual code is held before allocation (because new instance of RegistrationForm doesn't come magically out of space, does it?)</p>
  
  <p>2) If it allows fast creation of application in VS Express and doesn't require too much complicated code, use static calls so each time clicking on "Register" shows the same RegistrationForm window (which will be held in code section of application and therefore will be modifyable in OllyDbg). </p>
  
  <p>It will be OK to point out how to rewrite code and keep it simple to allocate same instance of RegistrationForm (singleton?). The only thing I need is to crack&amp;save *.exe, relaunch and fill in any data to "complete registration".</p>
  
  <p>Here is code of MyCrackMe class with Main() method:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace MyCrackMe {
      class MyCrackMe {
          public static void Main() {
              MyForm mainWindow = new MyForm();
              System.Windows.Forms.Application.Run(mainWindow);
          }
      }
  }
  </code></pre>
  
  <p>Main window class:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  
  namespace MyCrackMe {
      public partial class MyForm : Form {
          public MyForm() {
              InitializeComponent();
          }
  
          private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
              Application.Exit();
          }
  
          private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
              MessageBox.Show("All rights reserved", "Message");
          }
  
          private void registerToolStripMenuItem_Click(object sender, EventArgs e) {
              RegistrationForm registrationForm = new RegistrationForm();
              registrationForm.Show();
          }
      }
  }
  </code></pre>
  
  <p>Registration form class:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Runtime.InteropServices;
  
  namespace MyCrackMe {
      public partial class RegistrationForm : Form {
          // Use DllImport to import the Win32 MessageBox function.
  
          [DllImport("user32.dll", EntryPoint = "MessageBoxA", CharSet = CharSet.Ansi)]
          public static extern int MsgBox(int hWnd, String text, String caption, uint type);
  
          public RegistrationForm() {
              InitializeComponent();
          }
  
          private void button1_Click(object sender, EventArgs e) {
              if (textBox1.Text == "lincoln" &amp;&amp; textBox2.Text == "12345") {
                  MsgBox(0, "Registration completed successfully!", "Registration Message", 0);
              } else {
                  MsgBox(0, "Registration failed", "Message", 0);
              }
          }
      }
  }
  </code></pre>
  
  <p>Here is OllyDbg screenshot and message which comes when setting breakpoints
  <img src="https://i.stack.imgur.com/LCSnF.png" alt="ollydbgscreenshot"></p>