Tag: Hypertext Transfer Protocol

MVC ASP.NET and Microsoft Translator

Introduction

The Internet is moving towards restful services, where we use the HTTP protocol to get information from services. If we just need to send some values into the service and get a return back, then we can use a GET HTTP request. A good example of this is with Microsoft Translator, where we can call up the Translator API with a text value, and with the arguments of the input and the output languages (such as en for English and fr for French). In this tutorial I will illustrate how we can integrate Microsoft Translator into an ASP.NET MVC site. If you want to view the result it is here, which uses both a text translator, and a speech convertor.

Getting the Authentication Token

In order to access the service, we must first create an authentication token, which, once granted, is added to the HTTP Web request. The standard code to use is:

[DataContract]
        public class AdmAccessToken
        {
            [DataMember]
            public string access_token { get; set; }
            [DataMember]
            public string token_type { get; set; }
            [DataMember]
            public string expires_in { get; set; }
            [DataMember]
            public string scope { get; set; }
        }
        public class AdmAuthentication
        {
            public static readonly string DatamarketAccessUri = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
            private string clientId;
            private string cientSecret;
            private string request;

            public AdmAuthentication(string clientId, string clientSecret)
            {
                this.clientId = clientId;
                this.cientSecret = clientSecret;
                //If clientid or client secret has special characters, encode before sending request
                this.request = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(clientId), HttpUtility.UrlEncode(clientSecret));
            }

            public AdmAccessToken GetAccessToken()
            {
                return HttpPost(DatamarketAccessUri, this.request);
            }

            private AdmAccessToken HttpPost(string DatamarketAccessUri, string requestDetails)
            {
                //Prepare OAuth request 
                WebRequest webRequest = WebRequest.Create(DatamarketAccessUri);
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.Method = "POST";
                byte[] bytes = Encoding.ASCII.GetBytes(requestDetails);
                webRequest.ContentLength = bytes.Length;
                using (Stream outputStream = webRequest.GetRequestStream())
                {
                    outputStream.Write(bytes, 0, bytes.Length);
                }
                using (WebResponse webResponse = webRequest.GetResponse())
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AdmAccessToken));
                    //Get deserialized object from JSON stream
                    AdmAccessToken token = (AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream());
                    return token;
                }
            }

This is then called by:

       AdmAuthentication admAuth = new AdmAuthentication("CLIENT ID", "CLIENT SECRET");
       admToken = admAuth.GetAccessToken();
       Session["admToken"] = admToken;

where we keep the token as a session key, which is used for other accesses. To create the CLIENT ID and CLIENT SECRET we go to.

http://datamarket.azure.com/

and then click on Register Your Application (which is near the bottom of the page).  In the example below the CLIENT ID is french and the CLIENT SECRET is /fspbEcYHaX/TOm1TlDJAoeK6oMe7q0k9wDhTvl1gic=:

azCreating the controller

The controller has a GET and a POST, where the GET is called when the page is first accessed, and the POST results from the user clicking on the Translate button. With the code the getTranslation() method will return a translation of text, from English (“en”) to French (“fr”) and the getTranslationSp() method will return a wav file link for the translation.

 [HttpPost]
        public ActionResult french(FormCollection Form)
        {
            string text = Form["english"];
            AdmAccessToken admToken  = (AdmAccessToken)Session["admToken"];
            ViewData["english"] = text;
            string rtn = getTranslation(admToken,text, "en", "fr");
            ViewData["french"] = rtn;
            rtn = getTranslationSp(admToken,rtn, "fr");
            ViewData["audio"] = rtn;
            return PartialView("french_partial");
        }

        [HttpGet]
        public ActionResult french(string text)
        {
            string rtn = "";
            AdmAccessToken admToken;
            AdmAuthentication admAuth = new AdmAuthentication("CLIENT ID", "CLIENT SECRET");
            admToken = admAuth.GetAccessToken();
            Session["admToken"] = admToken;
            if (text != null)
            {
                rtn = getTranslation(admToken,text, "en", "fr");
                ViewData["french"] = rtn;
                rtn = getTranslationSp(admToken, rtn, "fr");
                ViewData["audio"] = rtn;
                ViewData["english"] = text;
            }
            return View();
        }

the methods for the translation can then be added:

 public string getTranslation(AdmAccessToken admToken, string text, string from, string to)
        {
            try
            {
                string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + System.Web.HttpUtility.UrlEncode(text) + "&from=" + from + "&to=" + to;
                string authToken = "Bearer" + " " + admToken.access_token;

                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
                httpWebRequest.Headers.Add("Authorization", authToken);

                WebResponse response = httpWebRequest.GetResponse();
                using (Stream stream = response.GetResponseStream())
                {
                    System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
                    string translation = (string)dcs.ReadObject(stream);
                    return (translation);
                }
            }
            catch (Exception ex)
            {
                return ("Not Known (" + ex.Message + ")");
            }
            return ("");
        }
        public string getTranslationSp(AdmAccessToken admToken, string text, string from)
        {
            try
            {
                string uri = "http://api.microsofttranslator.com/v2/Http.svc/Speak?text="+text+"&language="+from+"&format=" + HttpUtility.UrlEncode("audio/wav") + "&options=MaxQuality";
                string authToken = "Bearer" + " " + admToken.access_token;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                request.Headers.Add("Authorization", authToken);

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                // Get the stream associated with the response.
                Stream receiveStream = response.GetResponseStream();
                byte[] buffer = new byte[32768];
               string fname = "cc/"+System.Guid.NewGuid().ToString()+".wav";
               using (FileStream fileStream = System.IO.File.Create(Server.MapPath("~/") + fname))
                {
                    while (true)
                    {
                        int read = receiveStream.Read(buffer, 0, buffer.Length);
                        if (read <= 0)
                            break;
                        fileStream.Write(buffer, 0, read);
                    }
                }
                ViewData["file"] = "/" + fname;
            }
            catch (Exception ex)
            {
                return ("Audio error: (" + ex.Message + ")");
            }
            string filename = "/" + fname;
            return (filename);
        }

Creating the view

Now we need to create the view using Ajax with (french.cshtml):

@{
    ViewBag.Title = "French Translation";
}

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<!-- ~/Scripts/jquery.validate.unobtrusive.js -->

 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

        <script>
            function DHTMLSound(surl) {
  document.getElementById("dummyspan").innerHTML=
    "";
}
        </script>

    <table width="100%" border="1" cellpadding="0" cellspacing="0">

      <tr><td valign="top" colspan="2"><div> <h1>English to French</h1></div>
<div> &nbsp;</div>
           <div>
<p><a href="javascript:history.go(-1)">Back</a> Press the button for the translation:</p>

           @using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "french_partial", HttpMethod = "POST", InsertionMode = InsertionMode.Replace }))
            {           

                      <div id="french_partial">
 @Html.Partial("french_partial")
</div>             }     
               </div>
</td></tr>
    </table>

and a partial view of (french_partial.cshtml):

<table width="100%" border="0">
  <tr>   
    <td width="50%" valign="top" bgcolor="#F8F8FF">
        <h2>Parameters</h2>
        <div>
       <div>Enter English phrase:</div><div> @Html.TextArea("english", null, new { style = "width:400px" })
        <input type="submit" id="buttoncheckanswer" value="Translate" name="button1" /> </div>   
 </div>
</td>
    <td width="50%" valign="top"><h2>Translation</h2>
<p>Translation is: @ViewData["french"]</p> 
<form><input type="button" value="Play Translation" onClick="DHTMLSound('@ViewData["file"]')">
</form>
        <span id=dummyspan></span>
    </td>
  </tr>
</table>

Notice that ViewData[“file”] is used to point to the WAV file that has been created on the system, and that ViewData[“french”] is the translated text. So, try it here (just click on the graphic to try):

az2

The Art of Hidden Messages

Background

– I’ve hidden a secret message in this blog … see if you can find it.

Convert messages
Figure 1: Convert messages

As children we used to delight in sending secret messages to our friends, using secret codes, and sometimes we would hide things in something that only those who knew where to look could find it. Unfortunately, for most of us, we’ve lost that wonderment, also as we grow up we find out that people think we have doing something suspicious if we send secret messages, or our skills in creating these has often gone, as we stick to standard ways of communicating with everyone else.

So when are covert messages actually used … well these days it is likely to be used where there are two suspects (Figure 1), and they are being monitored for all their communications, and the only way they can pass messages to each other is to use a convert channel. So let’s look at some of the ways that this could be done, with very little effort.

The Mess that is the Internet

The Internet was created in a time of main frame computers where users typed-in messages to send emails and even to access Web pages. So when you wanted to send an email you connected to port 25 on the server, and type:

HELO myserver.com
MAIL FROM: Fred@home
RCPT TO: Bert@home
cc: My Message
DATA
How are you
.

which will send the email message to bert@home from fred@home, with the message of “How are you”. The “.” at the end on its own is the pointer that the message has ended, and that it can be sent. You can see that it is a fairly simple method that is used to send something as important as an email, but basically it hasn’t changed, as we are still using the same old protocols that were created all those years ago. In fact … they kinda got the email address wrong, as the orginal proposed structure was based on X.500, which has the top level domain first, such as Domain=uk.ac, Organisation=napier, Organisational Unit=”computing”, which would have given “uk.ac.napier.computing”, but it became standardized almost with the first email that was sent.

The same simplistic method goes for accessing Web pages, where, with the HTTP protocol, a simple “GET” is used to actually get a Web page. For how does that relate to covert messages? Well these simple protocols are full of holes, where they are being used in ways that they were never intended for. So things like Service-Oriented Architectures, are actually built around the original HTTP specification, so that a stateless protocol, actually becomes a way of creating and modifying objects contained within Web accesses.

Covert Channels in IP and TCP

IP and TCP
Figure 2: IP and TCP

The two great kings of the Internet are IP and TCP. They have done more for humankind than virtually anything else. In terms of knowledge creation and sharing, it is IP and TCP that have supported both the creation of the Internet, and in supporting all the applications that are now used. The figure on the right shows the main field using in IP and TCP, and basically they are the same as the first data packet that was transmitted over the Internet. In IP, there’s the 32-bit IP source and destination address, which has become so successful, that we have no more IP addresses to give it. Any for TCP, there’s the source and destination port, which allows hosts to have many connections at the same time, each of which are unique across the whole of the Internet. It’s an amazing relationship, IP does the grunt work, it stamps the data packets for their destination, and receives them on the other end, but it’s up to TCP to keep a track on them, and make sure that none of them get lost on the way.

So what about covert messages? Well there are so many fields in IP and TCP that do not have a strong use, that can be used for sending covert messages. In Figure 1, I’ve identified the rogues gallery of these, including the Fragment Offset, the TTL field and Identification, for IP, and for TCP, it is the Urgent Point and Data Offset. Each of these can easily support sending secret messages between two people. While most of these fields are unused, obviously the TTL field is used to make sure that the data packet doesn’t traverse the Internet forever, so how can we use that? Well we typically only new 26 characters to send a value, so when generating we take the first value, and then we Exclusive-OR it with the character, from 0 to 25 for each of the characters. So for example, if the TTL is 255 as an initial value the TTL value for an ‘a’ would be:

255 1111 1111
'a '  0000 0000
Result: 1111 1111 (255)

and for ‘z’

255 1111 1111
'z' 0001 1001
to give 1110 0110 (230)

So hopefully the value will never fall to a point where the TTL of the packet will never reach zero (and be deleted).

IP ID Field
Figure 3: IP ID Field

The strange thing about IP is that there is quite a bit of variation across its implementation, and the best example is in the identification field. The only requirement is that its can uniquely define IP fragments within a given time window. So some systems just increment this value by 1, from a random starting point (such as with Microsoft Windows, as show on the first diagram in Figure 3), or in Linux (second diagram in Figure 3) which create a random value for each one, or with Solaris, which increments for a while, and then takes a random jump (third diagram in Figure 3). Have a look at these: Windows packets.

You will see the ID field goes:

Identification: 0x1640 (5696)
Identification: 0x1641 (5697)

and for Ubuntu: Ubuntu packets.

This is a useful way to determine the operating system for someone monitoring network packets, and is used by security evaluators such as NMAP to guess the operating system type. The field could, though, be used to send covert messages, so the value within it is not checked by intermediate devices, or by the end hosts.

Storage or Timing

Timing or storage
Figure 4: Timing or storage

There are obviously many ways of sending covert messages. We might as a secret code decide that when we met I would wear a red rose on my lapel. So if I wanted to pass a message to you, an the first letter was an ‘a’, then the binary for this is 0110 0001 (61 in hex), so the first time I would wear a rose (‘1’), then not for the next four times, and then weak it the next two times, and then not for the next time. In this way, as long as no-one noticed I would send a message through our covert channel. Thus we can use either a storage channel – where to add something or not to be place – or we can use a timing challenge where we can change the operation of a resource. This might that I will drive the CPU on a computer to 100% at certain times when I want to pass a 1, or leave it for a 0.

HTTP Convert Channel
Figure 5: HTTP Convert Channel

So we an even use normal protocols to do this. For example there is a Connection: Keep Alive field in the HTTP header, which is used to tell the server that the client does not want to break the connection that it has created. This is used, as it reduces the time to access more content on the site. In Figure 4 we can see we can send a 0 when it is present, and a 1 when it is not present. For anyone viewing this communication, everything looks fine, but for the suspects they know where to look to find the hidden message.

Message in pictures

Picture4One obvious way to hide message is to put them in another container, such as in a graphics file. For the observer, the image looks fine, but hidden within is a message for the suspects. One way to hide a message is obviously to place it on a layer in the graphic file, and then make it invisible. In the figure we can see that we can add opacity to some text, we can make it less visible, and if we turn it to 100% opacity the user will not see it. This technique works for graphics file which have layers, such as PNG and PSD, but doesn’t work with flattened formats or bit maps, such as with GIF, JPEG, and so on. As with the problems in network protocols, so there are places in graphics files that we can hide things. For example in the GIF format we can have up to 256 24-bit colours (or Red, Green and Blue), which be used in the image. So GIF is a good graphic format when we only have a few colours (such as for icons) but it is not good with photographs, as we are limited in our colour palette. So one place to hide a message is to insert it into the colour table, which might affect a few pixels, the user might not see any changes.

Picture5I’ve hidden a message “hello” in the following image:

http://www.asecuritysite.com/information/gif?file=cat01_with_hidden_text.gif

Have a look at the two images on the right hand side, and you will see that a few pixels around the cat have changed, as the colours that these pixels map to, have had their colour changed. Remember that each colour is 24-bits, with 8-bits for red, 8-bits for green and 8 bits for blue. So for “hello” it will change a maximum of two colours in the pixel map. It could do it even more discrete by spreading the message across the least significant bits of the colour table, so that the changes in colour would be minimal, and no user could spot the changes.

The actual format of the file, with the convert message, is:

[00000000] 47 49 46 38 39 61 64 00   GIF89ad.
[00000008] 55 00 E6 00 00 FF FF FF   U.......
[00000016] F7 F7 F6 F1 F4 F2 EE EE   ........
[00000024] EF E7 E7 E7 E1 E4 E6 DF   ........
[00000032] DE DF D7 DA DD EF CE CE   ........
[00000040] D5 D5 D5 D5 D3 D0 D9 D1   ........
[00000048] A1 CC CC CC C4 C8 CC 68   .......h
[00000056] 65 6C 6C 6F C0 D1 C6 84   ello....
[00000064] C0 BF BD BD BB B8 B8 B6   ........
[00000072] B5 B5 B3 AE AA B1 B6 AB   ........
[00000080] AC AD AB A9 A5 A6 A6 A6   ........
[00000088] A7 A5 9E AB A8 70 AC 9C   .....p..
[00000096] 9F 99 99 99 94 9A A0 8B   ........
[00000104] 95 9C 93 92 8E 8C 8D 8A   ........
[00000112] 86 8C 96 98 8B 66 90 87   .....f..
[00000120] 82 83 83 83 7A 84 8A CB   ....z...
[00000128] 5E 5E FB 48 48 82 7C 73   ^^.HH.|s

Conclusions

Well if someone want to communicate with a convert channel, they will do it, and there’s often little you can do about. What you need to make sure is that, where important, the mechanisms for this are limited. In the next blog I’ll outline other methods. So, as a last little challenge, here’s a sample of some covert messages that we have used to present security to School kids:

Can you find the six secret messages? Hint … look down the lines.

If you want to check … here’s the answers.