c# wpf .net Confusere obfuscator - c#

I have this problem:
I made a C# WPF (.NET Framework 4.0) application and everything is ok. Now I want to obfuscate it. I tried with Confuser.
The question is:
If the app is published I can see the main exe file and dll-s. So I tried to obfuscate the exe. Where should I put the obfuscated .exe generated file (I obfuscate only the main exe file). I mean Confuser creates a folder called "Confuser" with the obfuscated exe in it.
If I doubleclick the obfuscated exe to start the app as usually it doesn't work and I get the normal application crush window.
I also tried to replace the original exe file (in the app folder) with the obfuscated one (from Confuser folder).
If the app is deployed I have the setup.msi pack or setup.exe. How should I obfuscate in this case ?
Thankx,
Adrian

All XAML-based platforms (WPF, Silverlight, Windows Phone) are quite problematic area for most obfuscators. The main problem is that XAML references types and members by name. No wonder the applications often doesn't work after obfuscation: the integrity breaks.
There are two ways that are used by obfuscators to keep the output assembly work afterwards:
Search XAML content for everything that could be a type or property name and then roughly exclude all members that anyhow match these names. This doesn't give 100% guarantee that the application is not broken while makes the obfuscation coverage poor because of the inaccurate analysis. Still this approach can potentially save your application from breaking.
Do something similar to what XamlReader does: load XAML tree and find all matching CLR types and members. After renaming all the changes are synchronized with XAML. This approach gives the best reliability and coverage (types and members are renamed both in the code and XAML). There are obfuscators on the market that claim that they support XAML renaming. You may want to try some of them.
There is also a workaround. Most obfuscators support conditional obfuscation allowing to manually exclude specific types and members. You should manually exclude all the types and members that are somehow referenced by XAML. This will take some time but usually works.

First, you obfuscate your main .exe file and replace the old one. Then you pack everything into installer.
Also make sure all the properties and types involved in xaml are declared as public. Or tell the obfuscator to ignore them somehow.

I'd experiment with a simple console app first. Make sure you can get it working with that.
Then I might progress to a simple WPF app.
The problem with WPF and obfuscation is it relies on reflection. The View Models are bound by name. It could be this obfuscator isn't sensitive to this, or you need to configure it to ignore the names of properties on your view models.
If you've any other reflection using name, that could break it too. Even a Enum.Parse call can be tripped up. Maybe you seralize to/from XML, that is also a point it could break at.
You may want to display the exception that the app is crashing with to help you debug, but I would get comfortible with the tool myself with simple apps to work out how it gets around the above issues.

Related

Why are source code seen in exe as it is supposed to be binary?

Recently I did an experiment with one of my c# .NET compiled exe by renaming the extension to .txt.
When viewed in the Notepad, I was able to see all my source code in English Language which made me confused if I was looking at a .cs file or a .exe file.
After this event I realized that anyone who has this exe can copy all the codes an re-compile them into a clone with some hidden additional codes (or malwares) that could harm my reputation among users.
However I know a little about obfuscation which can rename all methods and properties into a non-human readable format, still I would like to ask here, isn't an exe program supposed to be (only) in a computer readable binary format (as I have learnt in school)? Why is there a need of obfuscation additionally? Is this problem only limited to exe which has a CLR header? Should I consider creating applications on other languages like C, if I want to make it hard to decompile?
Please Answer...
Edit:- Most of the codes can be seen in this area and I am unaware of what part of the source code it consists of.
Edt:- Also when I do the same with an exe that is compiled using Visual Foxpro (where Encrypted is checked in Project Properties), not even a single readable text is seen except some assembly infos. What encryption technique is used in it, can we use it (or like that) in dotnet (free or paid)?
Source - https://www.tek-tips.com/viewthread.cfm?qid=286147

C#.NET & Translation of external component

I'm maintaining one program written in C# 2.0 (VS2005). It has pretty large codebase, lot of authors, it is almost internal app, but currently it is also one of our customers using it.
App is multilingual and translation of own forms and components works OK. But, there is one component - DockPanel Suite by WeifenLuo and I need to translate it to another language (zh-CN) - one of the chinese guys translated resource file to his language and now I'm trying to include and use in application, but I'm failing in it - although whole app is in chinese, this component remains in english. The untranslated resource file can be found on github: https://github.com/dockpanelsuite/dockpanelsuite/blob/master/WinFormsUI/Docking/Strings.resx
How to do that? I tried almost everything, naive approach (just resgen and compile by al, and trying to use it as satellite assembly - also tried ilmerge), then opening DockPanelSuite in VS2013 Express, adding resx as Strings.zh-CN.resx, but nothing works and tooltips and others are still in english.
Tried also stepping-in with debugger, but debugger broke at tooltip = Strings.DockPaneCaption_ToolTipAutoHide but it didn't step into getter defined in Strings.Designer.cs
I'm stuck and I don't know, how to do that. Any idea? Thanks very much!
I was able to translate a label in the demo application in a very simple process:
git clone the library
Copy & paste the Strings.resx file
Rename the copy into Strings.pt-BR.resx
Compile, it's done (in my case, I translated the "Close" label)
however, this project contains many Strings.resx files
Did you change all of them? Or did you just change one? (maybe a wrong one, like I did in my first try)

How does one inject resources into an already built executable

I am working on a windows application that will need to be branded. The client will be selling this to other businesses, and needs a customized logo and name for each sale.
The client does not know how to use visual studio!
I think I need to write a packager app to inject custom logo and string resources into the executable. I am planning on using WPF. But since this is a critical requirement, I'd be willing to do it in winforms if that is easier.
What is the best way to do this? Any and all suggestions welcome.
It sounds like what you are after is application skinning. This doesn't mean you have to unpack the exe and inject resources. You just need to consider skinning from the start of the project and build the application to support your skinning requirements.
WPF will make skinning your app much easier. There will be several different ways to accomplish what you want.
Simplest is to leave the logo image loose and reference it with a relative path from the XAML file(s) that need to show this image.
You should look into Resource Dictionaries in WPF and how they help you group resources and support skinning. http://msdn.microsoft.com/en-us/library/ms750613.aspx
The text will be a little different but I am not sure what you need as far as a text goes. Do you mean you need to localize the strings or do you simply need different text (all the same locale) to show for different clients?
One possible solution (perhaps not the simplest one) is to use a parent application which compiles source code for generating child application. You can do it with CSharpCodeProvider and CompilerParameters classes. Add the image as an embedded resource and retrieve it in the child application. A working demo with a source code is available at Slide Show Builder.
My best suggestion for the exact question you asked (although I suspect there is another way by reconsidering the exact requirements) would be to write a utility which uses ildasm to disassemble the assembly, then use ilasm to reassemble it and include your new resource file.
http://msdn.microsoft.com/en-us/library/496e4ekx%28VS.71%29.aspx
The trivial solution is to provide the bitmap along with the EXE as a separate file. Actually replacing an embedded resource in the EXE requires decompiling it with ildasm.exe and putting it back together with ilasm.exe. Ildasm.exe is only available in the Windows SDK, it can be downloaded separately. Error prone and small odds that your customer can get that right, you'll need to provide them with, say, a .bat file that does this.
Of course, whomever is interested in replacing the logo, for whatever reason, would not be slowed down by replacing either the separate image file or using the Ildasm.exe trick. There is therefore very little point in making it any more complicated then it needs to be.

Proper localization of a WinForms application

I have a WinForms application which I want to translate into multiple languages. However, I do not have any experience with localizing a WinForms app, and I find very contradictory information about this subject.
Basically, what I want is:
In the source code, I want only one file per language
This file gets compiled into the main application on compilation - no satellite assemblies or external data files after building the application
The user can select the language, I do not need/want auto-detection based on the operating system
This should mainly contain strings and ints, but also a CultureInfo
Most solutions I've seen either have one .resx file per Form and/or external satellite assemblies.
Do I have to roll my own?
Or is there something in the framework already?
.net Framework 3.5 SP1 if that matters.
Edit:
For the most part, Visual Studio already offers support for what I want, but there are two issues. When I set Form.Localizable to true I have this nice Designer support, but this generates one resx per Form. The idea of manually overriding it in InitializeComponent fails because it's designer-written code that will regularly be overwritten.
Theoretically, I only want to :
a) override the creation of the ComponentResourceManager to point it to my global resx and
b) change the call to ApplyResources to the overload that takes a CultureInfo as third parameter.
It seems as if I have to add a function call to my constructor that gets called after InitializeComponent() and overrides its behaviour. That seems terribly inefficient, but Visual Studio is right when it warns about touching InitializeComponent().
At the moment, I am indeed rolling my own WinForms localization Framework...
I've just completed a C# .Net 3.5 project with a similar problem. We were writing WinForms plugin for an existing multi-lingual application with 8 languages (including English).
This is how we did it:
Create all our forms and UI in the default language, English.
Put all our internal strings in a resource file (stuff not tied directly to a form like custom error messages and dialog box titles etc)
Once we had completed most of the work and testing we localised it.
Each form already had a .resx file but this was empty. We set the property 'Localizable' to true, the .resx file was filled with things like button sizes & strings.
For each of the other languages, we changed the 'Language' property on the form. We chose the basic version of each language eg: 'Spanish' instead of 'Spanish (Chile)' etc. so that it would work for every 'Spanish' dialect, I think.
Then we went through each control, translated its text and resized, if needed. This created a .resx per language and form combination.
We were then left with, for 8 languages, 8 .resx for each form and 8 .resx for the general strings. When compiled the output folder had the .dll we were creating and then a sub folder for each language with a .resources.dll in it.
We were able to test the versions of the UI in the designer by just changing the language property to check that we had the correct strings & layout.
All in all once we got our heads around it, it was quite easy and painless.
We didn't need to write any custom tweaks to the form loading
I was asking a similar question about ASP.NET and got a first answer - this tool and its workflow might also be something for you - have a look: Lingobit Localizer
It seems to be able to load your Winforms app and allows you to start translating your labels etc. and see the forms while you do it. Lots of other features, too, like incremental translation and translation memory (if you use the same terms over and over again).
Looks quite promising (for Winforms) - haven't used it myself, though.
Here's an extensive list of potential .NET localization tools - not sure, how well they work and what they cover - have a look, maybe you'll find what you're looking for.
Marc
I dont have a solution for your first and second requirement but keep in mind that localizing a form is not as simple as translating each word. You need to check that each translated text fits in their respective control. Also, maybe you have an icon or an image which need to be change in another culture.
For your point three, you can change the language manually with the following lines:
CultureInfo ci = new CultureInfo("fr");
Thread.CurrentThread.CurrentUICulture = ci;
This is a huge subject and there are many ways to accomplish what you want. The framework does provide the basis but a complete solution requires that you implement certain elements yourself.
For example the default framework implementation is to create a .resx file for every resource. In ASP.Net this means each user/server control or page. This doesn't lend itself to easy maintenance and if you want to move resources to a database you need to implement your own provider.
My familiarity with Winforms is limited but if you are using Silverlight or WPF then have a read of Guy Smith-Ferrier's work on the subject at: http://www.guysmithferrier.com/category/Internationalization.aspx. He also has some toolsets that can make your life easier at: http://www.dotneti18n.com/Downloads.aspx.
I've worked with him before and have never come across anyone else with a better depth of understanding of the subject.
What you are asking for:
no satellite resource files
only one size and control placement per form.
lots of languages embedded in the executable.
Is not do-able in vanilla Visual Studio's IDE.
What it would require is some custom work, basically fulfilling all these steps:
Acquire a custom resource manager that handles TMX resource files.
Put all your localizable strings in a TMX file.
Make this TMX file an embedded resource in your project.
In your Form constructor, create your TMX ResourceManager, loading the TMX file from your embedded resources.
In your code, use your tmx ResourceManager instead of the default ResourceManager for getting localized strings.
Let the Form use the default ResourceManager for getting all the designer things except the strings.
Get your TMX file fleshed out with the new language translations.
More can be added in the next release of your project, just by adding them to this TMX file before you compile.
RESOURCES: (not an exhaustive list, by any means)
http://en.wikipedia.org/wiki/Translation_Memory_eXchange
http://sourceforge.net/projects/tmx-editor/
The right way to do this is, suppose you want to add Arabic support witch is RightToLeft language:
Double click the form
Set localizable prop. to true
Change Language prop. to Arabic //This will automatically open a new version of the form so you can customize.
Set RightToLeft prop. to Yes
Set RightToLeftLayout prop. to True
Start renaming controls, and save the form.
Handle Messages/Errors in code // Sorry I don't have a quick solution for now, try duplicate them and If/Else the current local.

How To Store Files In An EXE

Alright, so I'm working on programming my own installer in C#, and what I'd like to do is something along the lines of put the files in the .exe, so I can do
File.Copy(file, filedir);
Or, if this isn't possible, is there another way of doing what I am attempting to do?
I wouldn't code my own installer, but if you truely want to embed files into your assembly you could use strongly typed resources. In the properties dialog of your project open up the "Resources" tab and then add your file. You'll then be able to get the file using:
ProjectNamespace.Properties.Resources.MyFile
Then you'll be able to write the embedded resource to disk using:
System.IO.File.WriteAllBytes(#"C:\MyFile.bin", ProjectNamespace.Properties.Resources.MyFile);
Honestly, I would suggest you NOT create your own installer. There are many many issues with creating installers. Even the big installer makers don't make their own actual installers anymore, they just create custom MSI packages.
Use Mirosoft Installer (MSI). It's the right thing to do. Make your own custom front-end for it, but don't recreate the already very complex wheel that exists.
UPDATE: If you're just doing this for learning, then I would shy away from thinking of it as "an installer". You might be tempted to take your "research" and use it someday, and frankly, that's how we end up with so many problems when new versions of Windows come out. People create their own wheels with assumptions that aren't valid.
What you're really trying to do is called "packaging", and you really have to become intimately familiar with the Executable PE format, because you're talking about changing the structure of the PE image on disk.
You can simulate it, to a point, with putting files in resources, but that's not really what installers, or self-extractors do.
Here's a link to Self-Extractor tutorial, but it's not in C#.
I don't know enough about the .NET PE requirements to know if you can do this in with a managed code executable or not.
UPDATE2: This is probably more of what you're looking for, it embeds files in the resource, but as I said, it's not really the way professional installers or self-extractors do it. I think there are various limitations on what you can embed as resources. But here's the like to a Self-Extractor Demo written in C#.
I'm guessing here, but if you are trying to store resources in your application before compilation, you can in the Project Explorer, right click a file you would like to add, chose properties and change the type to Embedded Resource.
You can then access the embedded resources later by using the instructions from this KB:
http://support.microsoft.com/kb/319292
in case you simply want to store multiple files in a single file storage (and extract files from there, interact etc.) you might also want to check out NFileStorage, a .net file storage. written in 100% .NET C# with all sources included. It also comes with a command line interpreter that allows interaction from the command line.

Categories

Resources