Skip to content

Instantly share code, notes, and snippets.

@hirman74
Last active September 13, 2023 04:39
Show Gist options
  • Save hirman74/6abd3392d3e4f02b9f21bb4072c0b8ef to your computer and use it in GitHub Desktop.
Save hirman74/6abd3392d3e4f02b9f21bb4072c0b8ef to your computer and use it in GitHub Desktop.
HTML application page to assist in generating "pfiles" command and extract the info for "netstat" count of relevant open session to external host.
<!DOCTYPE html>
<head>
<title>pfiles process port finder</title>
<!--<meta http-equiv="x-ua-compatible" content="ie=11"/>-->
<!--<meta http-equiv="x-ua-compatible" content="ie=edge" />-->
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="UTF-8">
<HTA:APPLICATION
ID="objHTApfilesCollector"
APPLICATIONNAME="HTApfilesCollector"
SCROLL="yes"
WINDOWSTATE="maximize"
SINGLEINSTANCE="yes"
borderStyle="raised"
caption="yes"
contextMenu="yes"
innerBorder="yes"
maximizeButton="yes"
minimizeButton="yes"
navigable="yes"
scrollFlat="yes"
selection="yes"
showInTaskBar="yes"
sysMenu="yes"
version="1.0"
/>
<style>
textarea{
height: 190px;
width: 100%;
box-sizing:border-box;
}
</style>
</head>
<body>
<a href="." id="localREF" ></a>
<input type="checkbox" id="localhost" name="localhost" value="localhostRemoved" checked>
<label for="localhost"> Remove Localhost IP</label><br>
<input type="checkbox" id="intiatePort" name="intiatePort" value="Port666" checked>
<label for="intiatePort"> Remove handshake start port</label><br>
<label for="portNum">Process ID to query:</label>
<input type="text" id="portNum" name="portNum" value="1234"><br><br>
<input type="submit" value="Generate pfiles command" onclick="javascript:return writePfiles();" ><br><br>
<hr>
<br>
<label for="pfilesCommand">Copy and paste this pfiles command into the server with root priviledge:</label><br>
<span id="pfilesGen"><i>pfiles xxx</i></span>
<br><br>
<label for="pfiles">Paste pfiles result here</label>
<textarea id="pfiles" name="pfiles" rows="5" cols="20">
Sample Example (delete this content before pasting actual pfiles result)
199.1.10.1 63354 199.1.20.1 2545
199.1.10.1 63354
0.0.0.0 6669
199.1.10.1 6669 199.1.10.1 63357
199.1.10.1 36646 199.50.10.3 15213
199.1.10.1 6669 199.60.10.1 53964
199.1.10.1 6669 199.60.10.1 53967
199.1.10.1 6669 199.60.10.2 48604
199.1.10.1 6669 199.30.10.1 63007
199.1.10.1 6669 199.60.10.2 48609
199.1.10.1 63360 199.1.10.1 6669
199.1.10.1 6669 199.1.10.1 63360
199.1.10.1 6669 199.1.10.1 63362
199.1.10.1 6669 199.1.10.1 63364
199.1.10.1 6669 199.30.10.1 63027
199.1.10.1 6669 199.50.10.1 43174
199.1.10.1 63354 199.30.10.1 63100
199.1.10.1 63523 199.1.10.1 63379
199.1.10.1 63375 199.50.10.1 6667
199.1.10.1 63354 199.50.10.1 43315
199.1.10.1 63376 199.50.10.1 48231
199.1.10.1 6669 199.1.10.1 63382
199.1.10.1 63354 199.2.10.1 55573
199.1.10.1 63354 199.60.10.2 48784
199.1.10.1 6669 199.1.20.1 2609
199.1.10.1 63354 199.60.10.2 48785
199.1.10.1 63354 199.2.10.1 43239
199.1.10.1 63354 199.60.10.1 54146
199.1.10.1 63354 199.60.10.1 54147
199.1.10.1 63388 199.60.10.1 6696
</textarea><br><br><hr><br>
<label for="localToLocal"> Remove local to local port communication and no destination host session</label><br><br>
<input type="submit" value="View relevant port session" onclick="javascript:return filterHost();" ><br><br>
<span id="relevantHost" name="relevantHost"></span><br><br><hr><br>
<input type="submit" value="Generate netstat command" onclick="javascript:return writeNetstat();" ><br><br>
<label for="netstatCommand">Copy and paste this netstat command into the server with root priviledge:</label><br>
<span id="netstatGen"></span><br><br>
</body>
<!--Starts Here-->
<script type="text/javascript">
var objFSO = new ActiveXObject("Scripting.FileSystemObject"),
ForWriting = 2, ForReading = 1,
ForAppending = 8, CreateIt = true,
dontWantCreateIt = false, AsciiMode = 0,
UnicodeMode = -1, systemDefaultMode = -2;
var objShell = new ActiveXObject("WScript.shell"), shellapp = new ActiveXObject("Shell.Application");
var customFileFolder = {
deleteFile : function (filePath) {
if (objFSO.FileExists(filePath)) {
var afile = objFSO.GetFile(filePath);
afile.Attributes[0];
afile.Delete();
}
},
copyFile : function (FromFile,ToFile,overwrite) {
// Set overwrite to true or false; FromFile, etc = full paths
var f = objFSO.GetFile(FromFile);
f.Attributes[0];
f.Copy(ToFile,overwrite);
},
makeFolder : function (DesiredPath){
var f = objFSO.CreateFolder(DesiredPath); // 'DesiredFolderPath' : e.g., "C:\\MainFolder\\NewFolderName".
},
deleteFolder : function (DesiredPath){ // where gpath = full folder path
if (objFSO.FolderExists(DesiredPath)) {
var afolder = objFSO.GetFolder(DesiredPath);
afolder.Delete();
}
},
copyFolder : function (FromFolder,ToFolder,overwrite) {
// where FromFolder, etc = full folder paths
var f = objFSO.GetFolder(FromFolder);
f.Copy(ToFolder,overwrite);
},
checkMakeFolder : function (NewFolderName) {
if (objFSO.FolderExists(NewFolderName)) {} else {var afolder = objFSO.CreateFolder(NewFolderName);}
}
}
function filterHost() {
//https://resultfor.dev/936369-split-string-in-javascript-and-detect-line-break
var getArray = document.getElementById("pfiles").innerHTML.split(/\r?\n/); //split innerHTML with line break
var collectSessionHost = []; //for collecting relevant information
document.getElementById("relevantHost").innerHTML = ""; //clear previous result
for (var num in getArray) { //reading each line by line
var item = getArray[num];
if (item.indexOf("Sample") == -1 && item.length > 0) { //if the line is not Sample and not empty
var splitItem = item.split(' '); //split the line with space
if (splitItem.length == 4 && splitItem[0].length > 0) { //if the line has 4 items and the first item is not empty
if (splitItem[0] != splitItem[2]) { //if the first item is not equal to the third item
collectSessionHost.push("From " + splitItem[0] + " : " + splitItem[1] + " to " + splitItem[2] + " : " + splitItem[3]); //push the line into the array
document.getElementById("relevantHost").innerHTML = collectSessionHost.join("<br>"); //display the array
}
}
}
}
}
function writePfiles() {
//change the pfiles command based on user selection.
//remove all local host IP address for source or destination and wildcard IP
if (document.getElementById("localhost").checked == true){
localhost = " | egrep -v 'sockname: AF_INET 127.' | egrep -v 'sockname: AF_INET 0.0.0.0'";
} else {
localhost = '';
}
//remove all port 66.. for source or destination
if (document.getElementById("intiatePort").checked == true){
intiatePort = " | egrep -v 'port: 66..'";
} else {
intiatePort = '';
}
//generate the pfiles command
var pfilesGen = document.getElementById('pfilesGen');
document.getElementById("pfilesGen").innerHTML = "pfiles " + document.getElementById("portNum").value + " | egrep -i 'sockname|peername'" + localhost + intiatePort + " | sed -e ':a' -e 'N;$!ba' -e 's&#47;&#92;n&#47; &#47;g' | sed $'s/sockname/&#92;&#92;&#92;nsockname/g' | cut -d' ' -f 3,6,9,12";
document.getElementById("pfilesGen").style.fontStyle = "italic";
document.getElementById("pfilesGen").style.fontWeight = "bold";
}
function unique(arr) {
var result = [];
for (var i in arr) {
var noRepeat = true;
for (var j in result) {
if (arr[i] === result[j]) {
noRepeat = false;
break;
}
}
if (noRepeat) {
result.push(arr[i]);
}
}
return result;
}
function writeNetstat() {
//change the netstat command based on user selection.
if (document.getElementById("relevantHost").innerHTML == "") {
alert ("Please complete all the steps above first.");
} else {
var getArray = document.getElementById("relevantHost").innerHTML.split("<br>"); //split innerHTML with line break
var notUnique = [];
for (var num in getArray) {
notUnique.push(getArray[num].split(' to ')[0].split(' : ')[1]);
}
getUnique = unique(notUnique);
document.getElementById("netstatGen").innerHTML = 'arp -a | egrep "' + getUnique.join("|") + '" | wc -l';
document.getElementById("netstatGen").style.fontStyle = "italic";
document.getElementById("netstatGen").style.fontWeight = "bold";
}
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment