`

Android NTLM Authentication

阅读更多

 

http://stackoverflow.com/questions/15762237/negotiate-using-one-session-with-ntlm-authentication-at-android

 

http://www.tekritisoftware.com/android-ntlm-authentication

 

 

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:

             NTLMSchemeFactory.java:

 

package com.movit.util;

 

import org.apache.http.auth.AuthScheme;

import org.apache.http.auth.AuthSchemeFactory;

import org.apache.http.impl.auth.NTLMScheme;

import org.apache.http.params.HttpParams;

 

public class NTLMSchemeFactory implements AuthSchemeFactory {

    public AuthScheme newInstance(HttpParams params) {

        return new NTLMScheme(new JCIFSEngine());

    }

}

 

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

JCIFSEngine.java

package com.movit.util;

 

import java.io.IOException;

 

import jcifs.ntlmssp.NtlmFlags;

import jcifs.ntlmssp.Type1Message;

import jcifs.ntlmssp.Type2Message;

import jcifs.ntlmssp.Type3Message;

 

import org.apache.http.impl.auth.NTLMEngine;

import org.apache.http.impl.auth.NTLMEngineException;

 

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.

 

 

    /*

     * authentication method 7

     * webserviceUrl, 

     * url of the web service. 

     * webserviceIP, 

     * IP of the server.

     * username, Domain username 

     * password, Domain password

     */

    public int getFileSize(String webserviceUrl, String username, String password) {

        int fileSize = 0;

        try {

            String deviceIP = null;// getLocalIpAddress(); Device IP

            String domainName = FileUtils.DOMAIN_NAME;

 

            DefaultHttpClient httpclient = new DefaultHttpClient();

            httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());

            AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);

            AuthScope authScope1 = new AuthScope(null, -1);

            httpclient.getCredentialsProvider().setCredentials(authScope,

                                                               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());

 

            if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {

                HttpEntity entity = response.getEntity();

                //fileSize = (int) entity.getContentLength();

                fileSize = getfileSizeByInputStream(entity.getContent());

                entity.consumeContent();

                return fileSize;

            }

 

        } catch (Exception e) {

            e.printStackTrace();

        }

        return fileSize;

    }

 

    public int getfileSizeByInputStream(InputStream input) {

        BufferedInputStream bis = new BufferedInputStream(input);

        int progress = 0;

        try {

            int length;

            byte buffer[] = new byte[6 * 1024];

            while (-1 != (length = bis.read(buffer))) {

                progress += length;

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                if (null != bis) {

                    bis.close();

                    bis = null;

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

        return progress;

    }

 

    public String getLocalIpAddress() {

 

        String deviceIp = null;

        boolean keepLookupOn = true;

 

        try {

            Enumeration availableNetwork = NetworkInterface.getNetworkInterfaces();

 

            while (availableNetwork.hasMoreElements() && keepLookupOn) {

                NetworkInterface intf = (NetworkInterface) availableNetwork.nextElement();

                Enumeration enumIpAddr = intf.getInetAddresses();

 

                while (enumIpAddr.hasMoreElements()) {

                    InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();

 

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

 

                    if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(deviceIp)) {

                        keepLookupOn = false;

                        break;

                    }

                }

            }

        } catch (SocketException ex) {

            ex.printStackTrace();

        }

 

        return deviceIp;

 

    }

 

分享到:
评论

相关推荐

    NTLM.ZIP_DELPHI NTLM_NTLM Authentication_authentication

    NTLM SSPI Authentication code for Delphi

    python-ntlm 1.1.0

    Python library that provides NTLM support, including an authentication handler for urllib2. Works with pass-the-hash in additon to password authentication.

    c++实现LM和NTLM哈希

    在vs2008下实现NTLM和LM哈希加密,包含MD4加密和DES加密函数

    requests-ntlm, 请求的NTLM身份验证支持.zip

    requests-ntlm, 请求的NTLM身份验证支持 请求 ntlm 这个包允许使用请求库进行 HTTP NTLM身份验证。用法HttpNtlmAuth 扩展请求 AuthBase,因此使用很简单:import requestsfrom requests_ntlm

    NTLM验证解除工具

    关于NTLM验证 由于Telnet功能太强大,而且也是入侵者使用最频繁的登录手段之一,因此微软公司为Telnet添加了身份验证,称为NTLM验证,它要求Telnet终端除了需要有Telnet服务主机的用户名和密码外,还需要满足NTLM...

    ntlm验证Java代码

    里面有ntlm验证的demo 修改一下你url username passwrod 等内容应该就可以使用了 如果不可以 请抓包核查一下

    绕过NTLM认证方式的另一种方法.doc

    绕过NTLM认证方式的另一种方法.doc 绕过NTLM认证方式的另一种方法.doc

    axis2客户端调用带Ntlm认证例子

    axis2客户端,调用带ntlm认证的web service的代码例子和所需jar和java

    NTLM.EXE:用于突破Telnet中的NTLM权限认证

    用于突破Telnet中的NTLM权限认证:先和目标计算机建立一个IPC$连接,将ntlm.exe复制到目标计算机上,再通过at命令执行即可。

    Clever Internet .NET Suite 6.0.26.0

    包括源代码 <br>Version 6.0.26.0 (8 February 2007) <br>Implemented Features: <br>HTTP Client - HTTPS (SSL / TLS) protocol, NTLM and Negotiate authentication were implemented. FTP Server -...

    ntlm.h 头文件 VC

    interface declarations for SMB authentication code 只要将此头文件放入VC文件夹中的“Include”文件中即可。相信,这对大家都挺好的。

    gss-ntlmssp:作为GSSAPI机制的MS-NLMP文档的完整实现

    这是用于实现NTLM身份验证的GSSAPI库的mechglue插件。 到目前为止,它仅使用MIT Kerberos随附的libgssapi实现进行构建和测试(版本1.11及更高版本) 项目信息 该项目目前托管在 与项目相关的信息(发布,热烈的...

    cntlm-0.92.3

    Once you're behind those cold steel bars of a corporate proxy server requiring NTLM authentication, you're done with. The same even applies to 3rd party Windows applications, which don't support NTLM...

    ntml.rar_NTLM_NTML_ntlm算法_ntml协议认证_ntml认证

    NTLM挑战模式散列认证加密协议过程,算法实现与一些想法

    NTLM认证基本原理及编程简介

    NTLM认证基本原理及编程简介 对需要的人很有用

    代理通信,linux下,包含NTLM认证

    本代码实现了网络代理通信,包含复杂的网络环境,ntlm认证等

    NTLM.rar_NTLM

    LM/NTLM验证机制,详细介绍了该机制的完整过程。

    NTLM PPROXY client的实现(C source code)

    其中arith_开头的相关算法,httppc_ntlm是 NTLM的type1、type2、type3消息的应答构成,test_httppc_connect是一个测试的例子,里面没有main函数,但是另外构造一个main函数,在wei_set_proxy_info之后,调用wei_...

    AD域单点登陆NTLM

    java 基于NTLM协议集成AD账号域登录 内含JAR包,DEMO包以及说明文档

    Windows下LM-Hash与NTLM-Hash

    文档描述了Windows下LM HASH ,NTLM HASH生成原理和规则。 并利用Python脚本进行模拟。 详细描述了 挑战/响应模式(鉴权协议)的原理 详细描述了NTLM SMB通信流程 对内网渗透入门感兴趣的一定下载

Global site tag (gtag.js) - Google Analytics