Frida syntax fo variadic print function - frida

With Frida's Objective-C API ( written in Javascript ), I can:
var b = ObjC.classes.NSString.stringWithString_("foo");
b.isKindOfClass_(ObjC.classes.NSString)
1 // true
b.isKindOfClass_(ObjC.classes.NSUUID)
0 // false
b.isEqualToString_("foo")
1
b.description().toString()
"foo"
But I can't find the correct syntax for a variadic NSString init call like this:
var c = ObjC.classes.NSString.stringWithFormat_('foo', 'bar', 'lives', '...');
c.description().toString()
"foo"
Any ideas?

The below did the trick:
var c = ObjC.classes.NSString.stringWithFormat_('foo ' + 'bar ' + 'lives');
c.description().toString()
"foo bar lives"
console.log(c)
foo bar lives

Related

Dynamically building subtables in a table

I'm trying to figure out how to dynamically build a series of sub-tables inside a lua table. For example
function BuildsubTable()
local oTable = {}
local name = {"P1","P2"}
for i = 1, 2 do
oTable[i] = {name = name[i], "x" = i + 2, "y" = i + 1}
end
return oTable
end
expected output:
oTable = {
{name = "P1", "x"=3, "y"=2},
{name = "P2", "x"=4, "y"=3}
}
Which obviously doesn't work, but you get the idea of what I'm trying to do. This is a rather simple task but in LUA 5.3 this is proving to be difficult. I cannot find a good example of building a table in this manner. Any solutions or other ideas would be appreciated. In Python I would use a class or a simple dictionary.
Your problem is that you quote the string indices. The generic syntax for declaring a table key inside the table constructor is [<key>] = <value>, for example, [20] = 20 or ["x"] = i + 2.
A shorthand for ["<key>"] = <value>, that is, for string indices that are valid variable names, you can write <key> = <value>, for example x = i + 2.
In your code you use a mix of both and write { ..., "x" = i + 2, ... }. A quick google search shows me that in Python, which you mention, you quote string keys in dictionaries, so you probably mixed that up with Lua?
EDIT: I noticed this a bit late, but you can also use ipairs to iterate the first table and table.insert to insert values:
function BuildsubTable()
local oTable = {}
local name = {"P1","P2"}
for i,name in ipairs(name) do
table.insert(oTable, {name = name, "x" = i + 2, "y" = i + 1})
end
return oTable
end
Use
oTable[i] = {name = name[i], x = i + 2, y = i + 1}
DarkWiiPlayers & lhf's answers are the proper way.
But here is how you can fix your current code if you intend to use a string as a key
function BuildsubTable()
local oTable = {}
local name = {"P1","P2"}
for i = 1, 2 do
oTable[i] = {name = name[i], ["x"] = i + 2, ["y"] = i + 1}
end
return oTable
end
Output
{
[1] = { ['name'] = 'P1', ['x'] = 3, ['y'] = 2},
[2] = { ['name'] = 'P2', ['x'] = 4, ['y'] = 3}
}

Changing variable in a table

How can I make a changing variable an element of a table, like so.
local table = {}
local var = 10
Now I want to insert this variable as an element of table.
Something like this:
table[1] = var
What I need is, whenever I call this table[1], even if the variable changes, it will call the actual value of that variable, like this:
print(table[1]) -> prints 10
var = var + 5
print(table[1]) -> prints 15
Is this even possible somehow?
EDIT:
What I want to accomplish is the following: I want to have an element in table that says which variable should be shown. For example:
local var1 = 10
local var2 = 20
Now if I have a table that has elements as strings of those variables like so:
local table = {"var1", "var2"}
Now if I do print(table[1]), of course it will print out "var1", but is there any way I could turn this element of a table that is string into a call for variable when I actually need that variable. You might be asking why don't I just call the var1, but there is a reason which I can explain, but it would be really long. Let's say I just need it this way. Also, the var1/var2 CAN CHANGE.
You have a couple of choices.
1) Field function as a closure over var: Straightforward but requires changes how you use it
local t = {}
local var = 10
t.varf = function() return var end -- varf could be named var but that might be confusing
var = var + 5
print(t.varf()) -- call it to get the value
2) __index metamethod to avoid the explicit function call syntax
local t = {}
local var = 10
setmetatable(t, {
__index = function(_, k)
if k=="var" then return var else return nil
end})
var = var + 5
print(t.var) -- t does not contain a field with key "var" so __index is called
(The __index function is also a closure over var.)
If you want to modify var via t, then look to the __newindex metamethod.
Both methods use closures. A closure is a function that refers to non-global variables outside of its parameters and body.
Numbers in Lua are shared by copy, at time of assignment. The table[1] and var both receive their own, independent copy of the number 10.
If you want to share numbers, you'll need to encapsulate them in their own table.
local table = {}
local var = { value = 10 }
table[1] = var
print(table[1].value) -- prints 10
var.value = var.value + 5
print(table[1].value) -- prints 15
You can also consider creating some kind of abstraction over numbers. A quick example. You'll need to make sure your operations are well defined, though.
local number = {}
number.__index = number
local function Number (value)
return setmetatable({ value = value }, number)
end
function number.__add (a, b)
if type(b) == 'number' then
return Number(a.value + b)
elseif getmetatable(b) == number then
return Number(a.value + b.value)
end
error("one of `number, Number' expected")
end
function number:add (n)
if type(n) == 'number' then
self.value = self.value + n
elseif getmetatable(n) == number then
self.value = self.value + n.value
else
error("one of `number, Number' expected")
end
return self.value
end
function number.__tostring (v)
return v.value .. ''
end
local foo = {}
local bar = Number(10)
foo[1] = bar
print(foo[1]) -- 10
bar:add(5)
print(foo[1]) -- 15
print(bar + 25) -- 40

Wait for the View to be entirely generated before using CoffeeScript

I have a View page with loops that create inputs. These inputs are manipulated through CoffeeScript when the blur signal is triggered. I would like to wait that the ruby loop generates all of my inputs and then launch my script. It works when using the inputs but it doesn't at the initialization.
$(document).ready ->
$('body').on 'blur', 'input', ->
arr = [
'nb_kms'
'parkings'
'trains'
'taxis'
'metros'
'meals'
'hotels'
'amounts'
'o_ns'
'clients'
]
name = $(this).attr('name').slice(0, -2)
if jQuery.inArray(name, arr) != -1
total = 0
$('input[name=\'' + $(this).attr('name') + '\']').each ->
total += parseInt($(this).val())
return
$('#' + name + 'Total').text total
return
$('input[name=\'' + $(this).attr('name') + '\']').first().trigger 'blur'
return
The last line triggers the blur signal but nothing is detected. Why?
Assuming that you posted the exact indentation from your coffeescript file, then you need to be indenting your $('body').on 'blur', 'input', -> handler underneath the $(document).ready -> line. This will make sure the handler isn't defined until the document is fully loaded. Also, you need to then indent the body of your handler as well:
$(document).ready ->
$('body').on 'blur', 'input', ->
arr = [
'nb_kms'
'parkings'
'trains'
'taxis'
'metros'
'meals'
'hotels'
'amounts'
'o_ns'
'clients'
]
name = $(this).attr('name').slice(0, -2)
# etc...
Found the solution:
arr = [
'nb_kms'
'parkings'
'trains'
'taxis'
'metros'
'meals'
'hotels'
'amounts'
'o_ns'
'clients'
]
$(document).ready ->
$('body').on 'blur', 'input', ->
name = $(this).attr('name').slice(0, -2)
if jQuery.inArray(name, arr) != -1
total = 0
$('input[name=\'' + $(this).attr('name') + '\']').each ->
total += parseInt($(this).val())
return
$('#' + name + 'Total').text total
return
alert name + '[]'
$('input[name=\'' + name + '[]\']').first().trigger 'blur'
return
$(document).ready ->
$.each arr, (index, value) ->
$('input[name=\'' + value + '[]\']').first().trigger 'blur'
return
return
I had to put my trigger call in a second $(document).ready. I don't know why it wasn't working inside the first one.
I am sorry for the indentation, I don't know how to make it properly like CoffeeScript (pasting the code breaks the indentation).

LPeg Increment for Each Match

I'm making a serialization library for Lua, and I'm using LPeg to parse the string. I've got K/V pairs working (with the key explicitly named), but now I'm going to add auto-indexing.
It'll work like so:
#"value"
#"value2"
Will evaluate to
{
[1] = "value"
[2] = "value2"
}
I've already got the value matching working (strings, tables, numbers, and Booleans all work perfectly), so I don't need help with that; what I'm looking for is the indexing. For each match of #[value pattern], it should capture the number of #[value pattern]'s found - in other words, I can match a sequence of values ("#"value1" #"value2") but I don't know how to assign them indexes according to the number of matches. If that's not clear enough, just comment and I'll attempt to explain it better.
Here's something of what my current pattern looks like (using compressed notation):
local process = {} -- Process a captured value
process.number = tonumber
process.string = function(s) return s:sub(2, -2) end -- Strip of opening and closing tags
process.boolean = function(s) if s == "true" then return true else return false end
number = [decimal number, scientific notation] / process.number
string = [double or single quoted string, supports escaped quotation characters] / process.string
boolean = P("true") + "false" / process.boolean
table = [balanced brackets] / [parse the table]
type = number + string + boolean + table
at_notation = (P("#") * whitespace * type) / [creates a table that includes the key and value]
As you can see in the last line of code, I've got a function that does this:
k,v matched in the pattern
-- turns into --
{k, v}
-- which is then added into an "entry table" (I loop through it and add it into the return table)
Based on what you've described so far, you should be able to accomplish this using a simple capture and table capture.
Here's a simplified example I knocked up to illustrate:
lpeg = require 'lpeg'
l = lpeg.locale(lpeg)
whitesp = l.space ^ 0
bool_val = (l.P "true" + "false") / function (s) return s == "true" end
num_val = l.digit ^ 1 / tonumber
string_val = '"' * l.C(l.alnum ^ 1) * '"'
val = bool_val + num_val + string_val
at_notation = l.Ct( (l.P "#" * whitesp * val * whitesp) ^ 0 )
local testdata = [[
#"value1"
#42
# "value2"
#true
]]
local res = l.match(at_notation, testdata)
The match returns a table containing the contents:
{
[1] = "value1",
[2] = 42,
[3] = "value2",
[4] = true
}

Parsing POST request with unexpected URL encoding

This question follows an earlier one.
Here is some code that reproduces the problem:
POST:
str = "accountRequest=<NewUser>" & vbLf & _
"Hello" & vbTab & "World" & vbLf & _
"</NewUser>"
Set objHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
objHTTP.open "POST", "service.asp", False
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.send str
response.Write(objHTTP.responseText)
Set objHTTP = Nothing
service.asp:
function w (str)
response.Write(str & "<br>")
end function
str = request.Form("accountRequest")
w(str)
w("Tabs: "& InStr(str,vbTab))
w("Lines: "& InStr(str,vbLf))
output:
HelloWorld
Tabs: 0
Lines: 0
Can anyone please help?
Try:
Replace(Request.Form("accountRequest"), vbLF, vbCRLF))
Or:
Replace(Request.Form("accountRequest"), vbLF, "<br>"))|
Depending on where you're displaying it, either should work.
Or possibly this:
Function URLDecode(sConvert)
Dim aSplit
Dim sOutput
Dim I
If IsNull(sConvert) Then
URLDecode = ""
Exit Function
End If
' convert all pluses to spaces
sOutput = REPLACE(sConvert, "+", " ")
' next convert %hexdigits to the character
aSplit = Split(sOutput, "%")
If IsArray(aSplit) Then
sOutput = aSplit(0)
For I = 0 to UBound(aSplit) - 1
sOutput = sOutput & _
Chr("&H" & Left(aSplit(i + 1), 2)) &_
Right(aSplit(i + 1), Len(aSplit(i + 1)) - 2)
Next
End If
URLDecode = sOutput
End Function
From here: http://www.aspnut.com/reference/encoding.asp
If they are coming across the wire as the actual "\" and "n" characters, you can do a replace on those characters with the appropriate vbCRLF and vbTAB constants.
Finally figured out that ASP Request.Form method doesn't preserve tabs if they're in the "\t" format (as opposed to URL encoded). However, PHP's $_POST does.

Resources