Modify Compiled .Net Exe - c#

I have an application written in C# (without the source of course), that needs to be changed a little bit. For example, I need to stop a few lines of code that create an unnecessary menu. So I think I should comment them out.
The source code is not obfuscated. I know I can completely decompile, change, and compile again, using tools like Reflector/Reflexil. But everyone knows that by doing this, many parts of code won't compile again! Is there a way in Reflector (or any other product) that a part of could be disabled/changed without this process?
Thanks.

You might want to try dnSpy. It is a .NET assembly editor, decompiler, and debugger forked from ILSpy.
https://github.com/0xd4d/dnSpy

If you really needed to do this, you could decompile it with Reflector (or a similar product) and then use that to try to recreate a solution in .Net that will produce the same executable.
You may run into issues around:
Obfuscated code
Sections where the decompile shows you accurate code for specific sections, but for some reason it just doesn't work in your new solution (and then what do you do?)
This is not to mention the potential legal issues related to doing this. If the executable was released under a license that would permit you to do this, then you would most likely have access to the source code. So the fact that you do not have access to the source code implies that doing what you are suggesting might not be legal.

Eventually I managed to "disable" a few lines of code in the compiled exe.
I used Reflector with Reflexil plugin installed. Reflexil allowed me to edit an MSIL instruction, and then save the result back to an exe file. So, it involved learning a few MSIL instructions, especially the "no operation" command (making a line of code do nothing). To see the list of instructions and a tutorial, see here and here.
Hope it helps someone else.

for the sake of completeness:
Another possible solution is to use the ildasm http://msdn.microsoft.com/en-US/library/f7dy01k1%28v=vs.80%29.aspx
MSIL Disassembler, edit the MSIL and feed it back to ilasm.
How practical this solution is, depends on you of course

This thread may help: dotnet dll decompile and change the code
Last time When I tried with decompile the source using reflection, I got too many compilation issues with regarding to resources and other subs though the dll isn't obfuscated. So there could be things beyond just extracting the source and modifying in order to make your new dll work as the old one.
SO I would suggest to go with direct dll manipulation using any of the options mentioned in the other thread.

If you have source code on the same machine on which you are testing your exe file, and if you are making changes in your sourcecode in visual studio, then while compiling it will automatically get reflected in your exe file.
You need not do any special thing for it. And if it is not, then just make the changes in code and paste your debugg folder's new exe (with debugg folder) on another machine having all recent changes.

Related

Is that possible to extract C# source code from Winforms application exe file

Can someone tell me if it is possible to view source code only using exe file. For example I have made project and in debug there is exe file so if I'll send this to someone he will be able to view it's source code(Using hacking tolls or by something)?
The answer is Yes
Your code can be de-compiled and can be seen.
I personally use this one for .net:
http://www.jetbrains.com/decompiler/
But it is not the only one, there are many decompilers exist.
Yep, there's Resharper or ILSpy
Certainly he/she can see your IL code, the code that has been constructed by your compiler. For instance, a tool for doing this is the MSIL Disassembler http://msdn.microsoft.com/en-us/library/f7dy01k1(v=vs.110).aspx. I suppose having the IL language it wouldn't be difficult to get the C# code.
You must use a decompiler as Net Reflector or JetBrains, anyway if your exe is obfuscated also them can not work
You can use the default ILDASM (IL disassembler) from VS command prompt.

Why am I able to debug my assembly?

I am working on an assembly that handles various color transformations. When I load the assembly into a new project to test, if there happens to be an bug in the assembly, Visual Studio opens the offending code from the DLL. I can step through all of the code in the assembly.
I definitely don't want the code to be so easily visible/available. I would like the code to be somewhat "locked" in the assembly.
How can I set the DLL to simply throw some sort of error instead of opening?
Edit
I'm not interested in the code being "safe" and I have no need to obfuscate. This library is being used internally and the code itself is perfectly accessible to tohers. What I don't want is for someone using the library to find themselves suddenly debugging the assembly. If there is a problem, I prefer to have an error thrown instead of the assembly code opening in Visual Studio.
This is happening because you have VS installed on the machine, and because you are deploying the PDB files - you will not get this dialogue box if VS is not installed.
Additionally:
Do not deploy code that has been built in the Debug configuration. These contain additional information that helps with debugging.
Make sure you do not deploy the PDB files with the executables. Same as above, and they are not needed for running the code.
Both these will help, but any assembly would be easily decompiled with reflector, so you may also want to investigate obfuscators to stop other programmers from easily seeing your code.
There is a list of C# obfuscators here : http://www.csharp411.com/net-obfuscators/
What you need is to obfuscate your binaries.
Basically if you want your code to be safe and you dont want your classes are exposed to others, you should definitely need to Obfuscate your code.
To obfuscate your code you can use DotFuscator, it is included with Visual Studio installation.
check my article on it.
http://www.codeproject.com/KB/dotnet/code_security.aspx

Using Reflector To Create VisualStudio Project

I have a .exe app which I want to understand better - I can see it in reflector
Is there any way to get reflector to create a VS project with the code so I can view it properly in Visual Studio?
Nothing special is needed, it is built into Reflector, albeit not very discoverable. Right-click the assembly in the left pane and choose Export. You'll get a chance to change the output directory. Click OK and Reflector starts decompiling the code, creating a source file for each individual class. And creates a .csproj file which you can open in Visual Studio.
Check out Jason Bock's FileGenerator, it might be what you are looking for.
I've used Denis Bauer's Reflector.FileDisassembler http://www.denisbauer.com/NETTools/FileDisassembler.aspx. It works well enough to compile and step through the code.
Yea there is, but it doesn't come cheap
http://www.remotesoft.com/salamander/
I have used it to decompile assembly, but I've never used the feature to decompile it into a project so can't give you a review on that. The quality of the decompiler will match the one in reflector.
They also be some legal issues associated with decompiling exe into project - and source for recompilation, so use it with care.

(.net) Intermediate language exe editor?

I know we have ILdasm, but is there any tool out there that will let me edit .exe or .dll files without having to go through all the rigmarole of having to convert it to IL code, with resources includeded, etc etc, manually edit, then recompile again?
You could always use Reflector to disassemble whole namespaces to source code (not IL), but then you're still stuck without a direct editor, you have to copy/paste to a code file and recompile.
On the other hand, it seems like I was wrong, Reflector has an add-in Reflexil that looks like it'll do what you want.
check out this SourceForge project I guess it's what you're looking for:
http://sourceforge.net/projects/dile/
Perhaps you should check out Mono.Cecil. It's a managed library for manipulating IL. You can add, remove and modify methods as you please.
Granted it's not an IDE or anything but it should be a starting point.

Compile all C# files into one file?

Is there a way to compile C# files into one single file that is ready to give to the user?
One way is to use ILMerge. This will merge multiple assemblies into a single one (think combining all DLLs and a main exe into a single exe).
Yes :) But you must put all your sources into a single assembly and compile it to an EXE. Also note that the target system must also have the required .NET infrastructure installed.
Note that security policies on the target system may prevent the user from directly running your app.
Lastly, unless you "NGEN" your code, it will be jitted the first time it runs. This will incur some startup time costs. THese can be considerable in some instances.
If your looking to also merge the .Net assemblies required by your app, you can use something like This to even compile in System.dll, System.Windows.Forms.dll, etc, so the end user wont even need .Net installed.
I got this from a similar question (sorry to whoever posted the answer - I can't seem to find the original to link it here):
how to embed your application’s dependent DLLs inside your EXE file
It is an excerpt from Jeffrey Richter's CLR Via C#. I have not tried it yet but looks very promising. I think it will work with WPF as well (about to find out for myself).

Categories