如何在Linux发行版上找到glibc.so以动态加载它

问题描述

我正在尝试在ubuntu(最好是其他Linux发行版)下动态加载glibc。

目的是打电话给seteuid。

我不确定识别路径的正确方法。

运行find命令时,我什至看不到glib.so,但我确实看到了lib.so。

/usr/lib/x86_64-linux-gnu/libc.so

这是正确的文件吗?

如果它是正确的文件,那么在任何系统上找到它的正确方法是什么(硬编码路径将是最后的选择)。

静态链接不是一种选择,因为我是使用ffi从Dart调用该方法的。

解决方法

所以我找到了答案:

var path ='libc.so.6';

在路径上看起来像它,并且包含seteuid方法。

这是我最后得到的最终代码:

import 'dart:ffi';
import 'dart:io' show Platform;

import 'package:dcli/dcli.dart';


// on systems with _POSIX_SAVED_IDS defined
typedef setEffectiveUID_func = Int32 Function(Uint32 effectiveUID);
typedef setEffectiveUID = int Function(int effectiveUID);

/// on other systems.
/// sets the effective and real uids.
typedef setUID_func = Int32 Function(Uint32 realUID,Uint32 effectiveUID);
typedef setUID = int Function(int realUID,int effectiveUID);


class Priviliges {
  static final Priviliges _self = Priviliges._internal();

  DynamicLibrary dylib;

  // the logged in user's original UID
  int userUID;

  /// true if we are running under sudo.
  bool sudo;

  /// I'm confused.
  int realUID;

  /// The user's effective UID when the script started.
  /// If they are running as sudo then this will be root.
  int originalEffectiveUID;

  /// Used to track what UID is currently in effect.
  int currentEffectiveUID;

  factory Priviliges() => _self;

  Priviliges._internal() {
    var path = 'libc.so.6';
    if (Platform.isMacOS) path = '/usr/lib/libSystem.dylib';
    if (Platform.isWindows) path = r'primitives_library\Debug\primitives.dll';
    dylib = DynamicLibrary.open(path);

    realUID = _realUID;

    var sudo_uid = env['SUDO_UID'];
    if (sudo_uid != null) {
      sudo = true;
      userUID = int.tryParse(sudo_uid);
    } else {
      /// we aren't running sudo
      sudo = false;
      userUID = realUID;
    }

    originalEffectiveUID = _effectiveUID;
    currentEffectiveUID = originalEffectiveUID;
  }


  int get _effectiveUID {
    final geteuidPointer =
        dylib.lookup<NativeFunction<getEffectiveUID_func>>('geteuid');
    final geteuid = geteuidPointer.asFunction<getEffectiveUID>();
    var uid = geteuid();

    print('get effiective guid=$uid');
    return uid;
  }

  set _effectiveUID(int effectiveUID) {
    print('setting effective to $effectiveUID');
    var result = -1;
    try {
      final seteuidPointer =
          dylib.lookup<NativeFunction<setEffectiveUID_func>>('seteuid');
      final seteuid = seteuidPointer.asFunction<setEffectiveUID>();

      print(blue('settting effectiveUID =$effectiveUID'));
      result = seteuid(effectiveUID);
    } on ArgumentError catch (_) {
      // seteuid isn't available so lets try setreuid

      final setreuidPointer =
          dylib.lookup<NativeFunction<setUID_func>>('setreuid');
      final setreuid = setreuidPointer.asFunction<setUID>();
      result = setreuid(-1,effectiveUID);
    }

    if (result != 0) {
      throw PriviligesException('Unable to set the Effective UID: $result');
    }
  }
}

class PriviligesException extends DCliException {
  PriviligesException(String message) : super(message);
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...