OWASP O2 Platform Blog

Using Reflection to (try to) set .NET default proxy settings

As a variation of Dealing with “The server committed a protocol violation. Section=ResponseStatusLine” here a number of scripts that show how to use O2’s refection API to gain access to internal (to the .NET Framework) properties and fields

Example: get fields that exist inside the System.Net.Configuration.SettingsSectionInternal type

return "System".assembly().type("SettingsSectionInternal")
.prop("Section").type().fields();

Example: get the current System.Net.Configuration.DefaultProxySectionInternal which maps to the *.appconfig mapping of

//<system.net>
//  <defaultProxy
//  ....
// </defaultProxy>
//</system>
"System".assembly().type("DefaultProxySectionInternal").invokeStatic("GetSection");

Example: get the list of fields that exist inside DefaultProxySectionInternal

var proxySection = "System".assembly().type("DefaultProxySectionInternal").invokeStatic("GetSection");
return proxySection.type().fields();

Example: geting the webProxy field value and figuring out its type (which in this case is WebProxyWrapper):

var proxySection = "System".assembly().type("DefaultProxySectionInternal").invokeStatic("GetSection");
return proxySection.field("webProxy").typeName();

Example: listing the properties of the WebProxyWrapper live object

var proxySection = "System".assembly().type("DefaultProxySectionInternal").invokeStatic("GetSection");
return proxySection.field("webProxy").type().properties();

Example: getting the WebProxy value from the WebProxyWrapper class, which finallly give us a public class (System.Net.WebProxy) which we can direcly manipulate

var proxySection = "System".assembly().type("DefaultProxySectionInternal").invokeStatic("GetSection");
return proxySection.field("webProxy").prop("WebProxy");

Example: wrapping code in easy to consume function that returns a live object of the WebProxy class

Func<System.Net.WebProxy> getWebProxy =
()=>{
var proxySection = "System".assembly().type("DefaultProxySectionInternal").invokeStatic("GetSection");
var webProxy = (System.Net.WebProxy) proxySection.field("webProxy").prop("WebProxy");
return webProxy;
};

return getWebProxy();

Note: this technique didn’t work. See this StackOverflow question for more details:
http://stackoverflow.com/questions/4090151/progamattically-set-appconfig-proxy-settings  (this question was removed from StackOverflow)

November 3, 2010 - Posted by | .NET

2 Comments »

  1. stackoverflow link not found…

    Comment by kiquenet | December 26, 2011 | Reply

    • Thanks, yeah not sure what happened there. Also just tried to find it on google cache and it was not there either

      Comment by Dinis Cruz | January 13, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: