JMX安全漏洞修复 服务端增加用户名和密码验证机制

简介: JMX安全漏洞修复 服务端增加用户名和密码验证机制

方式1 启动脚本中增加参数

-Djmx.authenticate=true -Djmx.password=$base/conf/jmxremote.password -Djmx.access=$base/conf/jmxremote.access

方式2 启动程序中设置环境变量

前提是继承 extends ConnectorServerFactoryBean类
配置文件增加属性:
  # 是否启用JMX
  jmx.authenticate = true
  # jmxremote.access文件路径
  jmxremote.access.path = ${user.dir}/../conf/jmxremote.access
  # jmxremote.password文件路径
  jmxremote.password.path = ${user.dir}/../conf/jmxremote.password
private void setJMXParameter() {
        if ("true".equalsIgnoreCase(jmxAuthenticate)) {
            Properties prop = new Properties();
            prop.put("jmx.remote.x.password.file", jmxPassword);
            prop.put("jmx.remote.x.access.file", jmxUsername);
            prop.put("jmx.remote.x.mlet.allow.getMBeansFromURL", false);
            setEnvironment(prop);
        }
    }

附:

jmxremote.access 文件下载

jmxremote.password 文件下载

demo:

package com.tongtech.remote.impl;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.remote.*;
import javax.management.remote.rmi.RMIConnectorServer;
import javax.security.auth.Subject;
import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RMISocketFactory;
import java.util.*;
/**
 * @Auther: zhaoshuangjian  2023-04-25 10:07 PM
 */
public class UserJmxServer {
    public static void main(String[] args){
//        MBeanServer mBeanServer;
//        User bean=new User();
        try {
            MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); //向MBeanServer注册mbean
            ObjectName objectName = new ObjectName("user:name=User");
            mBeanServer.registerMBean(new User(), objectName);
            JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/user");
            System.out.println("JMXServiceURL: " + jmxServiceURL.toString());
            Map<String ,Object> jmxEnv=new HashMap<String,Object>();
            RMISocketFactory rmiFactory = RMISocketFactory.getDefaultSocketFactory();
            Registry registry = LocateRegistry.createRegistry(1099,null, rmiFactory); //构造 JMXServiceURL
            jmxEnv.put("jmx.remote.credentials", new String[]{"admin","admin123"});
            jmxEnv.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, rmiFactory);
            JMXAuthenticator auth = createJMXAuthenticator();
            jmxEnv.put(JMXConnectorServer.AUTHENTICATOR, auth);
            jmxEnv.put("jmx.remote.x.mlet.allow.getMBeansFromURL","false");
            JMXConnectorServer jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, jmxEnv, mBeanServer);
            jmxConnectorServer.start();
            System.out.println("JMXConnectorServer is running");
//            int rmiPort = 6090;
//            Registry registry = LocateRegistry.createRegistry(rmiPort);
//            mBeanServer = MBeanServerFactory.createMBeanServer("user");
//
//            ObjectName objectName = new ObjectName("user:name=User");
//            mBeanServer.registerMBean(bean, objectName);
//
//            JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:6090/mbean");
//            System.out.println("JMXServiceURL: " + url.toString());
//            Map<String ,Object> jmxEnv=new HashMap<String,Object>();
//            jmxEnv.put("jmx.remote.credentials", new String[]{"admin","admin123"});
//            JMXAuthenticator auth = createJMXAuthenticator();
//            jmxEnv.put(JMXConnectorServer.AUTHENTICATOR, auth);
            jmxEnv.put("jmx.remote.x.mlet.allow.getMBeansFromURL","false");
//            JMXConnectorServer jmxConnServer = JMXConnectorServerFactory.newJMXConnectorServer(url, jmxEnv, mBeanServer);
//            jmxConnServer.start();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     *  认证
     * @return
     */
    private static JMXAuthenticator createJMXAuthenticator(){
        return new JMXAuthenticator()
        {
            public Subject authenticate(Object credentials) {
                String[] sCredentials = (String[]) credentials;
                if (null == sCredentials || sCredentials.length != 2) {
                    throw new SecurityException("Authentication failed!");
                }
                String userName = sCredentials[0];
                String pValue = sCredentials[1];
                if ("admin".equals(userName) && "admin123".equals(pValue)) {
                    Set<JMXPrincipal> principals = new HashSet<JMXPrincipal>();
                    principals.add(new JMXPrincipal(userName));
                    System.out.println("认证成功");
                    return new Subject(true, principals, Collections.EMPTY_SET, Collections.EMPTY_SET);
                }
                throw new SecurityException("Authentication failed!");
            }
        };
    }
}
package com.tongtech.remote.impl;
/**
 * @Auther: zhaoshuangjian  2023-04-25 10:05 PM
 */
public interface JMXUserMBean {
    Long getUserId();
    String getUserName();
    String getPassWord();
    void setUserId(Long userId);
    void setUserName(String userName);
}
package com.tongtech.remote.impl;
/**
 * @Auther: zhaoshuangjian  2023-04-25 10:06 PM
 */
public class User implements JMXUserMBean {
    Long userId = 12345678L;
    String userName = "jvm-user";
    @Override
    public Long getUserId() {
        return userId;
    }
    @Override
    public String getUserName() {
        return userName;
    }
    @Override
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    @Override
    public void setUserName(String userName) {
        this.userName = userName;
    }
}
相关文章
|
6月前
|
网络安全 Nacos 数据安全/隐私保护
nacos常见问题之使用默认用户名密码提示错误如何解决
Nacos是阿里云开源的服务发现和配置管理平台,用于构建动态微服务应用架构;本汇总针对Nacos在实际应用中用户常遇到的问题进行了归纳和解答,旨在帮助开发者和运维人员高效解决使用Nacos时的各类疑难杂症。
|
5月前
|
Java 关系型数据库 MySQL
使用CAS登录模块连接数据库验证用户合法性
使用CAS登录模块连接数据库验证用户合法性
27 0
|
6月前
|
前端开发 安全 Go
CSRF 实验:更改请求方式绕过验证
CSRF 实验:更改请求方式绕过验证
|
6月前
|
数据安全/隐私保护
2012及其以上系统修改服务器密码指南
2012及其以上系统修改服务器密码指南
LXJ
|
数据安全/隐私保护
服务器用户、密码变动检测脚本
服务器上如果有用户变动,或者密码变动会有邮件通知。
LXJ
142 0
|
测试技术
Confluence 6 为登录失败配置使用验证码
如果你具有 Confluence 管理员的权限,你可以限制 Confluence 登录失败的最大尝试次数。在给予最大登录失败尝试(默认为 3 次)次数后,Confluence 将会在用户进行再次尝试的时候显示验证码输入框。
1301 0
|
Web App开发 算法 数据安全/隐私保护