SlideShare a Scribd company logo
1 of 3
NTLM Authentication in Android
With the increasing usage of smart phone in our daily life, this usage is getting more quantitative
as well as qualitative by each passing day. It started with basic telephony then gaming and now it
has graduated to Apps which helps in managing and exchanging important data like mails,
financial details, payrolls and many more. Such heavy exchanges from or to the outer world is
done by interacting with different server through legacy communication protocols and that
involves different types of authentication handshakes.

Recently we came across a requirement of communicating to a server which is using NTLM
authentication protocol. Now some of you would think “What is NTLM” so here is the answer::

NTLM is a suite of Microsoft security protocol and is successor to the authentication protocol in
Microsoft LAN Manager. It is used for the authentication and negotiation of secure DCE purpose.
NTLM Security Service Provider (NTLMSSP) implements some core operations and these are::

   1) Authentication :: Clients would be able to prove their respective identities
   2) Signing :: It provides digital “signature” security.
   3) Sealing :: It keeps the data confidential by providing symmetric-Key encryption.

More about NTLM and its authentication mechanism can be read in details at
 http://www.innovation.ch/personal/ronald/ntlm.html
http://davenport.sourceforge.net/ntlm.html#whatIsNtlm

Here is the solution for successfully authenticating and communicating with a server using NTLM
authentication protocol:

Step 1: We need to have the JCIF library.
JCIFS can be downloaded from: http://jcifs.samba.org/

Step 2: Create a class which is the AuthSchemeFactory interface:

public class NTLMSchemeFactory implements AuthSchemeFactory
{
      public AuthScheme newInstance(HttpParams params)
      {
             return new NTLMScheme(new JCIFSEngine());
      }
}


Step 3: Create a class for acting as a NTLM engine interface, which does all the type message
validation and verification.

public class JCIFSEngine implements NTLMEngine
{
      private static final int TYPE_1_FLAGS =
        NtlmFlags.NTLMSSP_NEGOTIATE_56 |
        NtlmFlags.NTLMSSP_NEGOTIATE_128 |
        NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2 |
        NtlmFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
        NtlmFlags.NTLMSSP_REQUEST_TARGET;

     public String generateType1Msg(String domain, String workstation) throws
NTLMEngineException
{
              final Type1Message type1Message = new Type1Message(TYPE_1_FLAGS, domain,
workstation);
     return Base64.encode(type1Message.toByteArray());
      }

      public String generateType3Msg(String username, String password,String domain, String
workstation, String challenge) throws NTLMEngineException
      {
              Type2Message type2Message;

                  try
                  {
             type2Message = new Type2Message(Base64.decode(challenge));
     }
                  catch (final IOException exception)
                  {
             throw new NTLMEngineException("Error in type2 message", exception);
     }

     final int type2Flags = type2Message.getFlags();
     final int type3Flags = type2Flags & (0xffffffff ^
(NtlmFlags.NTLMSSP_TARGET_TYPE_DOMAIN | NtlmFlags.NTLMSSP_TARGET_TYPE_SERVER));
     final Type3Message type3Message = new Type3Message(type2Message, password, domain,
username, workstation, type3Flags);
     return Base64.encode(type3Message.toByteArray());
       }
}


Step 4: Register the NTLM Scheme Factory with HttpClient instance and other domain credentials
to do the handshake. It has been observed that deviceIP and domainName is not required on a
mandatory basis. These can be set as null also.

String   webserviceUrl = "<Your server url>"; // url of the web service.
String   webserviceIP = "<server IP>"; // IP of the server.
String   username = “<domain username>”; // Domain username
String   password = “<domain password>”; // Domain password
String   deviceIP = “<device ip address>”; // Device IP
String   domainName = "<Your domain name>"; // Domain name


DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
httpclient.getCredentialsProvider().setCredentials(new AuthScope(webserviceIP, -1), new
NTCredentials(username, password, deviceIP, domainName));

HttpGet httpGet = new HttpGet(webserviceUrl);
httpGet.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

HttpResponse response = httpclient.execute(httpGet);
String responseXML = EntityUtils.toString(response.getEntity());


NB: to get the IP address of the device, use the following method:
public String getLocalIpAddress()
{
      String deviceIp = null;
      boolean keepLookupOn = true;

      try
      {
      Enumeration<NetworkInterface> availableNetwork =
NetworkInterface.getNetworkInterfaces();

      while( availableNetwork.hasMoreElements() && keepLookupOn )
      {
            NetworkInterface intf = availableNetwork.nextElement();
            Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();


             while( enumIpAddr.hasMoreElements() )
             {
                   InetAddress inetAddress = enumIpAddr.nextElement();

                    deviceIp = inetAddress.getHostAddress().toString();

                     if( !inetAddress.isLoopbackAddress() &&
InetAddressUtils.isIPv4Address(deviceIp) )
                     {
                             keepLookupOn = false;
                             break;
                     }
             }
      }
      }
      catch (SocketException ex)
      {
           ex.printStackTrace();
      }

      return deviceIp;
}


Keyword: Android, NTLM Authention in Android, Android Device IP

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Android NTLM Authentication

  • 1. NTLM Authentication in Android With the increasing usage of smart phone in our daily life, this usage is getting more quantitative as well as qualitative by each passing day. It started with basic telephony then gaming and now it has graduated to Apps which helps in managing and exchanging important data like mails, financial details, payrolls and many more. Such heavy exchanges from or to the outer world is done by interacting with different server through legacy communication protocols and that involves different types of authentication handshakes. Recently we came across a requirement of communicating to a server which is using NTLM authentication protocol. Now some of you would think “What is NTLM” so here is the answer:: NTLM is a suite of Microsoft security protocol and is successor to the authentication protocol in Microsoft LAN Manager. It is used for the authentication and negotiation of secure DCE purpose. NTLM Security Service Provider (NTLMSSP) implements some core operations and these are:: 1) Authentication :: Clients would be able to prove their respective identities 2) Signing :: It provides digital “signature” security. 3) Sealing :: It keeps the data confidential by providing symmetric-Key encryption. More about NTLM and its authentication mechanism can be read in details at http://www.innovation.ch/personal/ronald/ntlm.html http://davenport.sourceforge.net/ntlm.html#whatIsNtlm Here is the solution for successfully authenticating and communicating with a server using NTLM authentication protocol: Step 1: We need to have the JCIF library. JCIFS can be downloaded from: http://jcifs.samba.org/ Step 2: Create a class which is the AuthSchemeFactory interface: public class NTLMSchemeFactory implements AuthSchemeFactory { public AuthScheme newInstance(HttpParams params) { return new NTLMScheme(new JCIFSEngine()); } } Step 3: Create a class for acting as a NTLM engine interface, which does all the type message validation and verification. public class JCIFSEngine implements NTLMEngine { private static final int TYPE_1_FLAGS = NtlmFlags.NTLMSSP_NEGOTIATE_56 | NtlmFlags.NTLMSSP_NEGOTIATE_128 | NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2 | NtlmFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NtlmFlags.NTLMSSP_REQUEST_TARGET; public String generateType1Msg(String domain, String workstation) throws NTLMEngineException
  • 2. { final Type1Message type1Message = new Type1Message(TYPE_1_FLAGS, domain, workstation); return Base64.encode(type1Message.toByteArray()); } public String generateType3Msg(String username, String password,String domain, String workstation, String challenge) throws NTLMEngineException { Type2Message type2Message; try { type2Message = new Type2Message(Base64.decode(challenge)); } catch (final IOException exception) { throw new NTLMEngineException("Error in type2 message", exception); } final int type2Flags = type2Message.getFlags(); final int type3Flags = type2Flags & (0xffffffff ^ (NtlmFlags.NTLMSSP_TARGET_TYPE_DOMAIN | NtlmFlags.NTLMSSP_TARGET_TYPE_SERVER)); final Type3Message type3Message = new Type3Message(type2Message, password, domain, username, workstation, type3Flags); return Base64.encode(type3Message.toByteArray()); } } Step 4: Register the NTLM Scheme Factory with HttpClient instance and other domain credentials to do the handshake. It has been observed that deviceIP and domainName is not required on a mandatory basis. These can be set as null also. String webserviceUrl = "<Your server url>"; // url of the web service. String webserviceIP = "<server IP>"; // IP of the server. String username = “<domain username>”; // Domain username String password = “<domain password>”; // Domain password String deviceIP = “<device ip address>”; // Device IP String domainName = "<Your domain name>"; // Domain name DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory()); httpclient.getCredentialsProvider().setCredentials(new AuthScope(webserviceIP, -1), new NTCredentials(username, password, deviceIP, domainName)); HttpGet httpGet = new HttpGet(webserviceUrl); httpGet.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false); HttpResponse response = httpclient.execute(httpGet); String responseXML = EntityUtils.toString(response.getEntity()); NB: to get the IP address of the device, use the following method:
  • 3. public String getLocalIpAddress() { String deviceIp = null; boolean keepLookupOn = true; try { Enumeration<NetworkInterface> availableNetwork = NetworkInterface.getNetworkInterfaces(); while( availableNetwork.hasMoreElements() && keepLookupOn ) { NetworkInterface intf = availableNetwork.nextElement(); Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); while( enumIpAddr.hasMoreElements() ) { InetAddress inetAddress = enumIpAddr.nextElement(); deviceIp = inetAddress.getHostAddress().toString(); if( !inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(deviceIp) ) { keepLookupOn = false; break; } } } } catch (SocketException ex) { ex.printStackTrace(); } return deviceIp; } Keyword: Android, NTLM Authention in Android, Android Device IP