[关闭]
@Alex-Zhao 2021-09-27T15:08:09.000000Z 字数 8029 阅读 75

Elasticsearch7.4.2中破解X-PACK

ELK


在Elastic6.4之后,x-pack默认就放在安装包中,不需要另下载。
本文章场景是Centos7.7-minimal系统,通过yum安装。elasticsearch默认安装路径是/usr/share/elasticsearch
repo文件:

  1. [logstash-7.x]
  2. name=Elastic repository for 7.x packages
  3. baseurl=https://artifacts.elastic.co/packages/7.x/yum
  4. gpgcheck=1
  5. gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
  6. enabled=1
  7. autorefresh=1
  8. type=rpm-md

破解x-pack只需要x-pack-core-7.4.2.jar文件,该文件路径/usr/share/elasticsearch/modules/x-pack-core

准备工作

反编译软件Luyten:Luyten下载地址
免费版basic版本的license json文件:注册地址,注册完成会将链接发送到邮箱,然后下载json文件。
x-pack-core-7.4.2.jar文件:从原目录拷贝一份到工作电脑中,进行修改源代码。
JAVA1.8以上环境
JAVA1.8以上开发环境(javac)

修改X-Pack源代码

在Luyten工具中我们需要把2个文件提取出来进行修改。org.elasticsearch.license.LicenseVerifierorg.elasticsearch.xpack.core.XPackBuild,该工具并不能将其转换成文件,只能显示其代码,将代码拷贝出来,另创建两个java文件进行修改。

修改LicenseVerifier.java

LicenseVerifier中有两个静态方法,这就是验证授权文件是否有效的方法,我们把它修改为全部返回true

  1. package org.elasticsearch.license;
  2. import java.nio.*;
  3. import org.elasticsearch.common.bytes.*;
  4. import java.security.*;
  5. import java.util.*;
  6. import org.elasticsearch.common.xcontent.*;
  7. import org.apache.lucene.util.*;
  8. import org.elasticsearch.core.internal.io.*;
  9. import java.io.*;
  10. public class LicenseVerifier
  11. {
  12. public static boolean verifyLicense(final License license, final byte[] publicKeyData) {
  13. /*
  14. byte[] signedContent = null;
  15. byte[] publicKeyFingerprint = null;
  16. try {
  17. final byte[] signatureBytes = Base64.getDecoder().decode(license.signature());
  18. final ByteBuffer byteBuffer = ByteBuffer.wrap(signatureBytes);
  19. final int version = byteBuffer.getInt();
  20. final int magicLen = byteBuffer.getInt();
  21. final byte[] magic = new byte[magicLen];
  22. byteBuffer.get(magic);
  23. final int hashLen = byteBuffer.getInt();
  24. publicKeyFingerprint = new byte[hashLen];
  25. byteBuffer.get(publicKeyFingerprint);
  26. final int signedContentLen = byteBuffer.getInt();
  27. signedContent = new byte[signedContentLen];
  28. byteBuffer.get(signedContent);
  29. final XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
  30. license.toXContent(contentBuilder, (ToXContent.Params)new ToXContent.MapParams((Map)
  31. Collections.singletonMap("license_spec_view", "true")));
  32. final Signature rsa = Signature.getInstance("SHA512withRSA");
  33. rsa.initVerify(CryptUtils.readPublicKey(publicKeyData));
  34. final BytesRefIterator iterator = BytesReference.bytes(contentBuilder).iterator();
  35. BytesRef ref;
  36. while ((ref = iterator.next()) != null) {
  37. rsa.update(ref.bytes, ref.offset, ref.length);
  38. }
  39. return rsa.verify(signedContent);
  40. }
  41. catch (IOException ex) {}
  42. catch (NoSuchAlgorithmException ex2) {}
  43. catch (SignatureException ex3) {}
  44. catch (InvalidKeyException e) {
  45. throw new IllegalStateException(e);
  46. }
  47. finally {
  48. if (signedContent != null) {
  49. Arrays.fill(signedContent, (byte)0);
  50. }
  51. }
  52. */
  53. return true; //新增一行
  54. }
  55. public static boolean verifyLicense(final License license) {
  56. /*
  57. byte[] publicKeyBytes;
  58. try {
  59. final InputStream is = LicenseVerifier.class.getResourceAsStream("/public.key");
  60. try {
  61. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  62. Streams.copy(is, (OutputStream)out);
  63. publicKeyBytes = out.toByteArray();
  64. if (is != null) {
  65. is.close();
  66. }
  67. }
  68. catch (Throwable t) {
  69. if (is != null) {
  70. try {
  71. is.close();
  72. }
  73. catch (Throwable t2) {
  74. t.addSuppressed(t2);
  75. }
  76. }
  77. throw t;
  78. }
  79. }
  80. catch (IOException ex) {
  81. throw new IllegalStateException(ex);
  82. }
  83. //return verifyLicense(license, publicKeyBytes);
  84. */
  85. return true; //新增一行
  86. }
  87. }

修改XPackBuild.java

XPackBuild中最后一个静态代码块中 try的部分全部删除,这部分会验证jar包是否被修改.

  1. package org.elasticsearch.xpack.core;
  2. import org.elasticsearch.common.io.*;
  3. import java.net.*;
  4. import org.elasticsearch.common.*;
  5. import java.nio.file.*;
  6. import java.io.*;
  7. import java.util.jar.*;
  8. public class XPackBuild
  9. {
  10. public static final XPackBuild CURRENT;
  11. private String shortHash;
  12. private String date;
  13. @SuppressForbidden(reason = "looks up path of xpack.jar directly")
  14. static Path getElasticsearchCodebase() {
  15. final URL url = XPackBuild.class.getProtectionDomain().getCodeSource().getLocation();
  16. try {
  17. return PathUtils.get(url.toURI());
  18. }
  19. catch (URISyntaxException bogus) {
  20. throw new RuntimeException(bogus);
  21. }
  22. }
  23. XPackBuild(final String shortHash, final String date) {
  24. this.shortHash = shortHash;
  25. this.date = date;
  26. }
  27. public String shortHash() {
  28. return this.shortHash;
  29. }
  30. public String date() {
  31. return this.date;
  32. }
  33. static {
  34. final Path path = getElasticsearchCodebase();
  35. String shortHash = null;
  36. String date = null;
  37. Label_0109: {
  38. /* if (path.toString().endsWith(".jar")) { //将一下部分注释掉
  39. try {
  40. final JarInputStream jar =
  41. new JarInputStream(Files.newInputStream(path, new OpenOption[0]));
  42. try {
  43. final Manifest manifest = jar.getManifest();
  44. shortHash = manifest.getMainAttributes().getValue("Change");
  45. date = manifest.getMainAttributes().getValue("Build-Date");
  46. jar.close();
  47. }
  48. catch (Throwable t) {
  49. try {
  50. jar.close();
  51. }
  52. catch (Throwable t2) {
  53. t.addSuppressed(t2);
  54. }
  55. throw t;
  56. }
  57. break Label_0109;
  58. }
  59. catch (IOException e) {
  60. throw new RuntimeException(e);
  61. }
  62. }
  63. */
  64. shortHash = "Unknown";
  65. date = "Unknown";
  66. }
  67. CURRENT = new XPackBuild(shortHash, date);
  68. }
  69. }

生成.class文件

上述LicenseVerifier.java和XPackBuild.java两个文件在本地电脑修改完成后,我们需要将其复制到elasticsearch服务器上并编译成class文件,然后打包到x-pack-core-7.4.2.jar中。
在编译中需要使用elasticsearch的其他jar包:elasticsearch-7.4.2.jarlucene-core-8.2.0.jarx-pack-core-7.4.2.jarnetty-common-4.1.38.Final.jarelasticsearch-core-7.4.2.jar

  1. [root@elastic-a]# ES_HOME=/usr/share/elasticsearch
  2. [root@elastic-a]# javac -cp "$ES_HOME/lib/elasticsearch-7.4.2.jar:$ES_HOME/lib/lucene-core-8.2.0.jar:$ES_HOME/lib/elasticsearch-core-7.4.2.jar:$ES_HOME/modules/netty-common-4.1.38.Final.jar:$ES_HOME/modules/x-pack-core-7.4.2.jar" LicenseVerifier.java
  3. [root@elastic-a]# javac -cp "$ES_HOME/lib/elasticsearch-7.4.2.jar:$ES_HOME/lib/lucene-core-8.2.0.jar:$ES_HOME/lib/elasticsearch-core-7.4.2.jar:$ES_HOME/modules/netty-common-4.1.38.Final.jar:$ES_HOME/modules/x-pack-core-7.4.2.jar" XPackBuild.java

此时在当前目录中会生成两个class文件。

生成新的x-pack-core-7.4.2.jar包并替换源文件

单独创建一个文件夹,名为x-pack-core-new,将x-pack-core-7.4.2.jar文件拷贝这该目录下。使用jar命令进行解压缩,并将class文件替换原来的文件。

  1. jar -xvf x-pack-core-7.4.2.jar
  2. cp ../XPackBuild.class /root/x-pack-core-new/org/elasticsearch/xpack/core/ //按y进行替换确认
  3. cp ../LicenseVerifier.class /root/x-pack-core-new/org/elasticsearch/license/

删除拷贝过来的x-pack-core-7.4.2.jar文件,重新生成x-pack-core-7.4.2.jar包。

  1. rm -rf x-pack-core-7.4.2.jar
  2. jar cvf x-pack-core-7.4.2.jar . //此时的工作目录在x-pack-core-new中

将新的jar包拷贝到源目录中/usr/share/elasticsearch/modules/x-pack-core/,并替换掉源文件。
重新启动elasticsearch,看是否可以启动成功。

更改配置license

修改下载的license文件

使用文本编辑器打开json文件,并将该License的typeexpiry_date_in_millis、分别修改成platinum2524579200999,文件名保存为license.json。这样license就成为白金版,可以使用X-PACK的所有功能。

导入license

在导入license之前,需要将elasticsearch安全配置为false并重启。不然会出现报错Cannot install a [PLATINUM] license unless TLS is configured or security is disabled

  1. [root@elastic-a]# echo "xpack.security.enabled: false" >> /etc/elasticsearch/elasticsearch.yml
  2. [root@elastic-a]# systemctl restart elasticsearch
  3. [root@elastic-a]# curl -XPUT -u elastic 'http://localhost:9200/_xpack/license' -H "Content-Type: application/json" -d @/root/license.json
  4. {"acknowledged":true,"license_status":"valid"} # license写入成功 这里也可以从kibana界面上传license,在“管理” - “许可管理” - “更改许可”

以上信息说明许可导入成功。通过Kibana查看许可为platinum版本。

修改安全连接

  1. [root@elastic-a]# echo "xpack.security.transport.ssl.enabled: true" >> /etc/elasticsearch/elasticsearch.yml
  2. [root@elastic-a]# sed -i 's/xpack.security.enabled: false/xpack.security.enabled: true/g' /etc/elasticsearch/elasticsearch.yml
  3. [root@elastic-a]# systemctl restart elasticsearch

生成用户密码

生成用户密码有两种方式:auto自动方式,interactive交互式手动设置,这里我们采用auto模式。
首先进入到elasticsearch的工作目录。

  1. [root@elastic-a]# cd /usr/share/elasticsearch/bin
  2. [root@elastic-a]# ./elasticsearch-setup-passwords auto
  3. Changed password for user apm_system
  4. PASSWORD apm_system = xxxxxxxxxxxxxxxxxxxxx
  5. Changed password for user kibana
  6. PASSWORD kibana = xxxxxxxxxxxxxxxxxxxxxxxxxxx
  7. Changed password for user logstash_system
  8. PASSWORD logstash_system = xxxxxxxxxxxxxxxxxxx
  9. Changed password for user beats_system
  10. PASSWORD beats_system = xxxxxxxxxxxxxxxxxxxxxxx
  11. Changed password for user remote_monitoring_user
  12. PASSWORD remote_monitoring_user = xxxxxxxxxxxxxxxxxxxxxxxx
  13. Changed password for user elastic
  14. PASSWORD elastic = xxxxxxxxxxxxxxxxxxxxxxxxxx

重新配置Kibana

如果不重新配置Kibana,Kibana无法连接ES

  1. [root@IT-kibana kibana]# cat kibana.yml | grep ^[a-Z]
  2. server.port: 5601
  3. server.host: "xx.xx.xx.xx"
  4. server.name: "kibana"
  5. elasticsearch.hosts: ["http://elastic-a:9200"]
  6. elasticsearch.username: "kibana" //填写相应的用户名
  7. elasticsearch.password: "xxxxxxxxxxxxxxxxxxxxxx" //填写生成的用户密码
  8. i18n.locale: "zh-CN" //Kibana调整为中文

如果要开启TLS参考Elasticsearch 安全功能入门

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注