Skip to content

Instantly share code, notes, and snippets.

@rbobillot
Last active January 27, 2016 21:49
Show Gist options
  • Save rbobillot/5deb972f46a8b57465ec to your computer and use it in GitHub Desktop.
Save rbobillot/5deb972f46a8b57465ec to your computer and use it in GitHub Desktop.
Quick Scala program reading a Mac's keychain-dump, and displaying it quite nicely
import io.Source
object Main {
def formatInfos( infos:Array[String], password:String ) = {
val source = infos
.filter( _.contains("0x00000007 <blob>=") ).head.split( "<blob>=" ).last
val login = infos
.filter( _.contains("\"acct\"<blob>=") ).head.split( "<blob>=" ).last
val pass = password.split( "data:" ).last.split("\n\"|\"$")(1)
if ( pass.size > 1 )
println (
"Source: \u001b[96m" + source + "\u001b[0m\n"
+ " \u001b[0mLogin: \u001b[96m" + login + "\n"
+ " \u001b[0mPassword: \u001b[96m" + pass + "\u001b[0m\n"
)
}
def showDatas( datas:List[Array[String]] ) =
datas.filter( e => e.last.size < 80 && e.last.size > 7 )
.foreach ( e => formatInfos( e.head.split("\\n"), e.last ) )
def getDatas( content:Array[String] ) = {
val PAT = "attributes:|data:"
var datas = List[Array[String]]()
content.map { e =>
PAT.r.replaceAllIn(e, "_-s-_$0")
}.foreach { e =>
datas ++= ( e.split("_-s-_")
.filter( s => ! PAT.r.findAllIn(s).isEmpty )
.toArray :: Nil
)
}
showDatas( datas )
}
def readFile( file:String ) = {
val content = Source.fromFile( file ).mkString
val arr = content.split("keychain: ")
.filter( _.contains("data:") )
.toArray
getDatas( arr )
}
def main(av:Array[String]) = av.size match {
case 1 => readFile( av.head )
case _ => println( "Please give a file to parse" )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment