从脚本语言调用 Linux 系统调用

jos*_*hlf 15 linux scripting syscalls

我想直接从脚本语言调用 Linux 系统调用(或至少是 libc 包装器)。我不在乎什么脚本语言 - 重要的是它不被编译(原因基本上与不想要依赖路径中的编译器有关,但这既不存在也不存在)。是否有任何脚本语言(shell、Python、Ruby 等)允许这样做?

特别是,它是getrandom系统调用。

der*_*ert 33

Perl 的syscall功能允许这样做:

$ perldoc -f syscall
    syscall NUMBER, LIST
            Calls the system call specified as the first element of the list,
            passing the remaining elements as arguments to the system call. If
?
Run Code Online (Sandbox Code Playgroud)

该文档还提供了调用 write(2) 的示例:

$ perldoc -f syscall
    syscall NUMBER, LIST
            Calls the system call specified as the first element of the list,
            passing the remaining elements as arguments to the system call. If
?
Run Code Online (Sandbox Code Playgroud)

不过,不能说我曾经使用过这个功能。好了,刚才确认一下之前的例子确实有效。

这似乎适用于getrandom

$ perl -E 'require "syscall.ph"; $v = " "x8; syscall(SYS_getrandom(), $v, length $v, 0); print $v' | xxd
00000000: 5790 8a6d 714f 8dbe                      W..mqO..
Run Code Online (Sandbox Code Playgroud)

如果您的 syscall.ph 中没有 getrandom,那么您可以使用该数字代替。在我的 Debian 测试 (amd64) 机器上是 318。请注意 Linux 系统调用号是特定于体系结构的。

  • Perl - B 计划的锤子! (2认同)

cg9*_*909 28

在 Python 中,您可以使用该ctypes模块访问动态库中的任意函数,包括syscall()来自 libc:

import ctypes

SYS_getrandom = 318 # You need to check the syscall number for your target architecture

libc = ctypes.CDLL(None)
_getrandom_syscall = libc.syscall
_getrandom_syscall.restypes = ctypes.c_int
_getrandom_syscall.argtypes = ctypes.c_int, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t, ctypes.c_uint

def getrandom(size, flags=0):
    buf = (ctypes.c_char * size)()
    result = _getrandom_syscall(SYS_getrandom, buf, size, flags)
    if result < 0:
        raise OSError(ctypes.get_errno(), 'getrandom() failed')
    return bytes(buf)
Run Code Online (Sandbox Code Playgroud)

如果您的 libc 包含getrandom()包装函数,您也可以调用它:

import ctypes

libc = ctypes.CDLL(None)
_getrandom = libc.getrandom
_getrandom.restypes = ctypes.c_int
_getrandom.argtypes = ctypes.POINTER(ctypes.c_char), ctypes.c_size_t, ctypes.c_uint

def getrandom(size, flags=0):
    buf = (ctypes.c_char * size)()
    result = _getrandom(buf, size, flags)
    if result < 0:
        raise OSError(ctypes.get_errno(), 'getrandom() failed')
    return bytes(buf)
Run Code Online (Sandbox Code Playgroud)


lge*_*get 17

Ruby 有一个syscall(num [, args...]) ? integer函数。

例如:

irb(main):010:0> syscall 1, 1, "hello\n", 6
hello
=> 6
Run Code Online (Sandbox Code Playgroud)

getrandom()

irb(main):001:0> a = "aaaaaaaa"
=> "aaaaaaaa"
irb(main):002:0> syscall 318,a,8,0
=> 8
irb(main):003:0> a
=> "\x9Cq\xBE\xD6|\x87\u0016\xC6"
irb(main):004:0> 
Run Code Online (Sandbox Code Playgroud)