|
| 1 | +/* Written by oxagast. Edited some things from mycliget to get this to work. */ |
| 2 | + |
| 3 | +/* |
| 4 | + * V: 0.0.9 - 3/25/2019 |
| 5 | +*/ |
| 6 | + |
| 7 | +/* |
| 8 | + * clipboard function originated with mozilla webextension examples here: |
| 9 | + * https://github.com/mdn/webextensions-examples |
| 10 | + */ |
| 11 | + |
| 12 | + |
| 13 | +var quotesOption = false; |
| 14 | +var programOption = 'curl'; |
| 15 | +var fileOption = 'auto'; |
| 16 | +var verboseOption = false; |
| 17 | +var headers = ''; |
| 18 | +var sqlmapheaders = ''; |
| 19 | +var snackbarOption = false; |
| 20 | +var trigger; |
| 21 | + |
| 22 | + |
| 23 | +//Right click context adds for links + audio/video |
| 24 | +browser.contextMenus.create({ |
| 25 | + id: "copy-link-to-clipboard", |
| 26 | + title: "Copy SQLMap command to clipboard", |
| 27 | + contexts: ["link"] |
| 28 | +}); |
| 29 | +browser.contextMenus.create({ |
| 30 | + id: "copy-media-to-clipboard", |
| 31 | + title: "Copy SQLMap command to clipboard", |
| 32 | + contexts: ['video', 'audio'] |
| 33 | +}); |
| 34 | + |
| 35 | +//basic promisified xmlhttpreq, will be stopped at building the request |
| 36 | +let ajaxGet = (obj) => { |
| 37 | + return new Promise((resolve, reject) => { |
| 38 | + let xhr = new XMLHttpRequest(); |
| 39 | + xhr.timeout = 5000; |
| 40 | + xhr.open(obj.method || "GET", obj.url); |
| 41 | + if (obj.headers) { |
| 42 | + Object.keys(obj.headers).forEach(key => { |
| 43 | + xhr.setRequestHeader(key, obj.headers[key]); |
| 44 | + }); |
| 45 | + } |
| 46 | + xhr.send(obj.body || ''); |
| 47 | + resolve(true); |
| 48 | + }); |
| 49 | +}; |
| 50 | + |
| 51 | +// callback for onBeforeSendHeaders listener. |
| 52 | +// Returns a promise and adds cancel request to the xmlhttpreq |
| 53 | +// trigger now looped into main promise.all |
| 54 | +let getHeaders = (e) => { |
| 55 | + headers = ''; |
| 56 | + sqlmapheaders = ''; |
| 57 | + for (let header of e.requestHeaders) { |
| 58 | + sqlmapheaders += " --header '" + header.name + ": " + header.value + "'"; |
| 59 | + } |
| 60 | + //console.log('headers: ' + headers.toString()); |
| 61 | + |
| 62 | + |
| 63 | + var asyncCancel = new Promise((resolve, reject) => { |
| 64 | + resolve({cancel: true}); |
| 65 | + }); |
| 66 | + trigger(true); |
| 67 | + return asyncCancel; |
| 68 | +}; |
| 69 | + |
| 70 | +// main command builder function |
| 71 | +function assembleCmd(url, referUrl) { |
| 72 | + let sqlmapText = "sqlmap.py"; // sqlmap command holder |
| 73 | + if (verboseOption) {sqlmapText += " -v 4"; } |
| 74 | + // ###################################################################### |
| 75 | + // use remote suggested filename, how safe is this? also only available in moderately up to date |
| 76 | + // ## replacement for -O -J, same security issues though, make optional |
| 77 | + // ## this version will accept filename or location header |
| 78 | + // curl -s -D - "$url" -o /dev/null | grep -i "filename\|Location" | (IFS= read -r spo; sec=$(echo ${spo//*\//filename=}); echo ${sec#*filename=}); |
| 79 | + // ###################################################################### |
| 80 | + |
| 81 | + |
| 82 | + sqlmapText += sqlmapheaders; |
| 83 | + try { |
| 84 | + if (sqlmapUserOption.replace(/\s/g,'')) { sqlmapText += " " + sqlmapUserOption; } |
| 85 | + } |
| 86 | + catch (e) { |
| 87 | + //ignore empty user option text inputs |
| 88 | + } |
| 89 | + sqlmapText += " -u '" + url + "'"; |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + if (quotesOption) { |
| 94 | + sqlmapText = wgetText.replace(/'/g,'"'); |
| 95 | + } |
| 96 | + const sqlmapCode = "copyToClipboard(" + JSON.stringify(sqlmapText) + ", " + snackbarOption + ");"; |
| 97 | + |
| 98 | + switch (programOption) { |
| 99 | + case "sqlmap": |
| 100 | + return (sqlmapCode); |
| 101 | + break; |
| 102 | + } |
| 103 | + //return (programOption === "curl") ? curlCode : wgetCode; |
| 104 | +}; |
| 105 | + |
| 106 | +function copyCommand(code, tab) { |
| 107 | +browser.tabs.executeScript({ |
| 108 | + code: "typeof copyToClipboard === 'function';", |
| 109 | + }).then((results) => { |
| 110 | + // The content script's last expression will be true if the function |
| 111 | + // has been defined. If this is not the case, then we need to run |
| 112 | + // clipboard-helper.js to define function copyToClipboard. |
| 113 | + if (!results || results[0] !== true) { |
| 114 | + return browser.tabs.executeScript(tab.id, { |
| 115 | + file: "clipboard-helper.js", |
| 116 | + }); |
| 117 | + } |
| 118 | + }).then(() => { |
| 119 | + return browser.tabs.executeScript(tab.id, { |
| 120 | + code, |
| 121 | + }); |
| 122 | + }).catch((error) => { |
| 123 | + // This could happen if the extension is not allowed to run code in |
| 124 | + // the page, for example if the tab is a privileged page. |
| 125 | + console.error("Failed : " + error); |
| 126 | + }); |
| 127 | +}; |
| 128 | + |
| 129 | +browser.contextMenus.onClicked.addListener((info, tab) => { |
| 130 | + |
| 131 | + let url = (info.menuItemId === 'copy-media-to-clipboard') ? info.srcUrl : info.linkUrl |
| 132 | + let referUrl = info.pageUrl; |
| 133 | + let cleanedUrl = ''; |
| 134 | + |
| 135 | + let gettingHeaders = new Promise(function(resolve, reject) {trigger = resolve;}); |
| 136 | + |
| 137 | + // basic port num strip for certain urls |
| 138 | + // TODO more robust sanitize of url for match pattern |
| 139 | + if (RegExp('//').test(url)) { |
| 140 | + let temp = url.split('/'); |
| 141 | + temp[2] = temp[2].replace(/:[0-9]+/, ''); |
| 142 | + cleanedUrl = temp.join('/'); |
| 143 | + } |
| 144 | + |
| 145 | + // add onbeforesendheaders listener for clicked url |
| 146 | + browser.webRequest.onBeforeSendHeaders.addListener( |
| 147 | + getHeaders, {urls: [cleanedUrl]}, ["blocking","requestHeaders"]); |
| 148 | + |
| 149 | + // workaround for xmlhttpget firing before addlistener is complete |
| 150 | + let gettingHtml = new Promise (function(resolve,reject) { |
| 151 | + setTimeout(function(){ |
| 152 | + resolve(ajaxGet({url: url})); |
| 153 | + }, 1); |
| 154 | + }); |
| 155 | + |
| 156 | + // check the saved options each click in case they changed |
| 157 | + let gettingOptions = browser.storage.sync.get( |
| 158 | + ['quotes','prog','file','filename','ratelimit','verbose','resume','wgetUser','curlUser', 'sqlmapUser', 'snackbar']) |
| 159 | + .then((res) => { |
| 160 | + quotesOption = res.quotes; |
| 161 | + programOption = res.prog; |
| 162 | + fileOption = res.file; |
| 163 | + filenameOption = res.filename; |
| 164 | + verboseOption = res.verbose; |
| 165 | + sqlmapUserOption = res.sqlmapUser; |
| 166 | + snackbarOption = res.snackbar; |
| 167 | + }); |
| 168 | + let promiseCancel = new Promise(function(resolve,reject) { |
| 169 | + setTimeout(function() { |
| 170 | + reject(false); |
| 171 | + },2000); |
| 172 | + }); |
| 173 | + |
| 174 | + // 2s timeout if somehow the request was not caught on the |
| 175 | + // header generation. |
| 176 | + let cancelled = Promise.race([promiseCancel, gettingHeaders]); |
| 177 | + |
| 178 | + // loop all requesite async functions into promise.all |
| 179 | + Promise.all([gettingOptions,gettingHtml,cancelled]).then(() => { |
| 180 | + let code = assembleCmd(url, referUrl); |
| 181 | + copyCommand(code,tab); |
| 182 | + |
| 183 | + browser.webRequest.onBeforeSendHeaders.removeListener(getHeaders); |
| 184 | + }, |
| 185 | + () => { |
| 186 | + // reset listener on reject |
| 187 | + browser.webRequest.onBeforeSendHeaders.removeListener(getHeaders); |
| 188 | + }); |
| 189 | + |
| 190 | + |
| 191 | +}); |
| 192 | + |
0 commit comments