-
Notifications
You must be signed in to change notification settings - Fork 2
/
psp-inviscloak.groovy
133 lines (116 loc) · 5.1 KB
/
psp-inviscloak.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
pipeline {
agent any
environment {
// << CHANGE THESE >>
TOOLNAME = "Rubeus"
OBS_TOOLNAME = "Dudebeus"
GITURL = "https://github.com/GhostPack/Rubeus.git"
BRANCH = "master"
WORKDIR = "C:\\opt\\jenkins-psp" // git-cloned directory
PSP_OUTPUT = "${WORKDIR}\\Invoke-${OBS_TOOLNAME}.ps1"
OBS_PSP_OUTPUT = "${WORKDIR}\\Obs-Invoke-${OBS_TOOLNAME}.ps1"
// << CHANGE THESE >> - .NET Compile configs
CONFIG="Release"
PLATFORM="Any CPU"
DOTNETVERSION="v4.0"
DOTNETNUMBER="net40"
// 3rd party tools
INVISCLOAKPATH = "${WORKDIR}\\InvisibilityCloak\\InvisibilityCloak.py"
CHAMELEONPATH = "${WORKDIR}\\chameleon\\chameleon.py"
EMBEDDOTNETPATH = "${WORKDIR}\\embedDotNet.ps1"
PREPPSPPATH = "${WORKDIR}\\PSPprep.ps1"
TEMPLATEPATH = "${WORKDIR}\\template.ps1"
}
stages {
stage('Cleanup'){
steps{
deleteDir()
dir("${TOOLNAME}"){
deleteDir()
}
}
}
// Try main, then master for old github repos.
stage('Git-Clone'){
steps{
script {
checkout([
$class: 'GitSCM',
branches: [[name: "*/${BRANCH}"]],
userRemoteConfigs: [[url: "${GITURL}"]]
])
}
}
}
// Skip prep powersharppack if the tool already has public class/main function.
stage('Prep-PSP'){
steps{
powershell "${PREPPSPPATH} -inputDir ${WORKSPACE} -toolName ${TOOLNAME}"
}
}
// Obfuscate with invisibilitycloak.
stage('InvisibilityCloak-Obfuscate') {
steps {
bat """python ${INVISCLOAKPATH} -d ${WORKSPACE} -n ${OBS_TOOLNAME} -m rot13 """
}
}
// Some projects doesn't need nuget restore. Continue on failure.
// TODO: what's this "msbuild PROJECT.sln /t:Restore /p:Configuration=Release"
stage('Nuget-Restore'){
steps{
script{
def slnPath = powershell(returnStdout: true, script: "(Get-ChildItem -Path ${WORKSPACE} -Include '${OBS_TOOLNAME}.sln' -Recurse).FullName")
env.SLNPATH = slnPath
try{
bat "nuget restore ${SLNPATH}"
}
catch(Exception e){
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
bat """dotnet restore ${SLNPATH} """
}
}
}
}
}
// If Compilation fails due to invisiblity cloak, run without string obfuscation (delete -m rot 13)
stage('Compile'){
steps {
script{
def slnPath = powershell(returnStdout: true, script: "(Get-ChildItem -Path ${WORKSPACE} -Include '${OBS_TOOLNAME}.sln' -Recurse).FullName")
env.SLNPATH = slnPath
try{
bat "\"${tool 'MSBuild_VS2019'}\\MSBuild.exe\" /p:Configuration=${CONFIG} \"/p:Platform=${PLATFORM}\" /maxcpucount:%NUMBER_OF_PROCESSORS% /nodeReuse:false /p:DebugType=None /p:DebugSymbols=false /p:TargetFrameworkMoniker=\".NETFramework,Version=${DOTNETVERSION}\" ${SLNPATH}"
}
catch(Exception e){
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
bat """dotnet build --configuration ${CONFIG} ${SLNPATH} """
}
}
}
}
}
stage('Create-PSP'){
steps{
script{
def exePath = powershell(returnStdout: true, script: """
\$exeFiles = (Get-ChildItem -Path ${WORKSPACE} -Include '*.exe' -Recurse | Where-Object {\$_.DirectoryName -match 'release' -and \$_.DirectoryName -match 'bin' } ).FullName
if (\$exeFiles -match "${DOTNETNUMBER}"){
\$exeFiles.trim()
}
else{
(Get-ChildItem -Path ${WORKSPACE} -Include '*.exe' -Recurse | Where-Object {\$_.DirectoryName -match 'release'} )[0].FullName
}
""")
env.EXEPATH = exePath
// Beaware of environment variable created from ps in jenkins (exePath). Always .trim() INSIDE powershell.
powershell "${EMBEDDOTNETPATH} -inputFile \"${EXEPATH}\".trim() -outputFile ${PSP_OUTPUT} -templatePath ${TEMPLATEPATH} -toolName ${OBS_TOOLNAME}"
}
}
}
stage('Obfuscate-PSP'){
steps{
bat encoding: 'UTF-8', script: """python ${CHAMELEONPATH} -v -d -c -f -r -i -l 4 ${PSP_OUTPUT} -o ${OBS_PSP_OUTPUT}"""
}
}
}
}