Wednesday, July 24, 2013

Reverse engineering your .NET applications

The .NET Framework makes it easy to reverse engineer an existing application. Discover what techniques to use to deter prying eyes from deconstructing your code. 
A recent project entailed making changes to an existing Windows C# application with the caveat the source code was unavailable (the previous developer refused to provide it). Most developers have experienced a similar situation where the source code cannot be located; sometimes the source code disappears or is lost.
The process of reverse engineering or decompiling an application can be done with the right tools and persistence when working with languages like C++ or C, but it's not a straightforward process. The managed code (.NET and Java) world provides a much easier approach, as all source code is compiled into Microsoft Intermediate Language (MSIL). The MSIL is converted to machine code by a just-in-time (JIT) compiler when it is executed. The MSIL is well-documented. MSIL includes metadata that provides a wealth of information on the code. For this post, I look at working backwards with a C# application.

Getting started

The starting point for reverse engineering is getting the DLL or EXE files of which you want to learn the inner workings. The next step is locating a tool for decompiling the files (this may be first step, but why do you need a tool without the files?). There are a number of tools available, but the .NET Framework comes with its own tool called Ildasm (MSIL Disassembler). It is installed as part of the Visual Studio and Windows SDKs, so you can access it via the Visual Studio command prompt or the Windows SDK command prompt.
Ildasm takes a file containing MSIL as input (a DLL or EXE file) and outputs source code suitable for the assembler tool (Ildasm.exe). The following command line takes the dll file as input and generates the specified output file (which contains MSIL).
Ildasm compiledFile.dll /output:TRTest.il
Here is a snippet of the MSIL generated by the previous Ildasm command.
.module compiledFile.dll
// MVID: {F5233C6F-701D-4E6B-BA37-ECCAA0B22083}
.imagebase 0x11000000
.file alignment 0x00001000
.stackreserve 0x00100000
.subsystem 0x0003       // WINDOWS_CUI
.corflags 0x00000001    //  ILONLY
// Image base: 0x03370000
// =============== CLASS MEMBERS DECLARATION ===================
.class interface public abstract auto ansi compiledFile.IUpdatableControl
{
.method public hidebysig newslot specialname abstract virtual
instance bool  get_UpdateAfterCallBack() cil managed
{
} // end of method IUpdatableControl::get_UpdateAfterCallBack
.method public hidebysig newslot specialname abstract virtual
instance void  set_UpdateAfterCallBack(bool 'value') cil managed
{
} // end of method IUpdatableControl::set_UpdateAfterCallBack
.method public hidebysig newslot specialname abstract virtual
instance bool  get_AutoUpdateAfterCallBack() cil managed
{
} // end of method IUpdatableControl::get_AutoUpdateAfterCallBack
.method public hidebysig newslot specialname abstract virtual
instance void  set_AutoUpdateAfterCallBack(bool 'value') cil managed
{
} // end of method IUpdatableControl::set_AutoUpdateAfterCallBack
This is rather difficult to read and analyze, but it is possible. You can make changes in the MSIL and send it through the assembler (Ildasm.exe).

Exploring other options

I've used other tools that simplify the process; I prefer Red Gate's Reflector, but it's no longer free -- it now requires purchase after a trial period. However, it handles decompiling code and presents the results in an easy to read interface. Figure A shows the results for one DLL. The bottom right portion of the interface includes analysis results where it shows you dependencies.Figure A
The Reflector interface simplifies reverse engineering code. (Click the image to enlarge.)
Here are three more available options, all of which are free:
  • Dotnet IL Editor (DILE) lets you disassemble and debug .NET code.
  • dotPeek lets you decompile and browse the results.
  • ILSpy is an open source assembly browser and decompiler.
This is only a sampling of the available tools.

Installing roadblocks

While you cannot totally stop others from deconstructing application files/code, you can deter them by using a number of techniques. You can:
  • Use strong-named assemblies that incorporate digital signatures and public keys.
  • Use Microsoft Authenticode.
  • Avoid the all-too-common practice of shipping debug versions of an application. These versions include a wealth of debugging-related information that is beneficial in the reverse engineering process.
  • Consider using obfuscation, which converts your code and symbols into incoherent data while retaining logic. Microsoft offers the Dotfuscator tool for obfuscation, but third-party tools from vendors such as Red Gate are even better.

Summary

Whatever the business reason is for reverse engineering code, it is a good skill to be able to use in your everyday work as needed.

Glimpse - A nice tool for ASP.NET for debugging and profiling

I recently got an answer to my google about debugging and profiling in ASP.Net.

Thanks to SCOTT HANSELMAN to write a good article on 'Glimpse' [now sponsored by The Red Gate company].

This article helped me a lot. I thought to share with you friends as it may help you, too.

New Interview Questions for Senior Software Engineers

  • What is something substantive that you've done to improve as a developer in your career?
  • Would you call yourself a craftsman (craftsperson) and what does that word mean to you?
  • Implement a <basic data structure> using <some language> on <paper|whiteboard|notepad>.
  • What is SOLID?
  • Why is the Single Responsibility Principle important?
  • What is Inversion of Control? How does that relate to dependency injection?
  • How does a 3 tier application differ from a 2 tier one?
  • Why are interfaces important?
  • What is the Repository pattern? The Factory Pattern? Why are patterns important?
  • What are some examples of anti-patterns?
  • Who are the Gang of Four? Why should you care?
  • How do the MVP, MVC, and MVVM patterns relate? When are they appropriate?
  • Explain the concept of Separation of Concerns and it's pros and cons.
  • Name three primary attributes of object-oriented design. Describe what they mean and why they're important.
  • Describe a pattern that is NOT the Factory Pattern? How is it used and when?
  • You have just been put in charge of a legacy code project with maintainability problems. What kind of things would you look to improve to get the project on a stable footing?
  • Show me a portfolio of all the applications you worked on, and tell me how you contributed to design them.
  • What are some alternate ways to store data other than a relational database? Why would you do that, and what are the trade-offs?
  • Explain the concept of convention over configuration, and talk about an example of convention over configuration you have seen in the wild.
  • Explain the differences between stateless and stateful systems, and impacts of state on parallelism.
  • Discuss the differences between Mocks and Stubs/Fakes and where you might use them (answers aren't that important here, just the discussion that would ensue).
  • Discuss the concept of YAGNI and explain something you did recently that adhered to this practice.
  • Explain what is meant by a sandbox, why you would use one, and identify examples of sandboxes in the wild.
  • Concurrency
    • What's the difference between Locking and Lockless (Optimistic and Pessimistic) concurrency models?
    • What kinds of problems can you hit with locking model? And a lockless model?
    • What trade offs do you have for resource contention?
    • How might a task-based model differ from a threaded model?
    • What's the difference between asynchrony and concurrency?
  • Are you still writing code? Do you love it?
  • You've just been assigned to a project in a new technology how would you get started?
  • How does the addition of Service Orientation change systems? When is it appropriate to use?
  • What do you do to stay abreast of the latest technologies and tools?
  • What is the difference between "set" logic, and "procedural" logic. When would you use each one and why?
  • What Source Control systems have you worked with?
  • What is Continuous Integration?  Have you used it and why is it important?
  • Describe a software development life cycle that you've managed.
  • How do you react to people criticizing your code/documents?
  • Whose blogs or podcasts do you follow? Do you blog or podcast?
  • Tell me about some of your hobby projects that you've written in your off time.
  • What is the last programming book you read?
  • Describe, in as much detail as you think is relevant, as deeply as you can, what happens when I type "cnn.com" into a browser and press "Go".
  • Describe the structure and contents of a design document, or a set of design documents, for a multi-tiered web application.
  • What's so great about <cool web technology of the day>?
  • How can you stop your DBA from making off with a list of your users’ passwords?
  • What do you do when you get stuck with a problem you can't solve?
  • If your database was under a lot of strain, what are the first few things you might consider to speed it up?
  • What is SQL injection?
  • What's the difference between unit test and integration test?
  • Tell me about 3 times you failed.
  • What is Refactoring ? Have you used it and it is important? Name three common refactorings.
  • You have two computers, and you want to get data from one to the other. How could you do it?
  • Left to your own devices, what would you create?
  • Given Time, Cost, Client satisfaction and Best Practices, how will you prioritize them for a project you are working on? Explain why.
  • What's the difference between a web server, web farm and web garden? How would your web application need to change for each?
  • What value do daily builds, automated testing, and peer reviews add to a project? What disadvantages are there?
  • What elements of OO design are most prone to abuse? How would you mitigate that?
  • When do you know your code is ready for production?
  • What's YAGNI? Is this list of questions an example?
  • Describe to me some bad code you've read or inherited lately.

ASP.NET Interview Questions

This post is is continue series of my last post.

I do a LOT of interviewing here, and for a while we were hiring ASP.NET people.  Here's some of the questions that I asked them.  I came up with these questions because you'd "just know" this stuff if you spent time working on a REAL WORLD ASP.NET site - through design, development, debugging, production debugging, and deployment.
Do they suck? Did I miss any?  How do you think people did?
  • From constructor to destructor (taking into consideration Dispose() and the concept of non-deterministic finalization), what the are events fired as part of the ASP.NET System.Web.UI.Page lifecycle. Why are they important? What interesting things can you do at each?
  • What are ASHX files?  What are HttpHandlers?  Where can they be configured?
  • What is needed to configure a new extension for use in ASP.NET? For example, what if I wanted my system to serve ASPX files with a *.jsp extension?
  • What events fire when binding data to a data grid? What are they good for?
  • Explain how PostBacks work, on both the client-side and server-side. How do I chain my own JavaScript into the client side without losing PostBack functionality?
  • How does ViewState work and why is it either useful or evil?
  • What is the OO relationship between an ASPX page and its CS/VB code behind file in ASP.NET 1.1? in 2.0?
  • What happens from the point an HTTP request is received on a TCP/IP port up until the Page fires the On_Load event?
  • How does IIS communicate at runtime with ASP.NET?  Where is ASP.NET at runtime in IIS5? IIS6?
  • What is an assembly binding redirect? Where are the places an administrator or developer can affect how assembly binding policy is applied?
  • Compare and contrast LoadLibrary(), CoCreateInstance(), CreateObject() and Assembly.Load().

What Great .NET Developers Ought To Know (More .NET Interview Questions)

A while back, I posted a list of ASP.NET Interview QuestionsConventional wisdom was split, with about half the folks saying I was nuts and that it was a list of trivia. The others said basically "Ya, those are good. I'd probably have to look a few up." To me, that's the right response.
Certainly I wasn't trying to boil all of .NET Software Development down to a few simple "trivia" questions. However, I WAS trying to get folks thinking. I believe that really good ASP.NET (and for that matter, WinForms) is a little [read: lot] more than just draging a control onto a designer and hoping for the best. A good race driver knows his car - what it can do and what it can't.
So, here's another list...a greatly expanded list, for your consumption (with attribution). I wrote this on a plane last week on the way from Boise to Portland. I tried to take into consideration the concerns that my lists contain unreasonable trivia. I tried to make a list that was organized by section. If you've never down ASP.NET, you obviously won't know all the ASP.NET section. If you're an indenpendant consultant, you may never come upon some of these concepts. However, ever question here has come up more than once in the last 4 years of my time at Corillian. So, knowing groking these questions may not make you a good or bad developer, but it WILL save you time when problems arise. 
What Great .NET Developers Ought To Know
Everyone who writes code
  • Describe the difference between a Thread and a Process?
  • What is a Windows Service and how does its lifecycle differ from a "standard" EXE?
  • What is the maximum amount of memory any single process on Windows can address? Is this different than the maximum virtual memory for the system? How would this affect a system design?
  • What is the difference between an EXE and a DLL?
  • What is strong-typing versus weak-typing? Which is preferred? Why?
  • Corillian's product is a "Component Container." Name at least 3 component containers that ship now with the Windows Server Family.
  • What is a PID? How is it useful when troubleshooting a system?
  • How many processes can listen on a single TCP/IP port?
  • What is the GAC? What problem does it solve?
Mid-Level .NET Developer
  • Describe the difference between Interface-oriented, Object-oriented and Aspect-oriented programming.
  • Describe what an Interface is and how it’s different from a Class.
  • What is Reflection?
  • What is the difference between XML Web Services using ASMX and .NET Remoting using SOAP?
  • Are the type system represented by XmlSchema and the CLS isomorphic?
  • Conceptually, what is the difference between early-binding and late-binding?
  • Is using Assembly.Load a static reference or dynamic reference?
  • When would using Assembly.LoadFrom or Assembly.LoadFile be appropriate?
  • What is an Asssembly Qualified Name? Is it a filename? How is it different?
  • Is this valid? Assembly.Load("foo.dll");
  • How is a strongly-named assembly different from one that isn’t strongly-named?
  • Can DateTimes be null?
  • What is the JIT? What is NGEN? What are limitations and benefits of each?
  • How does the generational garbage collector in the .NET CLR manage object lifetime? What is non-deterministic finalization?
  • What is the difference between Finalize() and Dispose()?
  • How is the using() pattern useful? What is IDisposable? How does it support deterministic finalization?
  • What does this useful command line do? tasklist /m "mscor*"
  • What is the difference between in-proc and out-of-proc?
  • What technology enables out-of-proc communication in .NET?
  • When you’re running a component within ASP.NET, what process is it running within on Windows XP? Windows 2000? Windows 2003?
Senior Developers/Architects
  • What’s wrong with a line like this? DateTime.Parse(myString);
  • What are PDBs? Where must they be located for debugging to work?
  • What is cyclomatic complexity and why is it important?
  • Write a standard lock() plus “double check” to create a critical section around a variable access.
  • What is FullTrust? Do GAC’ed assemblies have FullTrust?
  • What benefit does your code receive if you decorate it with attributes demanding specific Security permissions?
  • What does this do? gacutil /l | find /i "Corillian"
  • What does this do? sn -t foo.dll
  • What ports must be open for DCOM over a firewall? What is the purpose of Port 135?
  • Contrast OOP and SOA. What are tenets of each?
  • How does the XmlSerializer work? What ACL permissions does a process using it require?
  • Why is catch(Exception) almost always a bad idea?
  • What is the difference between Debug.Write and Trace.Write? When should each be used?
  • What is the difference between a Debug and Release build? Is there a significant speed difference? Why or why not?
  • Does JITting occur per-assembly or per-method? How does this affect the working set?
  • Contrast the use of an abstract base class against an interface?
  • What is the difference between a.Equals(b) and a == b?
  • In the context of a comparison, what is object identity versus object equivalence?
  • How would one do a deep copy in .NET?
  • Explain current thinking around IClonable.
  • What is boxing?
  • Is string a value type or a reference type?
  • What is the significance of the "PropertySpecified" pattern used by the XmlSerializer? What problem does it attempt to solve?
  • Why are out parameters a bad idea in .NET? Are they?
  • Can attributes be placed on specific parameters to a method? Why is this useful?
C# Component Developers
  • Juxtapose the use of override with new. What is shadowing?
  • Explain the use of virtual, sealed, override, and abstract.
  • Explain the importance and use of each component of this string: Foo.Bar, Version=2.0.205.0, Culture=neutral, PublicKeyToken=593777ae2d274679d
  • Explain the differences between public, protected, private and internal.
  • What benefit do you get from using a Primary Interop Assembly (PIA)?
  • By what mechanism does NUnit know what methods to test?
  • What is the difference between: catch(Exception e){throw e;} and catch(Exception e){throw;}
  • What is the difference between typeof(foo) and myFoo.GetType()?
  • Explain what’s happening in the first constructor: public class c{ public c(string a) : this() {;}; public c() {;} } How is this construct useful?
  • What is this? Can this be used within a static method?
ASP.NET (UI) Developers
  • Describe how a browser-based Form POST becomes a Server-Side event like Button1_OnClick.
  • What is a PostBack?
  • What is ViewState? How is it encoded? Is it encrypted? Who uses ViewState?
  • What is the <machinekey> element and what two ASP.NET technologies is it used for?
  • What three Session State providers are available in ASP.NET 1.1? What are the pros and cons of each?
  • What is Web Gardening? How would using it affect a design?
  • Given one ASP.NET application, how many application objects does it have on a single proc box? A dual? A dual with Web Gardening enabled? How would this affect a design?
  • Are threads reused in ASP.NET between reqeusts? Does every HttpRequest get its own thread? Should you use Thread Local storage with ASP.NET?
  • Is the [ThreadStatic] attribute useful in ASP.NET? Are there side effects? Good or bad?
  • Give an example of how using an HttpHandler could simplify an existing design that serves Check Images from an .aspx page.
  • What kinds of events can an HttpModule subscribe to? What influence can they have on an implementation? What can be done without recompiling the ASP.NET Application?
  • Describe ways to present an arbitrary endpoint (URL) and route requests to that endpoint to ASP.NET.
  • Explain how cookies work. Give an example of Cookie abuse.
  • Explain the importance of HttpRequest.ValidateInput()?
  • What kind of data is passed via HTTP Headers?
  • Juxtapose the HTTP verbs GET and POST. What is HEAD?
  • Name and describe at least a half dozen HTTP Status Codes and what they express to the requesting client.
  • How does if-not-modified-since work? How can it be programmatically implemented with ASP.NET?
    Explain <@OutputCache%> and the usage of VaryByParam, VaryByHeader.
  • How does VaryByCustom work?
  • How would one implement ASP.NET HTML output caching, caching outgoing versions of pages generated via all values of q= except where q=5 (as in http://localhost/page.aspx?q=5)?
Developers using XML
  • What is the purpose of XML Namespaces?
  • When is the DOM appropriate for use? When is it not? Are there size limitations?
  • What is the WS-I Basic Profile and why is it important?
  • Write a small XML document that uses a default namespace and a qualified (prefixed) namespace. Include elements from both namespace.
  • What is the one fundamental difference between Elements and Attributes?
  • What is the difference between Well-Formed XML and Valid XML?
  • How would you validate XML using .NET?
  • Why is this almost always a bad idea? When is it a good idea? myXmlDocument.SelectNodes("//mynode");
  • Describe the difference between pull-style parsers (XmlReader) and eventing-readers (Sax)
  • What is the difference between XPathDocument and XmlDocument? Describe situations where one should be used over the other.
  • What is the difference between an XML "Fragment" and an XML "Document."
  • What does it meant to say “the canonical” form of XML?
  • Why is the XML InfoSet specification different from the Xml DOM? What does the InfoSet attempt to solve?
  • Contrast DTDs versus XSDs. What are their similarities and differences? Which is preferred and why?
  • Does System.Xml support DTDs? How?
  • Can any XML Schema be represented as an object graph? Vice versa?

Create Single Page Applications with ASP.NET and Visual Studio 2012.2

The evolution of JavaScript, CSS, and HTML makes Single Page Applications (SPAs) much more powerful ways to build an application. Give the latest Visual Studio 2012 SPA templates a try. 
Every time you work with Gmail, you're using a Single Page Application (SPA), which offers a rich user experience within a single page. Navigation techniques and AJAX provide necessary functionality without a page reload. A recent Visual Studio update includes templates for building SPAs with ASP.NET.

The basics about SPAs

Before we dive into the ASP.NET offerings, let's take a quick tour of the key ingredients of a SPA, along with more examples that thoroughly embrace the concept.
While the SPA approach has been with us for a long time, it has only recently taken off due to the maturation of web standard technology threesome JavaScriptCSS, and HTML. All of these can be used to build full-featured user interfaces consisting of one web page -- navigate to different content within the page using one or more of the technologies.
You may be thinking that you know of all of this, but why would you ever build an application consisting of only one page? One of the main reasons is the rise of mobile platforms -- nobody likes to navigate multiple pages on their mobile device. Another benefit is performance (if designed correctly), as a rich user experience is provided while reducing the number of server round-trips. There will still be a lot of resources (CSS, images, JavaScript, etc.) to load, so a key is to load most everything (or what is immediately needed) up front to minimize wait time. Also, the SPA approach lends itself to creating silos within a Web application -- that is, creating different areas within a site as SPA.
While SPA is described as "single page," it doesn't mean there is only one page or content screen; you can utilize multiple data views within a page -- show/hide as needed, load data via AJAX, etc.
An excellent example of the SPA concept is FreeTheChicken.org, which uses scrolling to deliver multiple screens of content all contained within one page request. The Hungree site is another good example (although I cannot read it without a translator) -- I love the simple design.

SPA via ASP.NET

The SPA concept uses core web technologies and standards, so it is easily accomplished via ASP.NET. You can build everything from scratch, but ASP.NET and Web Tools 2012.2download provides SPA templates (and many more updates), so you don't have to reinvent the wheel when tackling a project. There are four templates available.
  • ASP.NET SPA template: A basic SPA application with one view. It uses standard ASP.NET along with the Knockout JavaScript library. It is a good starting point for learning SPA.
  • BreezeJS SPA template: This template extends the standard ASP.NET SPA template by using the Breeze open source JavaScript library, which provides an easy way to manage rich data.
  • Durandal SPA template: This template utilizes Durandal, an open source JavaScript libraryfor building rich client applications. This template proves the basics for navigation-style SPA.
  • Hot Towel SPA template: This is a more comprehensive template that provides the foundation for building an SPA with rich client features. It is styled using Bootstrap, and it uses a number of JavaScript libraries, including Breeze, Durandal, Knockout, andRequireJS.
These templates are easily accessed in Visual Studio 2012 (with ASP.NET and Web Tools 2012.2 installed). If you don't have the templates installed, you can still create a simple SPA by creating a new MVC 4 Web Application and choosing Single Page Application. However, the templates provide much more functionality. Each template has a Scripts folder that contains all of the necessary JavaScript files (your custom code goes in the app subdirectory). You'll notice the Scripts folder is full of files for frameworks like jQuery, Knockout, and Modernizr and the other libraries used (depending on the selected template).
The templates are an excellent SPA starting point. We'll take a closer look at some of these templates and their technologies in future articles.

Go for a test drive

One of the great (and often irritating) aspects of web development is its fluidity -- things are always changing. Years ago, table-based page layouts were seen as good solutions to layout problems, and now the SPA concept is big with developers building applications for multiple platforms. Who knows what the next new thing will be, but for now, we embrace the trend and use the available technologies.
SPAs allow you to create a dynamic user experience with a (hopefully) smaller number of server round-trips. Take the latest Visual Studio 2012 SPA templates for a test drive to gain a better understanding of how to build such applications using ASP.NET.

SQL SERVER – Check If Column Exists in SQL Server Table

A very frequent task among SQL developers is to check if any specific column exists in the database table or not. Based on the output developers perform various tasks. Here arecouple of simple tricks which you can use to check if column exists in your database table or not.

Method 1

IF EXISTS(SELECT FROM sys.columnsWHERE Name N'columnName' AND OBJECT_ID OBJECT_ID(N'tableName'))BEGIN
PRINT 
'Your Column Exists'END  
For AdventureWorks sample database
IF EXISTS(SELECT FROM sys.columnsWHERE Name N'NameAND OBJECT_ID OBJECT_ID(N'[HumanResources].[Department]'))BEGIN
PRINT 
'Your Column Exists'END  

Method 2

IF COL_LENGTH('table_name','column_name'IS NOT NULLBEGIN
PRINT 
'Your Column Exists'END
For AdventureWorks sample database
IF COL_LENGTH('[HumanResources].[Department]','Name'IS NOT NULLBEGIN
PRINT 
'Your Column Exists'END

Method 3

IF EXISTS(SELECT TOP *FROM INFORMATION_SCHEMA.COLUMNSWHERE [TABLE_NAME] 'TableName'AND [COLUMN_NAME] 'ColumnName'AND [TABLE_SCHEMA] 'SchemaName')BEGIN
PRINT 
'Your Column Exists'END
For AdventureWorks sample database
IF EXISTS(SELECT TOP *FROM INFORMATION_SCHEMA.COLUMNSWHERE [TABLE_NAME] 'Department'AND [COLUMN_NAME] 'Name'AND [TABLE_SCHEMA] 'HumanResources')BEGIN
PRINT 
'Your Column Exists'END
Let me know if you know any other method to find if Column Exists in SQL Server Table.
Thanks to Pinal Dave.

Find a cool free stuff everyday

Giveaway of the Day

Hiren Bharadwa's Posts

DotNetJalps